diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | src/common/extstr.c | 10 |
2 files changed, 13 insertions, 2 deletions
@@ -1,3 +1,8 @@ +14-07-30 Cyrille Bagard <nocbos@gmail.com> + + * src/common/extstr.c: + Fix a bug: take into account that realloc() can move the haystack. + 14-07-28 Cyrille Bagard <nocbos@gmail.com> * src/glibext/Makefile.am: diff --git a/src/common/extstr.c b/src/common/extstr.c index 851401c..59d4389 100644 --- a/src/common/extstr.c +++ b/src/common/extstr.c @@ -174,16 +174,21 @@ char *strrpl(char *haystack, const char *needle1, const char *needle2) size_t inlen; /* Taille en entrée */ size_t len1; /* Taille de l'aiguille n°1 */ size_t len2; /* Taille de l'aiguille n°2 */ + size_t index; /* Conversion en indice */ char *found; /* Position d'une trouvaille */ inlen = strlen(haystack) + 1; len1 = strlen(needle1); len2 = strlen(needle2); - for (found = strstr(haystack, needle1); + index = 0; + + for (found = strstr(haystack + index, needle1); found != NULL; - found = strstr(haystack, needle1)) + found = strstr(haystack + index, needle1)) { + index = found - haystack; + if (len1 != len2) { if (len2 > len1) @@ -193,6 +198,7 @@ char *strrpl(char *haystack, const char *needle1, const char *needle2) inlen -= len1 - len2; haystack = (char *)realloc(haystack, inlen * sizeof(char *)); + found = haystack + index; if (len2 > len1) memmove(found + len2, found + len1, inlen - len1 - (found - haystack)); |