From c682e83760291080a7b0b25be1a1b422e1c8a6e0 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Tue, 28 Feb 2012 14:25:37 +0000 Subject: [PATCH] Check for failing dm_asprintf --- daemons/common/daemon-server.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/daemons/common/daemon-server.c b/daemons/common/daemon-server.c index 2221ffc83..c2ba7f347 100644 --- a/daemons/common/daemon-server.c +++ b/daemons/common/daemon-server.c @@ -339,18 +339,23 @@ struct thread_baton { static int buffer_rewrite(char **buf, const char *format, const char *string) { char *old = *buf; - dm_asprintf(buf, format, *buf, string); + int r = dm_asprintf(buf, format, *buf, string); + dm_free(old); - return 0; + + return (r < 0) ? 0 : 1; } static int buffer_line(const char *line, void *baton) { response *r = baton; - if (r->buffer) - buffer_rewrite(&r->buffer, "%s\n%s", line); - else - dm_asprintf(&r->buffer, "%s\n", line); - return 0; + + if (r->buffer) { + if (!buffer_rewrite(&r->buffer, "%s\n%s", line)) + return 0; + } else if (dm_asprintf(&r->buffer, "%s\n", line) < 0) + return 0; + + return 1; } static response builtin_handler(daemon_state s, client_handle h, request r) @@ -387,7 +392,8 @@ static void *client_thread(void *baton) if (!res.buffer) { dm_config_write_node(res.cft->root, buffer_line, &res); - buffer_rewrite(&res.buffer, "%s\n\n", NULL); + if (!buffer_rewrite(&res.buffer, "%s\n\n", NULL)) + goto fail; dm_config_destroy(res.cft); }