0

When I run this c snippet, it outputs something really random every time, and then segfaults... Code:

#include <stdio.h>
#include <stdlib.h>

int parse(void) {
  int i = 0;
  int system(const char *command);
  char line[1024];
  scanf("%[^\n]", line);
  system(line);
  do {
    line[i] = "\0";
    i++;
  } while (i != 1024);
  parse();
}

int main(void) {
  parse();
  return 0;
}

What I expected was a prompt, and when any shell command is entered (I used pwd for my testing), the output of the command prints and the prompt returns. And this is what actually happened:

Output:

> pwd
/home/runner/c-test
sh: 1: �: not found
sh: 1: : not found
sh: 1: ׀: not found
signal: segmentation fault (core dumped)
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621

2 Answers2

1
  1. Print prompt
  2. Do not use scanf
  3. Use static storage duration buffer
#include <stdio.h>
#include <stdlib.h>


int parse(void) 
{
  static char line[1024];
  while(1)
  {
      printf(">>>");
      if(!fgets(line, 1024, stdin)) return 1;
      system(line);
  }
}

int main(void) {
  parse();
}

https://www.onlinegdb.com/wmPB3ZGNQ

enter image description here

0___________
  • 60,014
  • 4
  • 34
  • 74
0

The reason for your crash is most likely explained by the following:

scanf("%[^\n]", line);

means keep reading until there is a newline in the input stream. So once it completes the next character in the input stream is a newline.

The next time you do

scanf("%[^\n]", line);

that newline is still the first character, so the scanf will return without waiting for the user to type any input.

Since you are doing recursion this will happen again and again and again.... Then most likely the system crashes due to stack overflow after a while.

Solution: Read that newline from the input stream before calling scanf again.

Besides that you should:

  • Remove int system(const char *command);

  • Put a maximum field width on scanf

  • Check the return value of scanf

  • Change "\0" to '\0' (or delete the whole do-while loop as you don't need it)

  • Use a while(1) { ... } loop instead of recursive calls

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Solution: do not use `scanf` with an unbounded buffer like that. – Dúthomhas Nov 03 '22 at 13:14
  • 1
    @Dúthomhas The real solution would be to delete all the code and do it completely different. But the point of this answer is to explain why OPs code behaves the way it does. I do not intend to rewrite OPs code. We are not a free coding service – Support Ukraine Nov 03 '22 at 13:18