diff options
author | Mattias Andrée <maandree@kth.se> | 2024-09-13 19:37:07 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-09-13 19:37:07 +0200 |
commit | 61cdde54f4d4f1490bfb9c6452c5aef33355c2a7 (patch) | |
tree | d71cc91e700be46484db004f3089c250fedcc014 | |
parent | Organise files (diff) | |
download | anysum-61cdde54f4d4f1490bfb9c6452c5aef33355c2a7.tar.gz anysum-61cdde54f4d4f1490bfb9c6452c5aef33355c2a7.tar.bz2 anysum-61cdde54f4d4f1490bfb9c6452c5aef33355c2a7.tar.xz |
Fix memory corruption buf
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | anysum.c | 4 | ||||
-rw-r--r-- | cmdline.c | 11 | ||||
-rw-r--r-- | cmdline_sha3sum.c | 2 | ||||
-rw-r--r-- | common.h | 2 |
4 files changed, 10 insertions, 9 deletions
@@ -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; } @@ -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) @@ -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); |