Skip to contentSkip to conversation

On grub_strchr implementation

PATCH BVK 2 participants 6 messages Nov 1, 2009 – Nov 2, 2009

lore
Share & tools
lore

BVK

author
#0 lore raw

hi,

As per http://www.opengroup.org/onlinepubs/009695399/functions/strchr.html
strchr method should consider '\0' as part of the string, which means, strchr should return '\0' character position when it is passed as the character to look for.

I believe, grub_strchr is intended to mimic standard strchr semantics, but its implementation doesn't confrom the above requirement. Same goes for grub_strrchr function too. Attached is the patch to fix both of these.

thanks,

Signature · 1 line
bvk-chaitanya

=== modified file 'ChangeLog'

ChangeLog +5 -0
@@ -1,3 +1,8 @@
+2009-11-02  BVK Chaitanya  <[email protected]>
+
+	* kern/misc.c: Fixed grub_strchr and grub_strrchr functions for
+	'\0' case.
+
 2009-10-30  Robert Millan  <[email protected]>
 
 	Fix build problem.

=== modified file 'kern/misc.c'

misc.c +10 -3
@@ -223,12 +223,16 @@
 char *
 grub_strchr (const char *s, int c)
 {
-  while (*s)
+  if (! s)
+    return 0;
+
+  do
     {
       if (*s == c)
 	return (char *) s;
       s++;
     }
+  while (*s);
 
   return 0;
 }
@@ -238,12 +242,15 @@
 {
   char *p = 0;
 
-  while (*s)
+  if (! s)
+    return 0;
+
+  do
     {
       if (*s == c)
 	p = (char *) s;
-      s++;
     }
+  while (*s++);
 
   return p;
 }
#1 lore raw
Quoted 8 lines · BVK

hi,

As per http://www.opengroup.org/onlinepubs/009695399/functions/strchr.html
strchr method should consider '\0' as part of the string, which means, strchr should return '\0' character position when it is passed as the character to look for.

Quoted 4 lines

I believe, grub_strchr is intended to mimic standard strchr semantics, but its implementation doesn't confrom the above requirement. Same goes for grub_strrchr function too. Attached is the patch to fix both of these.

Thanks.

But you also added a check for NULL dereference. Is that also required by the standard?

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
author
#2 lore raw
Quoted 4 lines · Robert Millan

But you also added a check for NULL dereference.  Is that also required by the standard?

Standard doesn't say anything about it, so I guess it means, it is left for implementations. Old grub_strchr implementation has this check, so kept it as it is. I didn't want to break any code if it is assuming this behavior.

I just saw that other functions like, grub_strcmp, etc. don't check for NULL dereference, so its better to remove this check. I will send a new patch.

Signature · 1 line
bvk-chaitanya
author
#3 lore raw

Attached is the new patch without NULL dereference checks.

I looked at grub_strchr and grub_strrchr calls as much as I can, and they don't seem to rely on NULL dereference behavior. But it was not feasible to verify this for some cases, as we are using function pointers (like, grub_disk_dev->open, etc.).

thanks,

Signature · 1 line
bvk-chaitanya

=== modified file 'ChangeLog'

ChangeLog +5 -0
@@ -1,3 +1,8 @@
+2009-11-02  BVK Chaitanya  <[email protected]>
+
+	* kern/misc.c: Fixed grub_strchr and grub_strrchr functions for
+	'\0' case.
+
 2009-10-30  Robert Millan  <[email protected]>
 
 	Fix build problem.

=== modified file 'kern/misc.c'

misc.c +4 -3
@@ -223,12 +223,13 @@
 char *
 grub_strchr (const char *s, int c)
 {
-  while (*s)
+  do
     {
       if (*s == c)
 	return (char *) s;
       s++;
     }
+  while (*s);
 
   return 0;
 }
@@ -238,12 +239,12 @@
 {
   char *p = 0;
 
-  while (*s)
+  do
     {
       if (*s == c)
 	p = (char *) s;
-      s++;
     }
+  while (*s++);
 
   return p;
 }
#4 lore raw
In reply to BVK
Quoted 6 lines · BVK
Quoted 4 lines · Robert Millan

But you also added a check for NULL dereference.  Is that also required by the standard?

Quoted 8 lines

Standard doesn't say anything about it, so I guess it means, it is left for implementations. Old grub_strchr implementation has this check, so kept it as it is. I didn't want to break any code if it is assuming this behavior.

I just saw that other functions like, grub_strcmp, etc. don't check for NULL dereference, so its better to remove this check. I will send a new patch.

Hi,

Please see the commit I did yesterday. Does this suffice?

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
author
#5 lore raw
Quoted 3 lines · Robert Millan

Please see the commit I did yesterday.  Does this suffice?

Yes, it is sufficient.

Signature · 1 line
bvk-chaitanya