if one complains about gets(), why not do the same with scanf(“%s”,…)?

前端 未结 5 667
情深已故
情深已故 2021-01-06 03:57

From man gets:

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters ge

5条回答
  •  Happy的楠姐
    2021-01-06 04:18

    The answer is simply that no-one has written the code in GCC to produce that warning.

    As you point out, a warning for the specific case of "%s" (with no field width) is quite appropriate.

    However, bear in mind that this is only the case for the case of scanf(), vscanf(), fscanf() and vfscanf(). This format specifier can be perfectly safe with sscanf() and vsscanf(), so the warning should not be issued in that case. This means that you cannot simply add it to the existing "scanf-style-format-string" analysis code; you will have to split that into "fscanf-style-format-string" and "sscanf-style-format-string" options.

    I'm sure if you produce a patch for the latest version of GCC it stands a good chance of being accepted (and of course, you will need to submit patches for the glibc header files too).

提交回复
热议问题