Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
char string[]*path = "Hello secure coding wiki!";getenv("PATH"); 
/* PATH is something like "/usr/bin:/bin:/usr/sbin:/sbin" */
char *token; 
 
token = strtok(stringpath, ' '":"); 
puts(token); 
 
while (token = strtok(NULL0, ' '":")) { 
  puts(token); 
} 
 
printf("PATH: %s\n", path); 
/* stringPATH is hasnow been modifiedjust "/usr/bin" */

Wiki Markup
In this example, the {{strtok()}} function is used to parse the first argument into spacecolon-delimited tokens; it will output each word from the string on a new line. However, after the while loop ends, stringpath\[\] will have been modified to look like this: {{"Hello\0secure\0coding\0wiki/usr/bin\0/bin\0/usr/sbin\0/sbin\0"}}. This is an issue on several levels. If Anywe furthercheck manipulationour oflocal {{stringpath}} operating on the assumption that it is still whole will see only "Hello" instead of the expected string valuevariable, we will only see {{/usr/bin}} now. Even worse, we have changed the environment variable PATH, which could cause more confusion (see ENV30-C).

Compliant Solutions

One possible solution is to copy the string being tokenized into a temporary buffer which isn't referenced after the calls to strtok():

...