I'm writing a program that will have a command prompt where the user can infinitely input command strings and I will process them as needed.
I have a command-line limit of 200 characters, but for now, I am performing a hello world test with a limit of 4 characters per command to see how my system would handle an input overflow. To my absolute surprise and confusion, I'm seeing that even though I am declaring my command[5] input array as to only allocate 5 characters, I am able to write outside those bounds and read command[7] without getting any exception or runtime error. In the example below, I input hello world as a command and reading command[7] returns the letter o which is the correct answer (I was expecting an error after trying to read outside the 5 character bound of my array).
Can someone explain what's going on? How can I make sure that the input gets truncated as I was expecting when the user goes over the buffer size that I've established?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char command[5]; //commands can't be longer than 4 characters
char c;
while (1)
{
printf("# "); //print command prompt
scanf("%[^\n]4s", command); //read command
while ((c = getchar()) != '\n' && c != EOF)
{
/*discard overflow input*/;
}
printf("received command: %c\n", command[7]); //echo character from command
if (strcmp(command, "exit") == 0)
{
break;
}
memset(command, 0, sizeof(command)); //clean command buffer
}
return 0;
}
CodePudding user response:
The C standard doesn't specify what happens when you access memory outside of what you've allocated. It could read correctly, it could read something else (if something that owns that memory overwrites it, it could cause a segmentation fault (if you access memory outside of your program's allocated space).
One option would be to use the width modifier on scanf to ensure you receive at most 4 characters:
scanf("%4s", command);
CodePudding user response:
As multiple people mentioned in the comments and other answers, the C standard establishes that accessing memory outside the allocated bounds results in undefined behavior, but that doesn't mean it will always give an error. I guess that was an interpretation error by me.
Regarding the specific issue I was having where scanf was reading more than 4 characters, the advice provided by @Weather Vane in the comments worked well. All that was needed was changing my scanf command.
From this: scanf("%[^\n]4s", command); //read command
To this: scanf("%4[^\n]s", command); //read command
This way, scanf will only write up to 4 characters into the buffer command, and the contiguous memory will be left untouched. Therefore, if I try to access command[7], I would get garbage or possibly an error.
For anyone wondering about the while loop that discards overflow, see the comment section.
