All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Li Wang <liwang@redhat.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH] lib: tst_detach_device_by_fd() set dev_fd to -1
Date: Tue, 14 Oct 2025 17:30:05 +0200	[thread overview]
Message-ID: <aO5sfcv7R9-LOK0x@yuki.lan> (raw)
In-Reply-To: <CAEemH2dHYpXk+htRD1GkCjj8FJMXctrU2Y0iZpA6zb0Y2iCvJA@mail.gmail.com>

Hi!
> > Apparently I tend to forget that tst_detach_device_by_fd() closes the
> > file descriptor. This change makes it more obvious by chaning the fd to
> > a pointer and also sets the dev_fd to -1, which matches the SAFE_CLOSE()
> > behavior.
> >
> 
> It took me a while to think why we need to do this since we already had:
> https://github.com/linux-test-project/ltp/commit/c02d8ddc5f9d312ca5c384317141e213412a5c42
> 
> However, this patch makes sense because:
> 
> After the tst_detach_device_by_fd() call, dev_fd is silently closed,
> but the variable in the caller still retains its old value. If the caller
> attempts to use it again, getting use-after-close may cause problems.
> Therefore, reset it to -1 to prevent accidental use of a closed fd.

Yes it's about making the API more obvious and less dangerous.

> So maybe we could explicitly add the above explanation in the description
> when merging?

I've changed the function description to:

/*
 * Detaches a file from a loop device by a fd.
 *
 * The dev_fd needs to be the last file descriptor opened for the
 * device. Call to this function will close dev_fd and set it to -1 in
 * order to avoid incorrect usage after it's closed.
 *
 * @dev_path Path to the loop device e.g. /dev/loop0
 * @dev_fd An open fd for the loop device, set to -1 after the completion.
 * @return Zero on succes, non-zero otherwise.
 */

I hope that it makes it clearer.

> Anyway:
> Reviewed-by: Li Wang <liwang@redhat.com>

Pushed, thanks for the review.

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

      reply	other threads:[~2025-10-14 15:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-07 11:27 [LTP] [PATCH] lib: tst_detach_device_by_fd() set dev_fd to -1 Cyril Hrubis
2025-10-07 13:49 ` Petr Vorel
2025-10-07 13:59 ` Andrea Cervesato via ltp
2025-10-14  8:57 ` Li Wang via ltp
2025-10-14 15:30   ` Cyril Hrubis [this message]

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=aO5sfcv7R9-LOK0x@yuki.lan \
    --to=chrubis@suse.cz \
    --cc=liwang@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.