Browse Source

Use a more robust mechanism to obtain a \n terminated string over a socket.

nfactor-troky
Con Kolivas 12 years ago
parent
commit
fa4c9bf60f
  1. 17
      util.c

17
util.c

@ -895,8 +895,8 @@ static bool sock_full(SOCKETTYPE sock, bool wait)
* from the socket and returns that as a malloced char */ * from the socket and returns that as a malloced char */
char *recv_line(SOCKETTYPE sock) char *recv_line(SOCKETTYPE sock)
{ {
char *sret = NULL, *s; char *sret = NULL, *s, c;
ssize_t len; ssize_t offset = 0;
s = alloca(RECVSIZE); s = alloca(RECVSIZE);
if (SOCKETFAIL(recv(sock, s, RECVSIZE, MSG_PEEK))) { if (SOCKETFAIL(recv(sock, s, RECVSIZE, MSG_PEEK))) {
@ -908,12 +908,13 @@ char *recv_line(SOCKETTYPE sock)
applog(LOG_DEBUG, "Failed to parse a \\n terminated string in recv_line"); applog(LOG_DEBUG, "Failed to parse a \\n terminated string in recv_line");
goto out; goto out;
} }
len = strlen(sret) + 1;
/* We know how much data is in the buffer so this read should not fail */ do {
if (SOCKETFAIL(recv(sock, s, len, 0))) read(sock, &c, 1);
goto out; memcpy(s + offset++, &c, 1);
if (s) } while (strncmp(&c, "\n", 1));
sret = strdup(strtok(s, "\n")); sret = strdup(s);
strcpy(sret + offset - 1, "\0");
out: out:
if (!sret) if (!sret)
clear_sock(sock); clear_sock(sock);

Loading…
Cancel
Save