Fix possible non null-terminated string issue in socket sample (#1612)

This commit is contained in:
Wenyong Huang 2022-10-17 22:45:10 +08:00 committed by GitHub
parent 7cc7b56f88
commit ace05b182d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -53,7 +53,7 @@ main(int argc, char *argv[])
struct ip_mreq ipv4_group;
int sd;
int datalen;
char databuf[1024];
char databuf[1024] = { 0 };
char multicast_addr_buffer[16];
struct sockaddr_storage local_address = { 0 };
int addr_type = -1;
@ -116,7 +116,7 @@ main(int argc, char *argv[])
printf("Joined multicast group. Waiting for datagram...\n");
datalen = sizeof(databuf);
datalen = sizeof(databuf) - 1;
read_result = read(sd, databuf, datalen);
if (read_result < 0) {
@ -132,4 +132,4 @@ main(int argc, char *argv[])
fail:
close(sd);
return EXIT_FAILURE;
}
}