From 61cdde54f4d4f1490bfb9c6452c5aef33355c2a7 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 13 Sep 2024 19:37:07 +0200 Subject: Fix memory corruption buf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- anysum.c | 4 +++- cmdline.c | 11 +++++------ cmdline_sha3sum.c | 2 +- common.h | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/anysum.c b/anysum.c index de85e46..09ada8d 100644 --- a/anysum.c +++ b/anysum.c @@ -83,11 +83,12 @@ main(int argc, char **argv) struct config config = {.format = LOWERCASE_HEX | WITH_FILENAME | WITH_LF}; char stdin_str[] = "-"; char *stdin_array[] = {stdin_str, NULL}; + char *algostrbuf = NULL; size_t i; int ret; libsimple_default_failure_exit = 2; - cmdline(&argc, &argv, &config); + cmdline(&argc, &argv, &config, &algostrbuf); if (!*argv) argv = stdin_array; @@ -114,5 +115,6 @@ main(int argc, char **argv) for (i = 0; i < config.nalgorithms; i++) free(config.algorithms[i].result); free(config.algorithms); + free(algostrbuf); return ret; } diff --git a/cmdline.c b/cmdline.c index 182a29d..bcb68d8 100644 --- a/cmdline.c +++ b/cmdline.c @@ -34,22 +34,21 @@ NUSAGE(libsimple_default_failure_exit, void -cmdline(int *argcp, char ***argvp, struct config *config) +cmdline(int *argcp, char ***argvp, struct config *config, char **algostrbufp) { int old_argc = *argcp; int argc = *argcp; char **argv = *argvp; const char *algostr = NULL; - char *algostrbuf = NULL; argv0 = argv[0]; command = getcommand(&algostr, &algorithm); switch (getsupercommand(command, algorithm)) { case BSUM: - argc = cmdline_bsum(argc, argv, command, config, &algostr, &algorithm, &algostrbuf); + argc = cmdline_bsum(argc, argv, command, config, &algostr, &algorithm, algostrbufp); break; case SHA3SUM: - argc = cmdline_sha3sum(argc, argv, command, config, &algostr, &algorithm, &algostrbuf); + argc = cmdline_sha3sum(argc, argv, command, config, &algostr, &algorithm, algostrbufp); break; case ANYSUM: case SPECIALISED: @@ -73,12 +72,12 @@ cmdline(int *argcp, char ***argvp, struct config *config) if (algostr) { config->algorithms = erealloc(config->algorithms, sizeof(*config->algorithms)); + memset(&(config->algorithms)[0], 0, sizeof(config->algorithms[0])); (config->algorithms)[0].algostr = algostr; + (config->algorithms)[0].result = NULL; config->nalgorithms = 1U; } *argcp = argc; *argvp = &argv[old_argc - argc]; - - free(algostrbuf); } diff --git a/cmdline_sha3sum.c b/cmdline_sha3sum.c index 8fc33fd..7dc0757 100644 --- a/cmdline_sha3sum.c +++ b/cmdline_sha3sum.c @@ -32,7 +32,7 @@ fmtalgostr(const char *f, long int r, long int c, long int n, long int z) len = snprintf(NULL, 0, "%s[r=%li,c=%li,n=%li,z=%li]", f, r, c, n, z); if (len <= 0) abort(); - ret = emalloc((size_t)len); + ret = emalloc((size_t)len + 1U); p = stpcpy(ret, f); *p++ = '['; if (r) diff --git a/common.h b/common.h index f601ae5..00aae3e 100644 --- a/common.h +++ b/common.h @@ -172,4 +172,4 @@ int cmdline_sha3sum(int argc, char **argv, enum command command, struct config * int cmdline_other(int argc, char **argv, enum command command, struct config *config); /* cmdline.c */ -void cmdline(int *argcp, char ***argvp, struct config *config); +void cmdline(int *argcp, char ***argvp, struct config *config, char **algostrbufp); -- cgit v1.2.3-70-g09d2