]> xenbits.xensource.com Git - people/liuw/xen.git/commitdiff
string: avoid undefined behavior in strrchr()
authorJan Beulich <jbeulich@suse.com>
Wed, 13 Mar 2019 10:23:28 +0000 (11:23 +0100)
committerJan Beulich <jbeulich@suse.com>
Wed, 13 Mar 2019 10:23:28 +0000 (11:23 +0100)
The pre-decrement would not only cause misbehavior when wrapping (benign
because there shouldn't be any NULL pointers passed in), but may also
create a pointer pointing outside the object that the passed in pointer
points to (it won't be de-referenced though).

Take the opportunity and also
- convert bogus space (partly 7 of them) indentation to Linux style tab
  one,
- add two blank lines.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>
xen/common/string.c

index 1e122abca8609176644fbc6703d4a5812c2f4453..b8639dd30db780c5391987ba60d4b87eb6959934 100644 (file)
@@ -174,12 +174,13 @@ char *(strchr)(const char *s, int c)
  */
 char *(strrchr)(const char *s, int c)
 {
-       const char *p = s + strlen(s);
-       do {
-           if (*p == (char)c)
-               return (char *)p;
-       } while (--p >= s);
-       return NULL;
+       const char *p = s + strlen(s);
+
+       for (; *p != (char)c; --p)
+               if (p == s)
+                       return NULL;
+
+       return (char *)p;
 }
 #endif