From 50b72a29b1cc3155fa3386c1a90252cfab44ca55 Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Wed, 17 Jan 2007 21:35:58 -0500 Subject: [PATCH] Clear the contents of the input buffer before free()ing. --- libpwmd/pwmc.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/libpwmd/pwmc.c b/libpwmd/pwmc.c index 38b55b88..753b3076 100644 --- a/libpwmd/pwmc.c +++ b/libpwmd/pwmc.c @@ -135,7 +135,6 @@ int main(int argc, char *argv[]) } if ((ret = pwmd_command(pwm, &result, &error, PWMD_OPEN, filename)) != PWMD_OK) { - memset(&command, 0, sizeof(command)); pwmd_show_error(ret, error); pwmd_close(pwm); exit(EXIT_FAILURE); @@ -143,11 +142,19 @@ int main(int argc, char *argv[]) while ((p = fgets(command, sizeof(command), stdin)) != NULL) { int len = strlen(p); + char *t; if (p[len - 1] != '\n') { - if ((buf = realloc(buf, (total + len + 1) * sizeof(char))) == NULL) + if ((t = realloc(buf, (total + len + 1) * sizeof(char))) == NULL) { + if (buf) { + memset(buf, 0, total); + free(buf); + } + err(EXIT_FAILURE, "realloc()"); + } + buf = t; memcpy(&buf[total], p, len); total += len; buf[total] = 0; @@ -155,9 +162,16 @@ int main(int argc, char *argv[]) } else { if (buf) { - if ((buf = realloc(buf, (total + len + 1) * sizeof(char))) == NULL) + if ((t = realloc(buf, (total + len + 1) * sizeof(char))) == NULL) { + if (buf) { + memset(buf, 0, total); + free(buf); + } + err(EXIT_FAILURE, "realloc()"); + } + buf = t; memcpy(&buf[total], p, len); total += len; buf[total] = 0; @@ -166,6 +180,11 @@ int main(int argc, char *argv[]) if ((ret = pwmd_command(pwm, &result, &error, PWMD_COMMAND, (buf) ? buf : command)) != PWMD_OK) { + if (buf) { + memset(buf, 0, total); + free(buf); + } + memset(&command, 0, sizeof(command)); pwmd_show_error(ret, error); pwmd_close(pwm); @@ -173,6 +192,7 @@ int main(int argc, char *argv[]) } if (buf) { + memset(buf, 0, total); free(buf); buf = NULL; total = 0; -- 2.11.4.GIT