diff options
| author | Cyrille Bagard <nocbos@gmail.com> | 2014-07-30 05:43:44 (GMT) | 
|---|---|---|
| committer | Cyrille Bagard <nocbos@gmail.com> | 2014-07-30 05:43:44 (GMT) | 
| commit | dc436357ff29158dddd836d368d152d42d5b086b (patch) | |
| tree | 39744342708119ddc20e1642f87cc0d395c92b39 | |
| parent | ee9bbfc34d86bfcf9384ed93e4300f6464528b9b (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
| -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)); | 
