summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-09-25 13:10:02 -0400
committerJakob Kaivo <jkk@ung.org>2020-09-25 13:10:02 -0400
commitf45f928892e9d09f72dcc034bede48eb63014e35 (patch)
tree22cb9500dd47df087fda4ac1c698edd3e5b7f27b
parent288256939248c6c8f9160f20992b7d70720a989d (diff)
fix off-by-one error, remove extraneous optimization
-rw-r--r--src/string/strstr.c14
1 files changed, 4 insertions, 10 deletions
diff --git a/src/string/strstr.c b/src/string/strstr.c
index 3dd968da..d0f3551a 100644
--- a/src/string/strstr.c
+++ b/src/string/strstr.c
@@ -12,20 +12,14 @@ char * strstr(const char *s1, const char *s2)
ASSERT_NONNULL(s1);
ASSERT_NONNULL(s2);
- l2 = strlen(s2);
- if (l2 == 0) {
- return p;
- }
-
l1 = strlen(s1);
+ l2 = strlen(s2);
- do {
- p = memchr(p, *s2, l1);
- if (p == NULL || strcmp(p + 1, s2 + 1) == 0) {
+ for (p = (char*)s1; p < s1 + l1 - l2; p = strchr(p + 1, *s2)) {
+ if (p == NULL || strncmp(p, s2, l2) == 0) {
break;
}
- p++;
- } while (p < s1 + l2);
+ }
/*
RETURN_FAILURE(CONSTANT(NULL));