Skip to content

Conversation

@Christy538
Copy link

No description provided.

Copy link

@daniel-falk daniel-falk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

I added some comments about vulnerabilities and error handling :)

/ Daniel

int i;
char h[20];
printf("Enter your name: \n");
scanf("%s",h); //Takes input from the user

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using scanf like this is undefined behavior and can cause segfault or open up for malicious exploits. This will write the input to the stack until a word separator is found in the input, even if the input is longer than the size of the "h" buffer.

Use:
scanf("%19s", h); // leave 1 byte for the null-terminator

the rest of the line/word will be left in stdin and will be read by the next scanf instead of the number, so consider emptying stdin, e.g.

if (strlen(h) == sizeof(h)-1) while (getchar() != '\n'); // Empty left overs of name

scanf("%s",h); //Takes input from the user
printf("Your name is: %s\n",h);
printf("Enter a number: \n");
scanf("%d",&i);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the input number does not fit in an signed integer it will roll over, if it is not a number it will set i to 0. Some error handling for these cases are good practice, see https://wiki.sei.cmu.edu/confluence/display/c/INT05-C.+Do+not+use+input+functions+to+convert+character+data+if+they+cannot+handle+all+possible+inputs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants