Skip to contentSkip to conversation

kernel/power/disk.c string fix and if-less iterator

PATCH In review Ricardo Nabinger Sanchez 1 participant 2 messages May 26, 2005

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/power/disk.c string fix and if-less iterator

Hello,

The attached patch:

o Fixes kernel/power/disk.c string declared as 'char *p = "...";' to be declared as 'char p[] = "...";', as pointed by Jeff Garzik.

o Replaces:

	i++:
	if (i > 3) i = 0;

By:

	i = (i + 1) % (sizeof(p) - 1);

Which is if-less, and the adjust value is evaluated by the compiler in compile-time in case the string related to this loop is modified.

---

 kernel/power/disk.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
Signature · 6 lines
Ricardo Nabinger Sanchez
GNU/Linux #140696 [http://counter.li.org]
Slackware Linux + FreeBSD

    How long a minute is depends on which 
    side of the bathroom door you're on.
kernel/power/disk.c +2 -4
diff -urNp -X dontdiff linux-2.6.11.10/kernel/power/disk.c mod-linux-2.6.11.10/kernel/power/disk.c
@@ -89,15 +89,13 @@ static void free_some_memory(void)
 	unsigned int i = 0;
 	unsigned int tmp;
 	unsigned long pages = 0;
-	char *p = "-\\|/";
+	char p[] = "-\\|/";
 
 	printk("Freeing memory...  ");
 	while ((tmp = shrink_all_memory(10000))) {
 		pages += tmp;
 		printk("\b%c", p[i]);
-		i++;
-		if (i > 3)
-			i = 0;
+		i = (i + 1) % (sizeof(p) - 1);
 	}
 	printk("\bdone (%li pages freed)\n", pages);
 }

_______________________________________________
Kernel-janitors mailing list
[email protected]
http://lists.osdl.org/mailman/listinfo/kernel-janitors

#1 lore raw

Quoting walter harms <[email protected]>
Sent on Thu, 26 May 2005 10:32:19 +0200

Hello Walter,

Quoted 4 lines

are you sure about

i = (i + 1) % (sizeof(p) - 1);
                           ^^^^

p is defined as:

	char p[] = "-\\|/";

and the (sizeof(p) - 1) is meant to disconsider the string terminator, as sizeof() returns 5, but the string has only 4 characters which will be printed by the loop in this code.

Quoted 3 lines

asume sizeof(p)=4 then sizeof(p)-1 = 3 and 3 % 3 = 0 therefor 3 is never reached.

That was my first thought, but as p is a string, there's the extra character for '\0'. And it shouldn't (nor wouldn't) be printed.

Quoted 2 lines

to simplify the whole stuff whats about

	printk("\b%c", p [ i++ % sizeof(p) ] );

instead:

	printk("\b%c", p [ i++ % (sizeof(p) - 1) ] );
Quoted 4 lines

ignoring the overflow for i is safe since the result is
a) unsigned
b) less then sizeof(p)

Agreed.

Should I recreate the patch and send again?

Regards.

Signature · 6 lines
Ricardo Nabinger Sanchez
GNU/Linux #140696 [http://counter.li.org]
Slackware Linux + FreeBSD

    How long a minute is depends on which 
    side of the bathroom door you're on.

_______________________________________________
Kernel-janitors mailing list
[email protected]
http://lists.osdl.org/mailman/listinfo/kernel-janitors