Buffer overflow detected in a program that runs flawlessly ( apparently)

拈花ヽ惹草 提交于 2019-12-06 07:58:26
   int t; char r[3];
   gets(r);

Never ever use gets function. In this case if it receives more than 3 characters (including the terminating null character) you get a buffer overflow.

gets has been removed in the current Standard (C11) and was deprecated in the previous Standard (C99).

The code probably entered a 100-character string, which would take 101 bytes to store (including the NUL byte at the end)! Also, never, never use the gets() function in any code that is meant to be robust. Try getline(), which automatically expands the buffer for you.

Also, with backtraces like these you can use addr2line to decode the addresses. Compile with -g and -rdynamic.

This

char r[3];
gets(r);

is extremely likely to break for any input of more than 2 characters plus a newline. Note that gets is officially obsoleted by the C standard due to its buffer overflow problem. These days we use something along

char r[42];

if (fgets (r, sizeof r, stdin) != NULL) { ... }

which is safe from overflowing (it rather truncates the input and leaves the rest for the next fgets).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!