Skip to contentSkip to conversation

mm: Don't touch uninitialized variable in do_pages_stat_array()

DISCUSSION Joe Perches 2 participants 2 messages Dec 16, 2008 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 36 lines · Linux Kernel Mailing List
Gitweb:     http://git.kernel.org/git/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=c095adbc211f9f4e990eac7d6cb440de35e4f05f
Commit:     c095adbc211f9f4e990eac7d6cb440de35e4f05f
Parent:     a3dd15444baa9c7522c8457ab564c41219dfb44c
Author:     KOSAKI Motohiro <[email protected]>
AuthorDate: Tue Dec 16 16:06:43 2008 +0900
Committer:  Linus Torvalds <[email protected]>
CommitDate: Tue Dec 16 08:19:23 2008 -0800

mm: Don't touch uninitialized variable in do_pages_stat_array()

Commit 80bba1290ab5122c60cdb73332b26d288dc8aedd removed one necessary variable initialization. As a result following warning happened:

CC mm/migrate.o
mm/migrate.c: In function 'sys_move_pages':
mm/migrate.c:1001: warning: 'err' may be used uninitialized in this function

More unfortunately, if find_vma() failed, kernel read uninitialized memory.

CC Brice Goglin <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Nick Piggin <[email protected]>
Cc Hugh Dickins <[email protected]>
---
 mm/migrate.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
mm/migrate.c +0 -0
index d8f0766..037b096 100644

mm/migrate.c static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,

Quoted 9 lines
mm/migrate.c +1 -2
@@ -998,7 +998,7 @@ static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
 		unsigned long addr = (unsigned long)(*pages);
 		struct vm_area_struct *vma;
 		struct page *page;
-		int err;
+		int err = -EFAULT;
 
 		vma = find_vma(mm, addr);
 		if (!vma)
--

Perhaps this is more legible and avoids
the unnecessary assignments to err.

mm/migrate.c +10 -7
index 037b096..ddfeeac 100644
@@ -998,22 +998,25 @@ static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
 		unsigned long addr = (unsigned long)(*pages);
 		struct vm_area_struct *vma;
 		struct page *page;
-		int err = -EFAULT;
+		int err;
 
 		vma = find_vma(mm, addr);
-		if (!vma)
+		if (!vma) {
+			err = -EFAULT;
 			goto set_status;
+		}
 
 		page = follow_page(vma, addr, 0);
-
-		err = PTR_ERR(page);
-		if (IS_ERR(page))
+		if (IS_ERR(page)) {
+			err = PTR_ERR(page);
 			goto set_status;
+		}
 
-		err = -ENOENT;
 		/* Use PageReserved to check for zero page */
-		if (!page || PageReserved(page))
+		if (!page || PageReserved(page)) {
+			err = -ENOENT;
 			goto set_status;
+		}
 
 		err = page_to_nid(page);
 set_status:
#2 lore raw
Quoted 3 lines · Joe Perches

Perhaps this is more legible and avoids
the unnecessary assignments to err.

Not only is it less legible to me, but the "unnecessary" assignments are the ones that make the flow control much simpler.

I think it's a _lot_ easier to read

	retval = BADNESS;
	if (something bad)
		goto out;

than it is to make the insides of the conditional more complex. It also tends to generate better code, although gcc's code movement often makes it not matter (and equally often makes a mess of it). That's because a

	if (something)
		goto somewhere;

is actually how the CPU itself ends up executing things, while a

	if (something) {
		retval = something;
		goto somewhere;
	}

actually ends up becoming

	if (!someting)
		goto over;
	retval = something;
	goto somewhere;

over:

by the time the CPU actually has to execute it.

Linus