From: Niels Möller Date: Fri, 3 Oct 2025 20:04:20 +0000 (+0200) Subject: Rework handling of sexp-conv hashing, fix leak. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b61d1557e7f032f6bf738f2c9e031bb57de54f8e;p=thirdparty%2Fnettle.git Rework handling of sexp-conv hashing, fix leak. --- diff --git a/ChangeLog b/ChangeLog index 9156a14c..2ad86a9f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2025-10-03 Niels Möller + + * tools/output.c (sexp_output_init): Add hash argument, allocate + context if non-null. + (sexp_output_hash_init): Deleted function. + * tools/sexp-conv.c (main): Update call to sexp_output_init. Free + context before exiting. + 2025-10-02 Niels Möller * tools/sexp-conv-test: Improve tests, including --hash feature. diff --git a/tools/output.c b/tools/output.c index 41af74a4..afc36481 100644 --- a/tools/output.c +++ b/tools/output.c @@ -46,28 +46,26 @@ void sexp_output_init(struct sexp_output *output, FILE *f, + const struct nettle_hash *hash, unsigned width, int prefer_hex) { output->f = f; output->line_width = width; output->coding = NULL; output->prefer_hex = prefer_hex; - output->hash = NULL; - output->ctx = NULL; + output->hash = hash; + if (output->hash) + { + output->ctx = xalloc (output->hash->context_size); + output->hash->init (output->ctx); + } + else + output->ctx = NULL; output->pos = 0; output->soft_newline = 0; } -void -sexp_output_hash_init(struct sexp_output *output, - const struct nettle_hash *hash, void *ctx) -{ - output->hash = hash; - output->ctx = ctx; - hash->init(ctx); -} - static void sexp_put_raw_char(struct sexp_output *output, uint8_t c) { diff --git a/tools/output.h b/tools/output.h index dde4f9d1..66472377 100644 --- a/tools/output.h +++ b/tools/output.h @@ -64,12 +64,9 @@ struct sexp_output void sexp_output_init(struct sexp_output *output, FILE *f, + const struct nettle_hash *hash, unsigned width, int prefer_hex); -void -sexp_output_hash_init(struct sexp_output *output, - const struct nettle_hash *hash, void *ctx); - void sexp_put_newline(struct sexp_output *output, unsigned indent); diff --git a/tools/sexp-conv.c b/tools/sexp-conv.c index e7357052..883e8339 100644 --- a/tools/sexp-conv.c +++ b/tools/sexp-conv.c @@ -384,7 +384,7 @@ main(int argc, char **argv) sexp_input_init(&input, stdin); sexp_parse_init(&parser, &input, SEXP_ADVANCED); sexp_compound_token_init(&token); - sexp_output_init(&output, stdout, + sexp_output_init(&output, stdout, options.hash, options.width, options.prefer_hex); #if HAVE_FCNTL_LOCKING @@ -402,12 +402,6 @@ main(int argc, char **argv) die("Locking output file failed: %s\n", strerror(errno)); } #endif /* HAVE_FCNTL_LOCKING */ - if (options.hash) - { - /* Leaks the context, but that doesn't matter */ - void *ctx = xalloc(options.hash->context_size); - sexp_output_hash_init(&output, options.hash, ctx); - } sexp_get_char(&input); @@ -439,6 +433,8 @@ main(int argc, char **argv) if (fflush(output.f) < 0) die("Final fflush failed: %s.\n", strerror(errno)); - + + free (output.ctx); + return EXIT_SUCCESS; }