From 4570e01be4f8ee184a71f161ef86f91fda8ad4ae Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 31 Jan 2022 08:42:07 -0800 Subject: [PATCH 21/43] basenc: simplify -fsanitize=leak pacification * src/basenc.c (finish_and_exit): New function. (do_encode, do_decode): Use it. Accept new INFILE arg. Remove no-longer-needed IF_LINT code. Exit when done. Caller changed. --- src/basenc.c | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/basenc.c b/src/basenc.c index ff8ea99c0..b37545929 100644 --- a/src/basenc.c +++ b/src/basenc.c @@ -950,7 +950,21 @@ wrap_write (char const *buffer, idx_t len, } static void -do_encode (FILE *in, FILE *out, idx_t wrap_column) +finish_and_exit (FILE *in, char const *infile) +{ + if (fclose (in) != 0) + { + if (STREQ (infile, "-")) + die (EXIT_FAILURE, errno, _("closing standard input")); + else + die (EXIT_FAILURE, errno, "%s", quotef (infile)); + } + + exit (EXIT_SUCCESS); +} + +static void +do_encode (FILE *in, char const *infile, FILE *out, idx_t wrap_column) { idx_t current_column = 0; char *inbuf, *outbuf; @@ -990,12 +1004,11 @@ do_encode (FILE *in, FILE *out, idx_t wrap_column) if (ferror (in)) die (EXIT_FAILURE, errno, _("read error")); - IF_LINT (free (inbuf)); - IF_LINT (free (outbuf)); + finish_and_exit (in, infile); } static void -do_decode (FILE *in, FILE *out, bool ignore_garbage) +do_decode (FILE *in, char const *infile, FILE *out, bool ignore_garbage) { char *inbuf, *outbuf; idx_t sum; @@ -1057,11 +1070,7 @@ do_decode (FILE *in, FILE *out, bool ignore_garbage) } while (!feof (in)); -#if BASE_TYPE == 42 - IF_LINT (free (ctx.inbuf)); -#endif - IF_LINT (free (inbuf)); - IF_LINT (free (outbuf)); + finish_and_exit (in, infile); } int @@ -1233,17 +1242,7 @@ main (int argc, char **argv) fadvise (input_fh, FADVISE_SEQUENTIAL); if (decode) - do_decode (input_fh, stdout, ignore_garbage); + do_decode (input_fh, infile, stdout, ignore_garbage); else - do_encode (input_fh, stdout, wrap_column); - - if (fclose (input_fh) == EOF) - { - if (STREQ (infile, "-")) - die (EXIT_FAILURE, errno, _("closing standard input")); - else - die (EXIT_FAILURE, errno, "%s", quotef (infile)); - } - - return EXIT_SUCCESS; + do_encode (input_fh, infile, stdout, wrap_column); } -- 2.32.0