Fix socket sample issue reported by coverity (#1397)

This commit is contained in:
Wenyong Huang 2022-08-19 18:06:26 +08:00 committed by GitHub
parent ccd664b81e
commit a517bb249a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -46,14 +46,16 @@ run_as_server(void *arg)
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) { if (sock < 0) {
pthread_mutex_unlock(&lock);
perror("Create a socket failed"); perror("Create a socket failed");
goto RETURN; return NULL;
} }
#ifndef __wasi__ #ifndef __wasi__
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) { if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) {
pthread_mutex_unlock(&lock);
perror("Setsockopt failed"); perror("Setsockopt failed");
goto RETURN; goto fail1;
} }
#endif #endif
@ -64,13 +66,15 @@ run_as_server(void *arg)
addrlen = sizeof(addr); addrlen = sizeof(addr);
if (bind(sock, (struct sockaddr *)&addr, addrlen) < 0) { if (bind(sock, (struct sockaddr *)&addr, addrlen) < 0) {
pthread_mutex_unlock(&lock);
perror("Bind failed"); perror("Bind failed");
goto UNLOCK_SHUTDOWN; goto fail1;
} }
if (listen(sock, 0) < 0) { if (listen(sock, 0) < 0) {
pthread_mutex_unlock(&lock);
perror("Listen failed"); perror("Listen failed");
goto UNLOCK_SHUTDOWN; goto fail1;
} }
server_is_ready = true; server_is_ready = true;
@ -82,25 +86,22 @@ run_as_server(void *arg)
new_sock = accept(sock, (struct sockaddr *)&addr, (socklen_t *)&addrlen); new_sock = accept(sock, (struct sockaddr *)&addr, (socklen_t *)&addrlen);
if (new_sock < 0) { if (new_sock < 0) {
perror("Accept failed"); perror("Accept failed");
goto SHUTDOWN; goto fail1;
} }
printf("Start sending. \n"); printf("Start sending. \n");
send_len = sendmsg(new_sock, &msg, 0); send_len = sendmsg(new_sock, &msg, 0);
if (send_len < 0) { if (send_len < 0) {
perror("Sendmsg failed"); perror("Sendmsg failed");
goto SHUTDOWN; goto fail2;
} }
printf("Send %ld bytes successfully!\n", send_len); printf("Send %ld bytes successfully!\n", send_len);
SHUTDOWN: fail2:
close(new_sock);
fail1:
shutdown(sock, SHUT_RD); shutdown(sock, SHUT_RD);
return NULL; close(sock);
UNLOCK_SHUTDOWN:
shutdown(sock, SHUT_RD);
RETURN:
pthread_mutex_unlock(&lock);
return NULL; return NULL;
} }
@ -125,7 +126,7 @@ run_as_client(void *arg)
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) { if (sock < 0) {
perror("Create a socket failed"); perror("Create a socket failed");
goto RETURN; return NULL;
} }
/* 127.0.0.1:1234 */ /* 127.0.0.1:1234 */
@ -135,14 +136,14 @@ run_as_client(void *arg)
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Connect failed"); perror("Connect failed");
goto UNLOCK_SHUTDOWN; goto fail;
} }
printf("Start receiving. \n"); printf("Start receiving. \n");
recv_len = recvmsg(sock, &msg, 0); recv_len = recvmsg(sock, &msg, 0);
if (recv_len < 0) { if (recv_len < 0) {
perror("Recvmsg failed"); perror("Recvmsg failed");
goto SHUTDOWN; goto fail;
} }
printf("Receive %ld bytes successlly!\n", recv_len); printf("Receive %ld bytes successlly!\n", recv_len);
@ -155,14 +156,9 @@ run_as_client(void *arg)
s += strlen(s) + 1; s += strlen(s) + 1;
} }
SHUTDOWN: fail:
shutdown(sock, SHUT_RD); shutdown(sock, SHUT_RD);
return NULL; close(sock);
UNLOCK_SHUTDOWN:
shutdown(sock, SHUT_RD);
RETURN:
pthread_mutex_unlock(&lock);
return NULL; return NULL;
} }