Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does strcat affect the strtok?

Assume we need to copy user’s input into another string by concatenating the tokens of input, e.g., "hello world" -> "helloworld".

#include <stdio.h>
#include <string.h>

int main(void) {
  char buffer[50];

  printf("\nEnter a string: ");

  while (fgets(buffer, sizeof(buffer), stdin) != 0) {
    size_t size = strlen(buffer);

    if (size > 0 && buffer[size - 1] == '\n') {
      char input[1]; // set it too small 
      buffer[size - 1] = '\0';

      char *tok = strtok(buffer, " "); // works fine
      do {
        strcat(input, tok); // append to "input" that has not enough space
        printf("\nfound token: %s", tok);
        tok = strtok(NULL, " "); // produces garbage
      } while (tok);

     break;
  }
}

Running the code above:

Enter a string: hello world

found token: hello
found token: w
found token: r
*** stack smashing detected ***: <unknown> terminated

I struggle to understand how is strtok related to strcat failing to append tok. They are not sharing variables except for tok which is (according to the docs) copied by strcat, so whatever strcat is doing shouldn’t affect the strtok behavior and the program should crash on the second strcat call at least, right? But we see that strcat is getting called 3 times before stack smashing gets detected. Can you please explain why?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For starters this array

char input[1];

is not initialized and does not contain a string.

So this call of strcat

strcat(input, tok);

invokes undefined behavior also because the array input is not large enough to store the copied string. It can overwrite memory beyond the array.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading