diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-09-25 13:10:02 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-09-25 13:10:02 -0400 |
commit | f45f928892e9d09f72dcc034bede48eb63014e35 (patch) | |
tree | 22cb9500dd47df087fda4ac1c698edd3e5b7f27b | |
parent | 288256939248c6c8f9160f20992b7d70720a989d (diff) |
fix off-by-one error, remove extraneous optimization
-rw-r--r-- | src/string/strstr.c | 14 |
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)); |