openai-cpp icon indicating copy to clipboard operation
openai-cpp copied to clipboard

'getenv' considered in MSVC as unsafe function

Open AwesomeTornado opened this issue 2 years ago • 2 comments

The function "std::getenv()" is flagged as insecure in Visual Studio (why) , in order to fix this, I changed the functions to "_dupenv_s()" instead.

The code is still the same as before, just with a more secure function.

I tested the changes with one of the example programs, works exactly as before.

AwesomeTornado avatar Aug 14 '23 02:08 AwesomeTornado

Thanks @AwesomeTornado for your work. Your changes do work in Visual Studio but not for other platform. (the CI fails)

I suggest two solutions:

  • We comment that in the README troubleshooting section to warn people using MSVC and having this error. Hence we can keep the current implementation (std::get_env) and we suggest to define the following at compilation:
#define _CRT_SECURE_NO_WARNINGS
  • We ignore the getenv warning with something like:
#ifdef _MSC_VER
#pragma warning(disable: 4996)
#endif

See https://stackoverflow.com/a/66090653/2352158

coin-au-carre avatar Aug 17 '23 12:08 coin-au-carre

Thanks for the quick follow up!

I think that using the pragma to disable the error when compiling with MSVC would be the best option, as it would require the least work from anyone using the library and would not suppress any errors elsewhere in someones code.

AwesomeTornado avatar Aug 19 '23 03:08 AwesomeTornado