[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH (speechd) 2/4] Insure that memory is freed in get_reply, from lib
From: |
Christopher Brannon |
Subject: |
[PATCH (speechd) 2/4] Insure that memory is freed in get_reply, from libspeechd.c. |
Date: |
Fri, 19 Feb 2010 09:50:10 -0600 |
These are the same problems we had with a function in src/server/output.c.
getline was allocating memory on every function call,
but the memory wasn't freed.
Also, it was possible for get_reply to leak memory in case of errors.
---
src/c/api/libspeechd.c | 23 +++++++++++++++++------
1 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/src/c/api/libspeechd.c b/src/c/api/libspeechd.c
index a4f8fb8..56c9b0e 100644
--- a/src/c/api/libspeechd.c
+++ b/src/c/api/libspeechd.c
@@ -1113,6 +1113,7 @@ get_reply(SPDConnection *connection)
size_t N = 0;
int bytes;
char *reply;
+ gboolean errors = FALSE;
str = g_string_new("");
@@ -1125,15 +1126,25 @@ get_reply(SPDConnection *connection)
if (connection->stream != NULL)
fclose(connection->stream);
connection->stream = NULL;
- return NULL;
+ errors = TRUE;
+ } else {
+ g_string_append(str, line);
}
- g_string_append(str, line);
/* terminate if we reached the last line (without '-' after numcode) */
- }while( !((strlen(line) < 4) || (line[3] == ' ')));
+ }while(!errors && !((strlen(line) < 4) || (line[3] == ' ')));
- /* The resulting message received from the socket is stored in reply */
- reply = str->str;
- g_string_free(str, FALSE);
+ xfree(line); /* getline allocates with malloc. */
+
+ if (errors) {
+ /* Free the GString and its character data, and return NULL. */
+ g_string_free(str, TRUE);
+ reply = NULL;
+ } else {
+ /* The resulting message received from the socket is stored in
reply */
+ reply = str->str;
+ /* Free the GString, but not its character data. */
+ g_string_free(str, FALSE);
+ }
return reply;
}
--
1.6.6.1