summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2014-07-30 05:43:44 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2014-07-30 05:43:44 (GMT)
commitdc436357ff29158dddd836d368d152d42d5b086b (patch)
tree39744342708119ddc20e1642f87cc0d395c92b39 /src
parentee9bbfc34d86bfcf9384ed93e4300f6464528b9b (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/common/extstr.c10
1 files changed, 8 insertions, 2 deletions
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));