From dc436357ff29158dddd836d368d152d42d5b086b Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Wed, 30 Jul 2014 05:43:44 +0000 Subject: Fixed a bug: take into account that realloc() can move the haystack. git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@384 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a --- ChangeLog | 5 +++++ src/common/extstr.c | 10 ++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index e604fd4..7c90163 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +14-07-30 Cyrille Bagard + + * src/common/extstr.c: + Fix a bug: take into account that realloc() can move the haystack. + 14-07-28 Cyrille Bagard * 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)); -- cgit v0.11.2-87-g4458