[PATCH] pNFS: Fix use of uninitialized be_prev in ext_tree_encode_commit()
From: Yiqing Wu
Date: Mon Nov 16 2026 - 03:05:48 EST
Building with W=2 warns:
fs/nfs/blocklayout/extent_tree.c:615:59: warning: be_prev may be used uninitialized [-Wmaybe-uninitialized]
When the encoded layoutcommit overflows the buffer,
ext_tree_encode_commit() breaks out of the extent loop with -ENOSPC
and computes lastbyte from be_prev, the last extent that was
successfully encoded. be_prev is only assigned after an extent is
encoded, so if the first extent matching the filter already exceeds
the buffer, the loop breaks before be_prev is ever set and the error
path reads it uninitialized.
That path cannot be reached in practice: overflowing on the very
first extent would require a buffer smaller than the encoding of a
single extent (20 bytes for SCSI layouts, 48 bytes for block
layouts), but the callers always pass a full page or the server
wsize, which nfs_io_size() clamps to at least NFS_MIN_FILE_IO_SIZE.
GCC cannot prove this, and zero-initializing be_prev alone would
turn the theoretical read into a NULL dereference, so handle the
case explicitly and report U64_MAX, the same "no information" value
that is already used when bl_lwb is zero.
Signed-off-by: Yiqing Wu <wuyiqing@xxxxxxxxxxxxxxxxxxxx>
---
fs/nfs/blocklayout/extent_tree.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/fs/nfs/blocklayout/extent_tree.c b/fs/nfs/blocklayout/extent_tree.c
index 7393118c42a2..f78699317d8e 100644
--- a/fs/nfs/blocklayout/extent_tree.c
+++ b/fs/nfs/blocklayout/extent_tree.c
@@ -585,7 +585,7 @@ static int
ext_tree_encode_commit(struct pnfs_block_layout *bl, __be32 *p,
size_t buffer_size, size_t *count, __u64 *lastbyte)
{
- struct pnfs_block_extent *be, *be_prev;
+ struct pnfs_block_extent *be, *be_prev = NULL;
int ret = 0;
spin_lock(&bl->bl_ext_lock);
@@ -612,9 +612,14 @@ ext_tree_encode_commit(struct pnfs_block_layout *bl, __be32 *p,
*lastbyte = (bl->bl_lwb != 0) ? bl->bl_lwb - 1 : U64_MAX;
bl->bl_lwb = 0;
} else {
- *lastbyte = be_prev->be_f_offset + be_prev->be_length;
- *lastbyte <<= SECTOR_SHIFT;
- *lastbyte -= 1;
+ if (be_prev) {
+ *lastbyte = be_prev->be_f_offset + be_prev->be_length;
+ *lastbyte <<= SECTOR_SHIFT;
+ *lastbyte -= 1;
+ } else {
+ /* no extent fit in the buffer */
+ *lastbyte = U64_MAX;
+ }
}
spin_unlock(&bl->bl_ext_lock);
--
2.56.0