[PATCH] Always check lockf64 return value
Florian Weimer
fweimer@redhat.com
Mon Jun 16 06:33:49 GMT 2025
* H. J. Lu:
> diff --git a/locale/programs/locarchive.c b/locale/programs/locarchive.c
> index 2c19f4fd29..ad025a819b 100644
> --- a/locale/programs/locarchive.c
> +++ b/locale/programs/locarchive.c
> @@ -638,7 +638,8 @@ open_archive (struct locarhandle *ah, bool readonly)
> || st.st_dev != st2.st_dev
> || st.st_ino != st2.st_ino)
> {
> - (void) lockf64 (fd, F_ULOCK, sizeof (struct locarhead));
> + if (lockf64 (fd, F_ULOCK, sizeof (struct locarhead)) != 0)
> + error (EXIT_FAILURE, errno, _("cannot unlock archive header"));
> close (fd);
> continue;
> }
> @@ -650,14 +651,17 @@ open_archive (struct locarhandle *ah, bool readonly)
> /* Read the header. */
> if (TEMP_FAILURE_RETRY (read (fd, &head, sizeof (head))) != sizeof (head))
> {
> - (void) lockf64 (fd, F_ULOCK, sizeof (struct locarhead));
> - error (EXIT_FAILURE, errno, _("cannot read archive header"));
> + int errval = errno;
> + if (lockf64 (fd, F_ULOCK, sizeof (struct locarhead)) != 0)
> + error (EXIT_FAILURE, errno, _("cannot unlock archive header"));
> + error (EXIT_FAILURE, errval, _("cannot read archive header"));
> }
>
> /* Check the magic value */
> if (GET (head.magic) != AR_MAGIC)
> {
> - (void) lockf64 (fd, F_ULOCK, sizeof (struct locarhead));
> + if (lockf64 (fd, F_ULOCK, sizeof (struct locarhead)) != 0)
> + error (EXIT_FAILURE, errno, _("cannot unlock archive header"));
> error (EXIT_FAILURE, 0, _("bad magic value in archive header"));
> }
>
> @@ -676,8 +680,10 @@ open_archive (struct locarhandle *ah, bool readonly)
> MAP_SHARED | xflags, fd, 0);
> if (ah->addr == MAP_FAILED)
> {
> - (void) lockf64 (fd, F_ULOCK, sizeof (struct locarhead));
> - error (EXIT_FAILURE, errno, _("cannot map archive header"));
> + int errval = errno;
> + if (lockf64 (fd, F_ULOCK, sizeof (struct locarhead)) != 0)
> + error (EXIT_FAILURE, errno, _("cannot unlock archive header"));
> + error (EXIT_FAILURE, errval, _("cannot map archive header"));
> }
> ah->reserved = reserved;
> ah->mmap_base = mmap_base;
Given Sam's comment about unlock ranges, I think this is okay.
Reviewed-by: Florian Weimer <fweimer@redhat.com>
(We'll see soon enough if the new error triggers in practice because the
file pointer is unexpected at this point.)
Thanks,
Florian
More information about the Libc-alpha
mailing list