How do you stop stack (buffer) overflow from scanf?

+1 David Green · August 4, 2015
I just tested if you can cause a stack overflow with scanf and indeed you can. Is there any way of mitigating this? What I did was I made a really small string 4 bytes long, and put a ton of bytes into it by mashing my keyboard at runtime.

Post a Reply

Replies

Oldest  Newest  Rating
+2 c student · August 4, 2015
scanf ("%ns", str);    // where n is max number of characters excluding null terminator
0 David Green · August 5, 2015
Ok thank you
  • 1

C

107,322 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator