+ 1

Whats overflowing in the memory there ? Put 18 chars in and something weird happens. Can anyone logically explain, why ?

https://code.sololearn.com/cPLSdUJ1Svy7/?ref=app

6th Dec 2018, 4:33 AM
Hotwire
Hotwire - avatar
5 odpowiedzi
+ 6
You're overwriting the return pointer (in the stack). That's how this code works: https://code.sololearn.com/cYgU9YGHlfD1/?ref=app You can make yours do the same thing by this line after gets(): a[18]=1; // max entry is 11 chars now; the pointer has to remain mostly intact. No input works fine. This causes main to return backwards halfway into a multibyte instruction (this split just happens to be valid) then fall through main() again, forever. Actually, if you remove the printf, you'll run out of memory but that's a longer explanation.
6th Dec 2018, 5:25 AM
Kirk Schafer
Kirk Schafer - avatar
+ 4
'gets()' doesn't do any bounds checking,It cant tell if you entered a number of strings exceeding what's expected,thus you get a memory leak..Use fgets(a,10,stdin) instead.
6th Dec 2018, 5:02 AM
Mensch
Mensch - avatar
+ 2
Yes i know this, i gave some stuff wich is too big in an memoryspace which is to small, but what im interested in is knowing what's going on there in the memory ? Why does it behave like this exactly. What am i overflowing ? Heap, Stack, buffer or which kind of magic is happening there ?
6th Dec 2018, 5:06 AM
Hotwire
Hotwire - avatar
+ 2
[meta, reposted to clarify] By the way, compilers can enable a stack "security cookie" to guard against stack overflows with the proper compiler flag. Cookies are autogenerated and corrupting the cookie auto-kills your app -- but they're optional.
6th Dec 2018, 5:58 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Nice to know, but it is very interesting to do things like that, thanks for the answer. Your explanation is really good and helpfull. Would nice to disassemble your program, to see exactly what is happening there. (Another reason to make an assembly tutorial in sololearn :) ) Thanks for that good explanation. 3E2B thumbs up !!
7th Dec 2018, 2:19 AM
Hotwire
Hotwire - avatar