From: Mark Wielaard <mark@klomp.org>
To: tbaeder@redhat.com, elfutils-devel@sourceware.org
Subject: Re: [PATCH 4/4] elfcompress: Replace cleanup() with label
Date: Mon, 01 Mar 2021 23:10:00 +0100 [thread overview]
Message-ID: <35619e7e48ec2d3d1a88c83303d42e1f739bd551.camel@klomp.org> (raw)
In-Reply-To: <20210217084510.135286-4-tbaeder@redhat.com>
Hi Timm,
On Wed, 2021-02-17 at 09:45 +0100, Timm Bäder via Elfutils-devel wrote:
> From: Timm Bäder <tbaeder@redhat.com>
>
> Get rid of a nested function this way.
It would be nice to get a ChangeLog entry with this.
> diff --git a/src/elfcompress.c b/src/elfcompress.c
> index 65e28f0e..ba08e73f 100644
> --- a/src/elfcompress.c
> +++ b/src/elfcompress.c
> @@ -298,47 +298,13 @@ process_file (const char *fname)
>
> /* How many sections are we talking about? */
> size_t shnum = 0;
> -
> - int cleanup (int res)
> - {
> - elf_end (elf);
> - close (fd);
> -
> - elf_end (elfnew);
> - close (fdnew);
> -
> - if (fnew != NULL)
> - {
> - unlink (fnew);
> - free (fnew);
> - fnew = NULL;
> - }
> -
> - free (snamebuf);
> - if (names != NULL)
> - {
> - dwelf_strtab_free (names);
> - free (scnstrents);
> - free (symstrents);
> - free (namesbuf);
> - if (scnnames != NULL)
> - {
> - for (size_t n = 0; n < shnum; n++)
> - free (scnnames[n]);
> - free (scnnames);
> - }
> - }
> -
> - free (sections);
> -
> - return res;
> - }
> + int res = 0;
If you are going to use a goto to the cleanup it makes sense to
initialize res to -1 here. Since till you get to the end, the result
will always be -1. And then you only need one (cleanup) label at the
end.
> @@ -1294,14 +1260,48 @@ process_file (const char *fname)
> if (rename (fnew, fname) != 0)
> {
> error (0, errno, "Couldn't rename %s to %s", fnew, fname);
> - return cleanup (-1);
> + goto error;
> }
>
> /* We are finally done with the new file, don't unlink it now. */
> free (fnew);
> fnew = NULL;
> + goto cleanup;
> +
> +error:
> + res = -1;
And then here instead of the goto cleanup and having an error label
simply set res = 0 and fall through to the cleanup label.
> +cleanup:
> + elf_end (elf);
> + close (fd);
>
> - return cleanup (0);
> + elf_end (elfnew);
> + close (fdnew);
> +
> + if (fnew != NULL)
> + {
> + unlink (fnew);
> + free (fnew);
> + fnew = NULL;
> + }
> +
> + free (snamebuf);
> + if (names != NULL)
> + {
> + dwelf_strtab_free (names);
> + free (scnstrents);
> + free (symstrents);
> + free (namesbuf);
> + if (scnnames != NULL)
> + {
> + for (size_t n = 0; n < shnum; n++)
> + free (scnnames[n]);
> + free (scnnames);
> + }
> + }
> +
> + free (sections);
> + return res;
> }
Cheers,
Mark
next prev parent reply other threads:[~2021-03-01 22:10 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-17 8:45 [PATCH 1/4] elfcompress: Pull set_section() into file scope tbaeder
2021-02-17 8:45 ` [PATCH 2/4] elfcompress: Pull get_section() " tbaeder
2021-03-01 22:03 ` Mark Wielaard
2021-02-17 8:45 ` [PATCH 3/4] elfcompress: Pull get_sections() " tbaeder
2021-03-01 22:04 ` Mark Wielaard
2021-02-17 8:45 ` [PATCH 4/4] elfcompress: Replace cleanup() with label tbaeder
2021-03-01 22:10 ` Mark Wielaard [this message]
2021-03-01 22:03 ` [PATCH 1/4] elfcompress: Pull set_section() into file scope Mark Wielaard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=35619e7e48ec2d3d1a88c83303d42e1f739bd551.camel@klomp.org \
--to=mark@klomp.org \
--cc=elfutils-devel@sourceware.org \
--cc=tbaeder@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).