Skip to content

clang(windows) getenv deprecation warn fix - #900

Closed
alexv-ds wants to merge 1 commit into
ArthurSonzogni:mainfrom
alexv-ds:msvc-getenv-deprecation-fix
Closed

alexv-ds wants to merge 1 commit into
ArthurSonzogni:mainfrom
alexv-ds:msvc-getenv-deprecation-fix

Conversation

@alexv-ds

@alexv-ds alexv-ds commented Jul 18, 2024

Copy link
Copy Markdown

getenv in clang(windows) is deprecated

based on spdlog

@ArthurSonzogni

Copy link
Copy Markdown
Owner

Thanks!

#else
size_t len = 0;
char buf[1024];
bool ok = ::getenv_s(&len, buf, sizeof(buf), field) == 0;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getenv_s is a C11 function. Is it guaranteed to be defined in C++11?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know.😅

std::string getenv_safe(const char *field) {
#if defined(_MSC_VER)
#if defined(__cplusplus_winrt)
return std::string{}; // not supported under uwp

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it means FTXUI won't work under UWP? This sounds problematic.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I haven't tested this under UWP, but I can assume that UWP applications don't have access to environment variables.

return ok ? buf : std::string{};
#endif
#else // revert to getenv
char *buf = ::getenv(field); // NOLINT(*-mt-unsafe)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the previous implementation was meant to fix this *-mt-unsafe warning. We probably want to revert it back (e.g. do not store the output in a non temporary object)

return s.find(key) != std::string::npos;
}

// https://github.com/gabime/spdlog/blob/885b5473e291833b148eeac3b7ce227e582cd88b/include/spdlog/details/os-inl.h#L566

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any warning on my bots:
https://github.com/ArthurSonzogni/FTXUI/actions/runs/9695462958/job/26755276508

What configs should I add to reproduce?

Are you using the build config the FTXUI project define (e.g. are you using CMake at the end)

@alexv-ds alexv-ds Jul 20, 2024

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I'm sorry. This is not msvc, but clang for windows.

cmake -Bbuild-clang -DCMAKE_CXX_COMPILER=clang++.exe -DCMAKE_C_COMPILER=clang.exe -GNinja
C:/Users/Alex/Desktop/FTXUI/src/ftxui/screen/terminal.cpp:64:37: warning: 'getenv' is deprecated: This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [-Wdeprecated-declarations]
   64 |   std::string COLORTERM = Safe(std::getenv("COLORTERM"));  // NOLINT

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexv-ds alexv-ds changed the title msvc getenv deprecation warn fix clang(windows) getenv deprecation warn fix Jul 20, 2024
@ArthurSonzogni
ArthurSonzogni force-pushed the main branch 5 times, most recently from f353474 to c5357ac Compare August 18, 2024 08:46
@ArthurSonzogni
ArthurSonzogni force-pushed the main branch 2 times, most recently from 3bdbccd to 11f7132 Compare August 27, 2025 16:51
@ArthurSonzogni

Copy link
Copy Markdown
Owner

Thanks @alexv-ds for the report and the patch, and sorry for the very long wait!

I'm going to follow a slightly different approach in #1350:

  • A single util::GetEnv() helper keeps using std::getenv, and silences the deprecation warning locally around that one call with #pragmas (#pragma clang diagnostic for clang, #pragma warning(disable : 4996) for MSVC).
  • Every call site in terminal.cpp and app.cpp goes through it.

Compared to getenv_s, this keeps the behavior identical: no fixed-size buffer, and no special case returning an empty value under UWP. It also covers the new getenv calls added since this PR.

I verified it by simulating the Microsoft CRT deprecation with clang on Linux, but I can't test a real clang build on Windows. If you still use that setup, could you confirm the warning is gone with #1350?

Closing in favor of #1350.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants