Skip to content

Commit

Permalink
Fix partial socket writes
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed Oct 27, 2023
1 parent 1714266 commit c69480e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,21 @@ int monitor_stop(void)

int monitor_send(int instance, const char *symbol, float value)
{
int ret;
int size, ret = -1;

char msg[255];
sprintf(msg, "monitor %d %s %f", instance, symbol, value);
char *buffer = msg;
size = sprintf(msg, "monitor %d %s %f", instance, symbol, value);

ret = send(g_sockfd, msg, strlen(msg) + 1, 0);
if (ret < 0)
while (size > 0)
{
perror("send error");
ret = send(g_sockfd, buffer, size + 1, 0);
if (ret < 0)
{
perror("send error");
}
size -= ret;
buffer += ret;
}

return ret;
Expand Down
13 changes: 9 additions & 4 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,17 @@ void socket_set_receive_cb(void (*receive_cb)(msg_t *msg))

int socket_send(int destination, const char *buffer, int size)
{
int ret;
int ret = -1;

ret = send(destination, buffer, size, 0);
if (ret < 0)
while (size > 0)

This comment has been minimized.

Copy link
@melonlm

melonlm Feb 5, 2024

I belive this fix introduced an error loop.
Lets say the send() function in line 262 hits an error an error like "Socket operation on non-socket".
Then ret = -1, and with each iteration size -= -1, and buffer underflows finnaly. Generally bad things happen (my Raspi crashes)

This comment has been minimized.

Copy link
@falkTX

falkTX Feb 5, 2024

Author Member

if there is an error the send returns < 0 and the loop stops. I dont see how there is an error here

This comment has been minimized.

Copy link
@melonlm

melonlm Feb 5, 2024

Well it does not for me. Execution passes the perror() call, does not stop there.
After that happens the size gets actually increased by 1 with each iteration, so the loop runs away.
Probably a break; is needed after perror().

This comment has been minimized.

Copy link
@melonlm

melonlm Feb 5, 2024

I found it while trying to debug the problem of interactive mode not being usable (see the newest issue)

This comment has been minimized.

Copy link
@falkTX

falkTX Feb 5, 2024

Author Member

then something is wrong with your build, perror is meant as a kill condition, it is supposed to stop the program right away.

This comment has been minimized.

Copy link
@falkTX

falkTX Feb 5, 2024

Author Member

ok no, there is a missing return error in this case indeed. I thought perror would close the program but that is done by something else then.
anyhow, even fixing this part there is still the issue of the sending having an error

This comment has been minimized.

Copy link
@melonlm

melonlm via email Feb 5, 2024

This comment has been minimized.

Copy link
@worikgh

worikgh Feb 5, 2024

As in issue 73 this is getting called with invalid sockets if in interactive mode

{
perror("send error");
ret = send(destination, buffer, size, 0);
if (ret < 0)
{
perror("send error");
}
size -= ret;
buffer += ret;
}

return ret;
Expand Down

0 comments on commit c69480e

Please sign in to comment.