argtable / argtable3

A single-file, ANSI C, command-line parsing library that parses GNU-style command-line options.
http://www.argtable.org
Other
372 stars 65 forks source link

Implicit declaration of getenv_s() #61

Closed pwpiwi closed 3 years ago

pwpiwi commented 3 years ago
cliparser/argtable3.c: In function 'getopt_internal':
cliparser/argtable3.c:1710:3: warning: implicit declaration of function 'getenv_s'; did you mean 'getenv'? [-Wimplicit-function-declaration]
 1710 |   getenv_s(&requiredSize, NULL, 0, "POSIXLY_CORRECT");
      |   ^~~~~~~~
      |   getenv

This is on mingw. I propose to add the usual checks for the availability of this C11 extension:

    if (posixly_correct == -1 || optreset) {
#ifdef _WIN32 && ((defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)))

        size_t requiredSize;
        getenv_s(&requiredSize, NULL, 0, "POSIXLY_CORRECT");
        posixly_correct = requiredSize != 0;
#else
        posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
#endif
    }
tomghuang commented 3 years ago

Hi @pwpiwi , when I tried to fix this issue in the msys2/mingw64 environment, I kept seeing the following error:

D:/Projects/argtable3/build_mingw64/src/version.rc:33:10: fatal error: verrsrc.h: No such file or directory
   33 | #include <verrsrc.h>
      |          ^~~~~~~~~~~
compilation terminated.

Do you have the same issue? If yes, how did you fix it? Thanks.

pwpiwi commented 3 years ago

Don't know. I never tried to build it. I am using the "amalgamation" argtable3.c instead. . I googled for this file and it seems to be part of Visual Studio. Therefore not available on msys2/mingw

pwpiwi commented 3 years ago

Re getenv_s(): Why using it at all? In your case you just want to check if the environment variable is availabe. Isn't this always safe?

tomghuang commented 3 years ago

It might be an issue of msys2/mingw64, because msys2/mingw32 doesn't have this issue.

getenv_s() is used to make Visual Studio compiler happy, because VC++ wants us to use more secure functions. Otherwise, it will show tons of warnings.