]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Rework handling of sexp-conv hashing, fix leak. master-updates
authorNiels Möller <nisse@lysator.liu.se>
Fri, 3 Oct 2025 20:04:20 +0000 (22:04 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Fri, 3 Oct 2025 20:04:20 +0000 (22:04 +0200)
ChangeLog
tools/output.c
tools/output.h
tools/sexp-conv.c

index 9156a14c39b420bf0b7569f8f5e62404a8121166..2ad86a9fdf3cd26f8d0a815a0d41b09995c76729 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-10-03  Niels Möller  <nisse@lysator.liu.se>
+
+       * 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  <nisse@lysator.liu.se>
 
        * tools/sexp-conv-test: Improve tests, including --hash feature.
index 41af74a4c65985b3a0ba5316a8a3d6c390867eac..afc36481b56009225d53060b64cdc43f21778f8c 100644 (file)
 
 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)
 {
index dde4f9d15ab42cc7ae91547ea5fbf0df7bf25243..66472377b055c28f8078220114f1e391381b3dd1 100644 (file)
@@ -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);
index e7357052218aaae821a0adaccfd0b60243171b9b..883e83399bfa08f14d2382bc54515f6eb9dc2abf 100644 (file)
@@ -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;
 }