]> granicus.if.org Git - php/commitdiff
Bail out earlier in zend_memnrstr
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 20 Jun 2019 10:18:54 +0000 (12:18 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 20 Jun 2019 14:21:53 +0000 (16:21 +0200)
To avoid decrementing a null pointer lateron. As we need to check
for NULL here anayway, we should take the chance to bail out right
away.

Zend/zend_operators.h

index b126e96611b71d3c31553cd5792b830d5aa943b3..c6a55c2b6dd2a5529024670dcdc569cb0f6b2676 100644 (file)
@@ -225,10 +225,12 @@ zend_memnrstr(const char *haystack, const char *needle, size_t needle_len, const
                p -= needle_len;
 
                do {
-                       if ((p = (const char *)zend_memrchr(haystack, *needle, (p - haystack) + 1)) && ne == p[needle_len-1]) {
-                               if (!memcmp(needle + 1, p + 1, needle_len - 2)) {
-                                       return p;
-                               }
+                       p = (const char *)zend_memrchr(haystack, *needle, (p - haystack) + 1);
+                       if (!p) {
+                               return NULL;
+                       }
+                       if (ne == p[needle_len-1] && !memcmp(needle + 1, p + 1, needle_len - 2)) {
+                               return p;
                        }
                } while (p-- >= haystack);