From c151e72ed2fd95a146635e73a035bbd26f1f6eb4 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Mon, 5 Jun 2017 09:22:40 -0400 Subject: [PATCH 1/5] Remove windows warning, use secure getenv --- include/CLI/App.hpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 85035f7e9..036455369 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -815,9 +815,24 @@ class App { // Get envname options if not yet passed for(const Option_p &opt : options_) { if(opt->count() == 0 && opt->envname_ != "") { - char *ename = std::getenv(opt->envname_.c_str()); - if(ename != nullptr) { - opt->add_result(std::string(ename)); + char *buffer = nullptr; + std::string ename_string; + + #ifdef _MSC_VER + // Windows version + size_t sz = 0; + if(_dupenv_s(&buf, &sz, opt->envname_.c_str()) == 0 && buf != nullptr) { + ename_string = std::string(buffer); + free(buffer); + } + #else + // This also works on Windows, but gives a warning + buffer = std::getenv(opt->envname_.c_str()); + ename_string = std::string(buffer); + #endif + + if(!ename_string.empty()) { + opt->add_result(ename_string)); } } } From 39df0e36fc224b182f03227c9d27ddebfa2dec80 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Mon, 5 Jun 2017 09:25:31 -0400 Subject: [PATCH 2/5] Fix typo in last commit --- include/CLI/App.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 036455369..6294e4726 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -832,7 +832,7 @@ class App { #endif if(!ename_string.empty()) { - opt->add_result(ename_string)); + opt->add_result(ename_string); } } } From 04c4480657487d9864dd8934eddc9a74c2a745d0 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Mon, 5 Jun 2017 09:27:46 -0400 Subject: [PATCH 3/5] Fix typo in windows version --- include/CLI/App.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 6294e4726..f2b134687 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -821,7 +821,7 @@ class App { #ifdef _MSC_VER // Windows version size_t sz = 0; - if(_dupenv_s(&buf, &sz, opt->envname_.c_str()) == 0 && buf != nullptr) { + if(_dupenv_s(&buffer, &sz, opt->envname_.c_str()) == 0 && buf != nullptr) { ename_string = std::string(buffer); free(buffer); } From 0428cedaa24b7d3b878aaef22085eff9ed642858 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Mon, 5 Jun 2017 09:29:53 -0400 Subject: [PATCH 4/5] Fix another typo in windows version --- include/CLI/App.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index f2b134687..fa01ca791 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -821,7 +821,7 @@ class App { #ifdef _MSC_VER // Windows version size_t sz = 0; - if(_dupenv_s(&buffer, &sz, opt->envname_.c_str()) == 0 && buf != nullptr) { + if(_dupenv_s(&buffer, &sz, opt->envname_.c_str()) == 0 && buffer != nullptr) { ename_string = std::string(buffer); free(buffer); } From 2bc77eb4fb3fa2df77e36a464096597f1cd6d1ac Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Mon, 5 Jun 2017 09:40:37 -0400 Subject: [PATCH 5/5] Fixing error, dropping warn suppression --- CMakeLists.txt | 1 - include/CLI/App.hpp | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a5666a7e8..38955915a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,6 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) # Be moderately paranoid with flags if(MSVC) add_definitions("/W4") - add_definitions(-D_CRT_SECURE_NO_WARNINGS) else() add_definitions("-Wall -Wextra -pedantic") endif() diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index fa01ca791..594b607d3 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -828,7 +828,8 @@ class App { #else // This also works on Windows, but gives a warning buffer = std::getenv(opt->envname_.c_str()); - ename_string = std::string(buffer); + if(buffer != nullptr) + ename_string = std::string(buffer); #endif if(!ename_string.empty()) {