Scanf_s warning? Skips User Inputs (topics: Runge-Kutta, Epidemic Simulation)

狂风中的少年 提交于 2019-12-01 23:22:44
Malcolm

What the compiler is telling you here is that the function scanf is not safe. scanf has a bug that, if exploited, can cause a system to become compromised (called a buffer overflow attack). In brief, the bug is that one does not tell scanf how many bytes to read for input. Thus scanf will read until it "believes" it is done reading the input. In a char array, this end is usually the null character '\0'. However, if one leaves off '\0' from a string, scanf will continue reading until it finds that byte -- usually, scanf will reach a memory location that is outside of its own virtual memory space. This action will cause the OS to send your program a segmentation fault (seg fault) which will summarily end your program's existence.

The newer function, scanf_s,(_s for secure), lets you determine the max size of the input, which you can use to more effectively prevent buffer overflow attacks. If this is for a HW assignment, which it looks like it is, you can leave scanf there. However, to get rid of the compilier warning AND try and become a better programmer, fix it! Use sscanf_s and have a global variable (or something...) that determines maximum input size (e.g. int SCANF_INPUT_SIZE = 1000 ).

Good luck!

EDIT -- Change those "&f" to "%f" that's the error!

What the compiler is telling you is that Microsoft thinks that scanf is not safe.

The scanf function can be used safely if you're careful. scanf does have problems with numeric input (overflow has undefined behavior), but scanf_s doesn't fix those problems.

scanf_s was originally a Microsoft-specific extension; it was added as an optional feature to the 2011 ISO C standard (N1570 Annex K). Many C implementations still don't provide scanf_s.

Quoting the C draft standard:

K.3.5.3.4p4:

The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s.

K.3.5.3.2p4:

The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a *). The first of these arguments is the same as for fscanf. That argument is immediately followed in the argument list by the second argument, which has type rsize_t and gives the number of elements in the array pointed to by the first argument of the pair. If the first argument points to a scalar object, it is considered to be an array of one element.

Using scanf_s rather than scanf for this particular program makes it no safer, but causes it to be less portable.

Use _CRT_SECURE_NO_WARNINGS and ignore the warning.

scanf reads a value into memory,if the value you are reading is longer than the memory you are giving it ( typically only a problem with strings) it could overwrite some other memory and lead to a bug or a virus

scanf_s is a new version where you tell the function the maximum memory to read.

If this is only homework code that only you or a trusted user is going to use - don'tworry

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