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 <jbeulich@suse.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>
This commit is contained in:
Jan Beulich committed 2019-03-13 11:23:28 +01:00
1 parent fa6eba8d35
commit 592b5fcb98
1 file changed
+7 -6
+7 -6
View 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
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.