Skip to content

Commit 6cb2ae6

Browse files
authored
Enable/fix compiler warning 4996 in host (#66431)
1 parent 0046648 commit 6cb2ae6

File tree

7 files changed

+23
-20
lines changed

7 files changed

+23
-20
lines changed

src/native/corehost/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ include(../../../eng/native/configurepaths.cmake)
1010
include(${CLR_ENG_NATIVE_DIR}/configurecompiler.cmake)
1111

1212
if (MSVC)
13-
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4996>)
1413
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4267>)
1514
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4018>)
1615
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4200>)

src/native/corehost/apphost/static/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ include(../../hostpolicy/files.cmake)
4545
include(../../hostcommon/files.cmake)
4646

4747
if(MSVC)
48-
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4996>)
4948
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4267>)
5049
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4018>)
5150
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4200>)

src/native/corehost/hostmisc/pal.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,24 @@ namespace pal
158158

159159
inline size_t strlen(const char_t* str) { return ::wcslen(str); }
160160

161-
#pragma warning(suppress : 4996) // error C4996: '_wfopen': This function or variable may be unsafe.
162-
inline FILE* file_open(const string_t& path, const char_t* mode) { return ::_wfopen(path.c_str(), mode); }
161+
inline FILE* file_open(const string_t& path, const char_t* mode) { return ::_wfsopen(path.c_str(), mode, _SH_DENYNO); }
163162

164163
inline void file_vprintf(FILE* f, const char_t* format, va_list vl) { ::vfwprintf(f, format, vl); ::fputwc(_X('\n'), f); }
165164
inline void err_fputs(const char_t* message) { ::fputws(message, stderr); ::fputwc(_X('\n'), stderr); }
166165
inline void out_vprintf(const char_t* format, va_list vl) { ::vfwprintf(stdout, format, vl); ::fputwc(_X('\n'), stdout); }
167166

168-
// This API is being used correctly and querying for needed size first.
169-
#pragma warning(suppress : 4996) // error C4996: '_vsnwprintf': This function or variable may be unsafe.
170-
inline int str_vprintf(char_t* buffer, size_t count, const char_t* format, va_list vl) { return ::_vsnwprintf(buffer, count, format, vl); }
167+
inline int str_vprintf(char_t* buffer, size_t count, const char_t* format, va_list vl) { return ::_vsnwprintf_s(buffer, count, _TRUNCATE, format, vl); }
168+
inline int strlen_vprintf(const char_t* format, va_list vl) { return ::_vscwprintf(format, vl); }
171169

172-
// Suppressing warning since the 'safe' version requires an input buffer that is unnecessary for
173-
// uses of this function.
174-
#pragma warning(suppress : 4996) // error C4996: '_wcserror': This function or variable may be unsafe.
175-
inline const char_t* strerror(int errnum) { return ::_wcserror(errnum); }
170+
inline const string_t strerror(int errnum)
171+
{
172+
// Windows does not provide strerrorlen to get the actual error length.
173+
// Use 1024 as the buffer size based on the buffer size used by glibc.
174+
// _wcserror_s truncates (and null-terminates) if the buffer is too small
175+
char_t buffer[1024];
176+
::_wcserror_s(buffer, sizeof(buffer) / sizeof(char_t), errnum);
177+
return buffer;
178+
}
176179

177180
bool pal_utf8string(const string_t& str, std::vector<char>* out);
178181
bool pal_clrstring(const string_t& str, std::vector<char>* out);
@@ -226,7 +229,9 @@ namespace pal
226229
inline void err_fputs(const char_t* message) { ::fputs(message, stderr); ::fputc(_X('\n'), stderr); }
227230
inline void out_vprintf(const char_t* format, va_list vl) { ::vfprintf(stdout, format, vl); ::fputc('\n', stdout); }
228231
inline int str_vprintf(char_t* str, size_t size, const char_t* format, va_list vl) { return ::vsnprintf(str, size, format, vl); }
229-
inline const char_t* strerror(int errnum) { return ::strerror(errnum); }
232+
inline int strlen_vprintf(const char_t* format, va_list vl) { return ::vsnprintf(nullptr, 0, format, vl); }
233+
234+
inline const string_t strerror(int errnum) { return ::strerror(errnum); }
230235

231236
inline bool pal_utf8string(const string_t& str, std::vector<char>* out) { out->assign(str.begin(), str.end()); out->push_back('\0'); return true; }
232237
inline bool pal_clrstring(const string_t& str, std::vector<char>* out) { return pal_utf8string(str, out); }

src/native/corehost/hostmisc/pal.unix.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ bool pal::getcwd(pal::string_t* recv)
128128
return false;
129129
}
130130

131-
trace::error(_X("getcwd() failed: %s"), strerror(errno));
131+
trace::error(_X("getcwd() failed: %s"), strerror(errno).c_str());
132132
return false;
133133
}
134134

@@ -401,7 +401,7 @@ bool pal::get_default_bundle_extraction_base_dir(pal::string_t& extraction_dir)
401401
}
402402
else if (errno != EEXIST)
403403
{
404-
trace::error(_X("Failed to create default extraction directory [%s]. %s"), extraction_dir.c_str(), pal::strerror(errno));
404+
trace::error(_X("Failed to create default extraction directory [%s]. %s"), extraction_dir.c_str(), pal::strerror(errno).c_str());
405405
return false;
406406
}
407407

@@ -478,7 +478,7 @@ bool get_install_location_from_file(const pal::string_t& file_path, bool& file_f
478478
}
479479
else
480480
{
481-
trace::error(_X("The install_location file ['%s'] failed to open: %s."), file_path.c_str(), pal::strerror(errno));
481+
trace::error(_X("The install_location file ['%s'] failed to open: %s."), file_path.c_str(), pal::strerror(errno).c_str());
482482
}
483483
}
484484

@@ -904,7 +904,7 @@ bool pal::realpath(pal::string_t* path, bool skip_error_logging)
904904

905905
if (!skip_error_logging)
906906
{
907-
trace::error(_X("realpath(%s) failed: %s"), path->c_str(), strerror(errno));
907+
trace::error(_X("realpath(%s) failed: %s"), path->c_str(), strerror(errno).c_str());
908908
}
909909

910910
return false;
@@ -1033,7 +1033,7 @@ bool pal::is_emulating_x64()
10331033
trace::info(_X("Could not determine whether the current process is running under Rosetta."));
10341034
if (errno != ENOENT)
10351035
{
1036-
trace::info(_X("Call to sysctlbyname failed: %s"), strerror(errno));
1036+
trace::info(_X("Call to sysctlbyname failed: %s"), strerror(errno).c_str());
10371037
}
10381038

10391039
return false;

src/native/corehost/hostmisc/pal.windows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ bool pal::get_default_bundle_extraction_base_dir(pal::string_t& extraction_dir)
597597
if (CreateDirectoryW(extraction_dir.c_str(), NULL) == 0 &&
598598
GetLastError() != ERROR_ALREADY_EXISTS)
599599
{
600-
trace::error(_X("Failed to create default extraction directory [%s]. %s, error code: %d"), extraction_dir.c_str(), pal::strerror(errno), GetLastError());
600+
trace::error(_X("Failed to create default extraction directory [%s]. %s, error code: %d"), extraction_dir.c_str(), pal::strerror(errno).c_str(), GetLastError());
601601
return false;
602602
}
603603

src/native/corehost/hostmisc/trace.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void trace::error(const pal::char_t* format, ...)
130130

131131
va_list dup_args;
132132
va_copy(dup_args, args);
133-
int count = pal::str_vprintf(nullptr, 0, format, args) + 1;
133+
int count = pal::strlen_vprintf(format, args) + 1;
134134
std::vector<pal::char_t> buffer(count);
135135
pal::str_vprintf(&buffer[0], count, format, dup_args);
136136

src/native/corehost/json_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ bool json_parser_t::parse_file(const pal::string_t& path)
135135
pal::ifstream_t file{ path };
136136
if (!file.good())
137137
{
138-
trace::error(_X("Cannot use file stream for [%s]: %s"), path.c_str(), pal::strerror(errno));
138+
trace::error(_X("Cannot use file stream for [%s]: %s"), path.c_str(), pal::strerror(errno).c_str());
139139
return false;
140140
}
141141

0 commit comments

Comments
 (0)