From: Cyril Hrubis <chrubis@suse.cz>
To: Bogdan Lezhepekov <bogdan.lezhepekov@suse.com>
Cc: "ltp@lists.linux.it" <ltp@lists.linux.it>
Subject: Re: [LTP] [PATCH v1] lib/safe_file_ops.c: Fix resource leak
Date: Tue, 15 Feb 2022 13:59:40 +0100 [thread overview]
Message-ID: <YgujvFcL0TIwoE5f@yuki> (raw)
In-Reply-To: <VI1PR04MB49583062AFA205712ABA54B793309@VI1PR04MB4958.eurprd04.prod.outlook.com>
Hi!
> safe_file_scanf and safe_file_vprintf suffered
> from resource leak, as opened file descriptor
> was not closed in case of error.
>
> Signed-off-by: Bogdan Lezhepekov <bogdan.lezhepekov@suse.com>
> ---
> lib/safe_file_ops.c | 14 +++++++++-----
> 1 file changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/lib/safe_file_ops.c b/lib/safe_file_ops.c
> index f803691d8..d7231df4d 100644
> --- a/lib/safe_file_ops.c
> +++ b/lib/safe_file_ops.c
> @@ -130,7 +130,7 @@ void safe_file_scanf(const char *file, const int lineno,
> if (f == NULL) {
> tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
> "Failed to open FILE '%s' for reading", path);
> - return;
> + goto out;
This one is wrong, we just tested that f == NULL
> }
>
> exp_convs = tst_count_scanf_conversions(fmt);
> @@ -142,14 +142,14 @@ void safe_file_scanf(const char *file, const int lineno,
> if (ret == EOF) {
> tst_brkm_(file, lineno, TBROK, cleanup_fn,
> "The FILE '%s' ended prematurely", path);
> - return;
Technically this tst_brkm_() call does not return unless we are in the
test cleanup. During the rest of the test execution it ends up calling
exit(TBROK). So this return is reached only if something fails during
the test cleanup. I guess that we can fix it like this, but the test
will exit shortly after anyways.
> + goto out;
> }
>
> if (ret != exp_convs) {
> tst_brkm_(file, lineno, TBROK, cleanup_fn,
> "Expected %i conversions got %i FILE '%s'",
> exp_convs, ret, path);
> - return;
> + goto out;
> }
>
> if (fclose(f)) {
> @@ -157,6 +157,8 @@ void safe_file_scanf(const char *file, const int lineno,
> "Failed to close FILE '%s'", path);
> return;
> }
> +out:
> + fclose(f);
> }
>
>
> @@ -261,13 +263,13 @@ static void safe_file_vprintf(const char *file, const int lineno,
> if (f == NULL) {
> tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
> "Failed to open FILE '%s' for writing", path);
> - return;
> + goto out;
And here as well.
> }
>
> if (vfprintf(f, fmt, va) < 0) {
> tst_brkm_(file, lineno, TBROK, cleanup_fn,
> "Failed to print to FILE '%s'", path);
> - return;
> + goto out;
> }
>
> if (fclose(f)) {
> @@ -275,6 +277,8 @@ static void safe_file_vprintf(const char *file, const int lineno,
> "Failed to close FILE '%s'", path);
> return;
> }
> +out:
> + fclose(f);
> }
>
> void safe_file_printf(const char *file, const int lineno,
> --
> 2.35.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2022-02-15 12:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-11 14:20 [LTP] [PATCH v1] lib/safe_file_ops.c: Fix resource leak Bogdan Lezhepekov via ltp
2022-02-15 12:59 ` Cyril Hrubis [this message]
-- strict thread matches above, loose matches on Subject: below --
2022-02-11 16:24 Bogdan Lezhepekov
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=YgujvFcL0TIwoE5f@yuki \
--to=chrubis@suse.cz \
--cc=bogdan.lezhepekov@suse.com \
--cc=ltp@lists.linux.it \
/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