[xen staging] string: avoid undefined behavior in strrchr()

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[xen staging] string: avoid undefined behavior in strrchr()

patchbot
commit 592b5fcb984487f1929d28e1f4eb49d25c2c719d
Author:     Jan Beulich <[hidden email]>
AuthorDate: Wed Mar 13 11:23:28 2019 +0100
Commit:     Jan Beulich <[hidden email]>
CommitDate: Wed Mar 13 11:23:28 2019 +0100

    string: avoid undefined behavior in strrchr()
   
    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 <[hidden email]>
    Reviewed-by: Juergen Gross <[hidden email]>
    Acked-by: Andrew Cooper <[hidden email]>
---
 xen/common/string.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/xen/common/string.c b/xen/common/string.c
index 1e122abca8..b8639dd30d 100644
--- a/xen/common/string.c
+++ b/xen/common/string.c
@@ -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
 
--
generated by git-patchbot for /home/xen/git/xen.git#staging

_______________________________________________
Xen-changelog mailing list
[hidden email]
https://lists.xenproject.org/xen-changelog