Re: [CODE] [NEWBIE] Search and replace function

From: Greg Miller (gmiller@CLASSIC-GAMES.COM)
Date: 11/02/98


Andrew wrote:
> void search_replace(char *string, const char *find, const char *replace)
> {
>         char final[MAX_INPUT_LENGTH];

Are you sure final[] isn't being overflowed?

>         size_t start, end, i;
>
>         while (strstr(string, find) != NULL) {
>
>                 start = strstr(string, find) - string;
>                 end = start + strlen(find);
>
>                 for (i = 0; i < start; i++)
>                         final[i] = string[i];
>
>                 strcat(final, replace);
>
>                 for (i = end; i < strlen(string); i++)
>                         final[i] = string[i];

Here's your problem! string[strlen(string)] is the null char that
terminates the string--but your loop stops at string[strlen(string)-1],
so you have an unterminated string in final[]

>
>                 sprintf(string, final);

Here's another potential crasher. If final contains any "%" characters,
they will be interpreted as format specifiers.

>         }
>         return;
> }
--
http://www.classic-games.com/
If you commit perjury, you go to jail... Is the President of the United
States above the law? If so, what was the American Revolution about?
*** NEWBIES: Limit signatures to four lines! No HTML mail or posts! ***


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST