summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2018-04-24 08:03:23 +0200
committerDaniel Stenberg <daniel@haxx.se>2018-04-24 08:03:23 +0200
commita7df35ce2130a99f26c4c8315ce46bf10804280e (patch)
tree9eaff253ce6ef2a65dd25a5b8ad4a632fc90ac30
parentabbc8457d85aca74b7cfda1d394b0844932b2934 (diff)
downloadcurl-a7df35ce2130a99f26c4c8315ce46bf10804280e.tar.gz
Curl_memchr: zero length input can't match
Avoids undefined behavior. Reported-by: Geeknik Labs
-rw-r--r--lib/curl_memrchr.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/lib/curl_memrchr.c b/lib/curl_memrchr.c
index c521497b2..eeb3044a9 100644
--- a/lib/curl_memrchr.c
+++ b/lib/curl_memrchr.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -44,17 +44,18 @@
void *
Curl_memrchr(const void *s, int c, size_t n)
{
- const unsigned char *p = s;
- const unsigned char *q = s;
+ if(n > 0) {
+ const unsigned char *p = s;
+ const unsigned char *q = s;
- p += n - 1;
+ p += n - 1;
- while(p >= q) {
- if(*p == (unsigned char)c)
- return (void *)p;
- p--;
+ while(p >= q) {
+ if(*p == (unsigned char)c)
+ return (void *)p;
+ p--;
+ }
}
-
return NULL;
}