diff --git a/src/chars.c b/src/chars.c
index d2088bd2c9b2517182068f57f0ff6c51d3ffb93b..8079317b57537bb1019210c2bf1a4d2465d90e5e 100644
--- a/src/chars.c
+++ b/src/chars.c
@@ -577,25 +577,25 @@ char *mbstrcasestr(const char *haystack, const char *needle)
 
 /* This function is equivalent to strstr(), except in that it scans the
  * string in reverse, starting at rev_start. */
-char *revstrstr(const char *haystack, const char *needle, const char
-	*rev_start)
+char *revstrstr(const char *haystack, const char *needle,
+	const char *pointer)
 {
-    size_t rev_start_len, needle_len;
+    size_t needle_len = strlen(needle);
+    size_t tail_len = strlen(pointer);
 
-    if (*needle == '\0')
-	return (char *)rev_start;
-
-    needle_len = strlen(needle);
+    if (needle_len == 0)
+	return (char *)pointer;
 
     if (strlen(haystack) < needle_len)
 	return NULL;
 
-    rev_start_len = strlen(rev_start);
+    if (tail_len < needle_len)
+	pointer += tail_len - needle_len;
 
-    for (; rev_start >= haystack; rev_start--, rev_start_len++) {
-	if (rev_start_len >= needle_len && strncmp(rev_start, needle,
-		needle_len) == 0)
-	    return (char *)rev_start;
+    while (pointer >= haystack) {
+	if (strncmp(pointer, needle, needle_len) == 0)
+	    return (char *)pointer;
+	pointer--;
     }
 
     return NULL;
diff --git a/src/proto.h b/src/proto.h
index cc9dd2086b8b5af58a2d924ba0085997fd84d154..c49868106330019415f06d41de19aaba91a4459f 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -227,8 +227,8 @@ int mbstrncasecmp(const char *s1, const char *s2, size_t n);
 char *nstrcasestr(const char *haystack, const char *needle);
 #endif
 char *mbstrcasestr(const char *haystack, const char *needle);
-char *revstrstr(const char *haystack, const char *needle, const char
-	*rev_start);
+char *revstrstr(const char *haystack, const char *needle,
+	const char *pointer);
 char *revstrcasestr(const char *haystack, const char *needle, const char
 	*rev_start);
 char *mbrevstrcasestr(const char *haystack, const char *needle, const