diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | src/common/extstr.c | 24 |
2 files changed, 19 insertions, 10 deletions
@@ -1,5 +1,10 @@ 16-12-15 Cyrille Bagard <nocbos@gmail.com> + * src/common/extstr.c: + Fix a bug when replacing strings. + +16-12-15 Cyrille Bagard <nocbos@gmail.com> + * src/arch/arm/v7/helpers.c: * src/arch/arm/v7/helpers.h: Remove dead code and a compilation warning. diff --git a/src/common/extstr.c b/src/common/extstr.c index 0b37606..2e69e3f 100644 --- a/src/common/extstr.c +++ b/src/common/extstr.c @@ -189,21 +189,25 @@ char *strrpl(char *haystack, const char *needle1, const char *needle2) { index = found - haystack; - if (len1 != len2) + if (len2 > len1) { - if (len2 > len1) - inlen += len2 - len1; - - else - inlen -= len1 - len2; + inlen += (len2 - len1); haystack = (char *)realloc(haystack, inlen * sizeof(char *)); found = haystack + index; - if (len2 > len1) - memmove(found + len2, found + len1, inlen - len1 - (found - haystack)); - else - memmove(found + len1, found + len2, inlen - len1 - (found - haystack)); + memmove(found + len1, found + len2, inlen - index + len2); + + } + + else if (len2 < len1) + { + memmove(found + len2, found + len1, inlen - index - len1); + + inlen -= (len1 - len2); + + haystack = (char *)realloc(haystack, inlen * sizeof(char *)); + found = haystack + index; } |