From d7751a3a4adf0bde22278bf8828664a38d08d53b Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sat, 1 Mar 2008 15:00:05 -0500 Subject: Clean up partial messages. Null terminate message buffers. --- src/mysqlerl.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/mysqlerl.c b/src/mysqlerl.c index 1731af2..dc1e868 100644 --- a/src/mysqlerl.c +++ b/src/mysqlerl.c @@ -112,6 +112,7 @@ read_msg() if (msg == NULL) { logmsg("ERROR: Couldn't allocate message for reading: %s.\n", strerror(errno)); + exit(2); } @@ -119,22 +120,29 @@ read_msg() if (restartable_read((char *)&len, sizeof(len)) == -1) { logmsg("ERROR: couldn't read %d byte message prefix: %s.", sizeof(len), strerror(errno)); + + free(msg); exit(2); } len = ntohl(len); - msg->buf = malloc(len); + msg->buf = malloc(len + 1); if (msg->buf == NULL) { logmsg("ERROR: Couldn't malloc %d bytes: %s.", len, strerror(errno)); + + free(msg); exit(2); } - memset(msg->buf, 0, len); + msg->buf[len] = '\0'; logmsg("DEBUG: reading message body (len: %d).", len); if (restartable_read(msg->buf, len) == -1) { logmsg("ERROR: couldn't read %d byte message: %s.", len, strerror(errno)); + + free(msg->buf); + free(msg); exit(2); } -- cgit v1.2.3