Skip to content

Commit 4370045

Browse files
committed
fix isssues detected by coverity
1 parent fa736d1 commit 4370045

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

samples/socket-api/wasm-src/socket_opts.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@ int
3131
set_and_get_bool_opt(int socket_fd, int level, int optname, int val)
3232
{
3333
int bool_opt = val;
34+
int ret = -1;
3435
socklen_t opt_len = sizeof(bool_opt);
35-
setsockopt(socket_fd, level, optname, &bool_opt, sizeof(bool_opt));
36+
37+
ret = setsockopt(socket_fd, level, optname, &bool_opt, sizeof(bool_opt));
38+
if (ret != 0)
39+
return !val;
40+
3641
bool_opt = !bool_opt;
37-
getsockopt(socket_fd, level, optname, &bool_opt, &opt_len);
42+
ret = getsockopt(socket_fd, level, optname, &bool_opt, &opt_len);
43+
if (ret != 0)
44+
return !val;
45+
3846
return bool_opt;
3947
}
4048

@@ -197,11 +205,13 @@ main(int argc, char *argv[])
197205

198206
// IP_TTL
199207
ttl = 8;
200-
setsockopt(tcp_socket_fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
208+
result = setsockopt(tcp_socket_fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
209+
OPTION_ASSERT(result, 0, "IP_TIL");
201210
ttl = 0;
202211
opt_len = sizeof(ttl);
203-
getsockopt(tcp_socket_fd, IPPROTO_IP, IP_TTL, &ttl, &opt_len);
212+
result = getsockopt(tcp_socket_fd, IPPROTO_IP, IP_TTL, &ttl, &opt_len);
204213
OPTION_ASSERT(ttl, 8, "IP_TTL");
214+
OPTION_ASSERT(result, 0, "IP_TIL");
205215

206216
// IPV6_V6ONLY
207217
OPTION_ASSERT(
@@ -233,11 +243,15 @@ main(int argc, char *argv[])
233243

234244
// IP_MULTICAST_TTL
235245
ttl = 8;
236-
setsockopt(udp_socket_fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
246+
result = setsockopt(udp_socket_fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
247+
sizeof(ttl));
248+
OPTION_ASSERT(result, 0, "IP_MULTICAST_TTL");
237249
ttl = 0;
238250
opt_len = sizeof(ttl);
239-
getsockopt(udp_socket_fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, &opt_len);
251+
result =
252+
getsockopt(udp_socket_fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, &opt_len);
240253
OPTION_ASSERT(ttl, 8, "IP_MULTICAST_TTL");
254+
OPTION_ASSERT(result, 0, "IP_MULTICAST_TTL");
241255

242256
// IPV6_MULTICAST_LOOP
243257
OPTION_ASSERT(set_and_get_bool_opt(udp_ipv6_socket_fd, IPPROTO_IPV6,
@@ -248,12 +262,14 @@ main(int argc, char *argv[])
248262
0, "IPV6_MULTICAST_LOOP disabled");
249263

250264
// IPV6_JOIN_GROUP
251-
setsockopt(udp_ipv6_socket_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mcast_ipv6,
252-
sizeof(mcast_ipv6));
265+
result = setsockopt(udp_ipv6_socket_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
266+
&mcast_ipv6, sizeof(mcast_ipv6));
267+
OPTION_ASSERT(result, 0, "IPV6_JOIN_GROUP");
253268

254269
// IPV6_LEAVE_GROUP
255-
setsockopt(udp_ipv6_socket_fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mcast_ipv6,
256-
sizeof(mcast_ipv6));
270+
result = setsockopt(udp_ipv6_socket_fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
271+
&mcast_ipv6, sizeof(mcast_ipv6));
272+
OPTION_ASSERT(result, 0, "IPV6_JOIN_GROUP");
257273

258274
printf("[Client] Close sockets\n");
259275
close(tcp_socket_fd);

samples/socket-api/wasm-src/udp_client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ main(int argc, char *argv[])
7373
(struct sockaddr *)&server_address, &serverlen);
7474

7575
if (ret > 0) {
76+
buffer[ret] = '\0';
7677
printf("[Client] Buffer recieved: %s\n", buffer);
7778
}
7879

samples/socket-api/wasm-src/udp_server.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ main(int argc, char *argv[])
4343
struct sockaddr_storage addr = { 0 };
4444
char *reply_message = "Hello from server";
4545
unsigned connections = 0;
46-
char ip_string[64];
47-
char buffer[1024];
46+
char ip_string[64] = { 0 };
47+
char buffer[1024] = { 0 };
4848

4949
if (argc > 1 && strcmp(argv[1], "inet6") == 0) {
5050
af = AF_INET6;
@@ -79,6 +79,7 @@ main(int argc, char *argv[])
7979
perror("Read failed");
8080
goto fail;
8181
}
82+
buffer[ret] = '\0';
8283

8384
if (sockaddr_to_string((struct sockaddr *)&addr, ip_string,
8485
sizeof(ip_string) / sizeof(ip_string[0]))

0 commit comments

Comments
 (0)