Skip to contentSkip to conversation

Btrfs: fix use of uninitialized err variable

PATCH In review Filipe David Borba Manana 2 participants 5 messages Dec 13, 2013 – Dec 17, 2013 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: fix use of uninitialized err variable

Quoted 1 line

From the compiler:

fs/btrfs/file.c: In function ‘prepare_pages.isra.18’:
fs/btrfs/file.c:1265:6: warning: ‘err’ may be used uninitialized in this function [-Wuninitialized]

---
 fs/btrfs/file.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/file.c +1 -1
index 740ae8c..35bf838 100644
@@ -1244,7 +1244,7 @@ static noinline int prepare_pages(struct inode *inode, struct page **pages,
 	int i;
 	unsigned long index = pos >> PAGE_CACHE_SHIFT;
 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
-	int err;
+	int err = 0;
 	int faili;
 
 	for (i = 0; i < num_pages; i++) {
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 4 lines · Filipe David Borba Manana
Quoted 1 line

From the compiler:

fs/btrfs/file.c: In function ‘prepare_pages.isra.18’:
fs/btrfs/file.c:1265:6: warning: ‘err’ may be used uninitialized in this function [-Wuninitialized]

My gcc 4.8.1 does not see this warning, nor do I while inspecting the souces in current next-master.

#2 lore raw
Quoted 8 lines · David Sterba
Quoted 4 lines · Filipe David Borba Manana
Quoted 1 line

From the compiler:

fs/btrfs/file.c: In function ‘prepare_pages.isra.18’:
fs/btrfs/file.c:1265:6: warning: ‘err’ may be used uninitialized in this function [-Wuninitialized]

My gcc 4.8.1 does not see this warning, nor do I while inspecting the souces in current next-master.

Here it's gcc 4.6.3.

Signature · 5 lines
Filipe David Manana,

"Reasonable men adapt themselves to the world.
 Unreasonable men adapt the world to themselves.
 That's why all progress depends on unreasonable men."
#3 lore raw
Quoted 11 lines · Filipe David Manana
Quoted 8 lines · David Sterba
Quoted 4 lines · Filipe David Borba Manana
Quoted 1 line

From the compiler:

fs/btrfs/file.c: In function ‘prepare_pages.isra.18’:
fs/btrfs/file.c:1265:6: warning: ‘err’ may be used uninitialized in this function [-Wuninitialized]

My gcc 4.8.1 does not see this warning, nor do I while inspecting the souces in current next-master.

Here it's gcc 4.6.3.

I've seen that some versions of gcc produce bogus warnings of that sort and manual review is needed, but I haven't found a code path that would lead to uninitialized use of err.

The warning points to

1259 if (i == 0)
1260 err = prepare_uptodate_page(pages[i], pos,
1261 force_uptodate);
1262 if (i == num_pages - 1)
1263 err = prepare_uptodate_page(pages[i],
1264 pos + write_bytes, false);
1265 if (err) {
^^^^

1266                         page_cache_release(pages[i]);
1267                         faili = i - 1;
1268                         goto fail;

1269 }

But the loop starts from i = 0 and the variable is initialized before the check. So ti's gcc that does not see that, not a real error.

#4 lore raw
Quoted 28 lines · David Sterba
Quoted 11 lines · Filipe David Manana
Quoted 8 lines · David Sterba
Quoted 4 lines · Filipe David Borba Manana
Quoted 1 line

From the compiler:

fs/btrfs/file.c: In function ‘prepare_pages.isra.18’:
fs/btrfs/file.c:1265:6: warning: ‘err’ may be used uninitialized in this function [-Wuninitialized]

My gcc 4.8.1 does not see this warning, nor do I while inspecting the souces in current next-master.

Here it's gcc 4.6.3.

I've seen that some versions of gcc produce bogus warnings of that sort and manual review is needed, but I haven't found a code path that would lead to uninitialized use of err.

The warning points to

1259 if (i == 0)
1260 err = prepare_uptodate_page(pages[i], pos,
1261 force_uptodate);
1262 if (i == num_pages - 1)
1263 err = prepare_uptodate_page(pages[i],
1264 pos + write_bytes, false);
1265 if (err) {
^^^^

Quoted 7 lines
1266                         page_cache_release(pages[i]);
1267                         faili = i - 1;
1268                         goto fail;

1269 }

But the loop starts from i = 0 and the variable is initialized before the check. So ti's gcc that does not see that, not a real error.

Right, my intention was to silence a compiler warning. Should have made it more explicit in the commit message title.

Signature · 5 lines
Filipe David Manana,

"Reasonable men adapt themselves to the world.
 Unreasonable men adapt the world to themselves.
 That's why all progress depends on unreasonable men."