public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Leo Martins <loemra.dev@gmail.com>
To: fstests@vger.kernel.org, linux-btrfs@vger.kernel.org, kernel-team@fb.com
Cc: Mark Harmstone <maharmstone@fb.com>
Subject: [PATCH] btrfs: fix stale errno check in btrfs_encoded_read/write helpers
Date: Tue, 24 Mar 2026 12:09:52 -0700	[thread overview]
Message-ID: <39eac6cef5afd99734483fa0e775a3e75407552b.1774303837.git.loemra.dev@gmail.com> (raw)

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


             reply	other threads:[~2026-03-24 19:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-24 19:09 Leo Martins [this message]
2026-03-25 11:02 ` [PATCH] btrfs: fix stale errno check in btrfs_encoded_read/write helpers Mark Harmstone

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=39eac6cef5afd99734483fa0e775a3e75407552b.1774303837.git.loemra.dev@gmail.com \
    --to=loemra.dev@gmail.com \
    --cc=fstests@vger.kernel.org \
    --cc=kernel-team@fb.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=maharmstone@fb.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