public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs: fix stale errno check in btrfs_encoded_read/write helpers
@ 2026-03-24 19:09 Leo Martins
  2026-03-25 11:02 ` Mark Harmstone
  0 siblings, 1 reply; 2+ messages in thread
From: Leo Martins @ 2026-03-24 19:09 UTC (permalink / raw)
  To: fstests, linux-btrfs, kernel-team; +Cc: Mark Harmstone

The atoll() error check `if (offset == 0 && errno != 0)` can falsely
trigger because errno is not cleared before the call. Library
initialization code (e.g. libcap calling prctl(PR_CAPBSET_READ) which
fails with EINVAL) can leave errno set to a non-zero value. When the
caller passes offset=0 (a valid value), atoll() returns 0 without
modifying errno, and the stale errno causes the helper to print usage
and exit.

This caused btrfs/333 to fail consistently on systems where libcap is
linked, since every call with offset 0 would bail out.

Fix by clearing errno before the atoll() calls in both
btrfs_encoded_read and btrfs_encoded_write helpers.

Signed-off-by: Leo Martins <loemra.dev@gmail.com>
---
 src/btrfs_encoded_read.c  | 1 +
 src/btrfs_encoded_write.c | 1 +
 2 files changed, 2 insertions(+)

diff --git a/src/btrfs_encoded_read.c b/src/btrfs_encoded_read.c
index 3ee0d8b0..c7c25b74 100644
--- a/src/btrfs_encoded_read.c
+++ b/src/btrfs_encoded_read.c
@@ -178,6 +178,7 @@ int main(int argc, char *argv[])
 
 	filename = argv[2];
 
+	errno = 0;
 	offset = atoll(argv[3]);
 	if (offset == 0 && errno != 0) {
 		usage();
diff --git a/src/btrfs_encoded_write.c b/src/btrfs_encoded_write.c
index 7e46d9fe..bc05cc3b 100644
--- a/src/btrfs_encoded_write.c
+++ b/src/btrfs_encoded_write.c
@@ -179,6 +179,7 @@ int main(int argc, char *argv[])
 
 	filename = argv[2];
 
+	errno = 0;
 	offset = atoll(argv[3]);
 	if (offset == 0 && errno != 0) {
 		usage();
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] btrfs: fix stale errno check in btrfs_encoded_read/write helpers
  2026-03-24 19:09 [PATCH] btrfs: fix stale errno check in btrfs_encoded_read/write helpers Leo Martins
@ 2026-03-25 11:02 ` Mark Harmstone
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Harmstone @ 2026-03-25 11:02 UTC (permalink / raw)
  To: Leo Martins, fstests, linux-btrfs, kernel-team

On 24/03/2026 7.09 pm, Leo Martins wrote:
> The atoll() error check `if (offset == 0 && errno != 0)` can falsely
> trigger because errno is not cleared before the call. Library
> initialization code (e.g. libcap calling prctl(PR_CAPBSET_READ) which
> fails with EINVAL) can leave errno set to a non-zero value. When the
> caller passes offset=0 (a valid value), atoll() returns 0 without
> modifying errno, and the stale errno causes the helper to print usage
> and exit.

Thanks Leo. From 
https://pubs.opengroup.org/onlinepubs/9699919799/functions/errno.html:

 > The following sentence is deleted from the DESCRIPTION: "The value of 
errno is 0 at program start-up, but is never set to 0 by any XSI function".

So yes, apparently you can no longer rely on errno being 0 at the 
beginning of main. TIL.

Reviewed-by: Mark Harmstone <mark@harmstone.com>

> This caused btrfs/333 to fail consistently on systems where libcap is
> linked, since every call with offset 0 would bail out.
> 
> Fix by clearing errno before the atoll() calls in both
> btrfs_encoded_read and btrfs_encoded_write helpers.
> 
> Signed-off-by: Leo Martins <loemra.dev@gmail.com>
> ---
>   src/btrfs_encoded_read.c  | 1 +
>   src/btrfs_encoded_write.c | 1 +
>   2 files changed, 2 insertions(+)
> 
> diff --git a/src/btrfs_encoded_read.c b/src/btrfs_encoded_read.c
> index 3ee0d8b0..c7c25b74 100644
> --- a/src/btrfs_encoded_read.c
> +++ b/src/btrfs_encoded_read.c
> @@ -178,6 +178,7 @@ int main(int argc, char *argv[])
>   
>   	filename = argv[2];
>   
> +	errno = 0;
>   	offset = atoll(argv[3]);
>   	if (offset == 0 && errno != 0) {
>   		usage();
> diff --git a/src/btrfs_encoded_write.c b/src/btrfs_encoded_write.c
> index 7e46d9fe..bc05cc3b 100644
> --- a/src/btrfs_encoded_write.c
> +++ b/src/btrfs_encoded_write.c
> @@ -179,6 +179,7 @@ int main(int argc, char *argv[])
>   
>   	filename = argv[2];
>   
> +	errno = 0;
>   	offset = atoll(argv[3]);
>   	if (offset == 0 && errno != 0) {
>   		usage();


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-03-25 11:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-24 19:09 [PATCH] btrfs: fix stale errno check in btrfs_encoded_read/write helpers Leo Martins
2026-03-25 11:02 ` Mark Harmstone

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox