Skip to content

Commit e7a3d01

Browse files
lpereiraMaureenHelm
authored andcommitted
samples: net: dumb_http_server: Handle recv() errors
The recv() call can return errors, so handle them before reading the received byte. Unrecoverable errors will just trigger the client socket to be closed as usual. Coverity-ID: 182778 Signed-off-by: Leandro Pereira <[email protected]>
1 parent 33aa905 commit e7a3d01

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

samples/net/sockets/dumb_http_server/src/socket_dumb_http.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,19 @@ int main(void)
8282
* connection reset error).
8383
*/
8484
while (1) {
85+
ssize_t r;
8586
char c;
8687

87-
recv(client, &c, 1, 0);
88+
r = recv(client, &c, 1, 0);
89+
if (r < 0) {
90+
if (errno == EAGAIN || errno == EINTR) {
91+
continue;
92+
}
93+
94+
printf("Got error %d when receiving from "
95+
"socket\n", errno);
96+
goto close_client;
97+
}
8898
if (req_state == 0 && c == '\r') {
8999
req_state++;
90100
} else if (req_state == 1 && c == '\n') {
@@ -111,6 +121,7 @@ int main(void)
111121
len -= sent_len;
112122
}
113123

124+
close_client:
114125
close(client);
115126
printf("Connection from %s closed\n", addr_str);
116127

0 commit comments

Comments
 (0)