0

I used -Wall -Werror flags and still MinGW is not able to produce a warning when I am using the gets( ) function? Please tell me which flags should I use so that MinGW produces a warning?

Niraj Raut
  • 205
  • 2
  • 7
  • Why do you expect a warning? – HolyBlackCat Apr 28 '20 at 11:12
  • Because I am using the gets( ) function. – Niraj Raut Apr 28 '20 at 11:17
  • I don't remember right now the standard version from which gets is deprecated. Anyway, you maybe are compiling with an older version of the standard. Have you tried forcing the standard version with `-std=cxx`? – Roberto Caboni Apr 28 '20 at 11:32
  • @NirajRaut That's not what I'm asking. Did somebody tell you that there should be a warning, or did you see it cause a warning on some compiler, or ...? – HolyBlackCat Apr 28 '20 at 11:36
  • @Robert Caboni I have. -std=c11 – Niraj Raut Apr 28 '20 at 11:39
  • @HolyBlackCat GCC on my android gives a warning that "gets is a dangerous function and should not be used". – Niraj Raut Apr 28 '20 at 11:40
  • The `gets()` function was removed (not just deprecated) in C11, so if you set `std=c11` your compiler could abort compilation with an error. However, we're technically talking about the standard library here, not the compiler itself. So, depending what specific implementation the compiler uses, the function may or may not exist or be marked as deprecated. So the question is: what is the library used by MinGW? – Felix G Apr 28 '20 at 11:41
  • @HolyBlackCat The gets( ) function was removed from the C standard in 2011. – Niraj Raut Apr 28 '20 at 11:42
  • 1
    I'm pretty sure the issue is `msvcrt` (which is the default stdc library used by MinGW). If that still contains the function, and hasn't marked it in any way as deprecated, then there's not much you can do (except complain to Microsoft, i guess?) – Felix G Apr 28 '20 at 11:44
  • @Felix That's true. My compiler aborted compilation. I think that the libraries are not strictly C11 conforming as they contain the gets( ) function. But what should I do now? Because I want a warning. – Niraj Raut Apr 28 '20 at 11:46
  • So when you use `std=c11`, your compiler actually does abort compilation, but you want a warning instead? – Felix G Apr 28 '20 at 11:48
  • @Felix G Yes, I want that warning even though it aborts compilation. How to get that? – Niraj Raut Apr 28 '20 at 11:52
  • For MinGW, you can compile with `-D_FORTIFY_SOURCE` and you'll get an undefined reference to `__gets_chk` from the linker if you try and call `gets`. – Ian Abbott Apr 28 '20 at 11:56
  • 1
    It's because MinGW uses Microsoft crap libs(tm) instead of standard compliant ones. Duplicate question: [Why can I use gets() in gcc -std=c11?](https://stackoverflow.com/questions/30619780/why-can-i-use-gets-in-gcc-std-c11). – Lundin Apr 28 '20 at 13:05
  • With some luck, you may be able to get the "standard" lib to uncrap itself by doing `#define __USE_MINGW_ANSI_STDIO 1` before `#include ` – Lundin Apr 28 '20 at 13:06
  • @Lundin Is there any good reason other than buffer overflow for not using the gets( ) function? Is it safe if we know the input size? — Personally, I will never use it but wanted other possible reasons. – Niraj Raut Apr 29 '20 at 09:02
  • @NirajRaut `gets` is defective by design, you can't restrict the input from it like you can with `fgets` etc. Therefore the `gets` function has been completely removed from the C language - which would be another good reason not to use it, the function is there no longer in standard compliant C. – Lundin Apr 29 '20 at 11:06
  • @Lundin Is function declaration and function prototype the same? Since a function declarator with empty parentheses is an obsolescent feature — which implies that you should use the void keyword in the parentheses. Or is the old K & R declaration still valid since it is only an obsolescent feature and hasn't been removed from the standard (yet)? – Niraj Raut Apr 29 '20 at 17:01
  • @NirajRaut What has this to do with the question? A function declaration can be done with or without prototype style - prototype means a function declaration that names the types of all parameters, optionally with parameter names. K&R style parameter list are no longer valid, but it is still valid (but obsolete and bad practice) to declare a function without any parameter list: `void func ();`. This is a function declaration but not a function prototype. – Lundin Apr 30 '20 at 07:00

0 Answers0