curlx: replace mbstowcs/wcstombs with _s counterparts (Windows)#19581
curlx: replace mbstowcs/wcstombs with _s counterparts (Windows)#19581vszakats wants to merge 4 commits into
mbstowcs/wcstombs with _s counterparts (Windows)#19581Conversation
|
IMO changes like this are unnecessary. I've never been a fan of these _s suffixed "secure" functions. This is harder to understand and for multibyte conversion code changes it looks like it doesn't have the same effect. edit: It looks like the invalid parameter handler is not invoked if the conversion fails. Still don't like it though. |
|
@jay: Thanks for your fixes! I also don't like the |
wcscpy() and wcsncpy() with their _s counterparts (Windows)_s counterparts (Windows)
|
Tests still hang due to file open errors, so something is fishy with my |
|
Are the methods generally portable? I have not seen _s for wcs broadly. |
They are supported by all the compiler/SDK versions curl supports. I didn't explore the edges of portability further than the supported range. |
|
Moved the first commit here to a separate PR: #19589 |
|
I wrongly thought "safe" open functions leave the global "Always check the return value to see if the function succeeded before you do |
|
@rsbeckerca I checked source (Chromium dependency): https://github.com/google/breakpad/blob/470771e32d0167c2654ecb9113dc56a3510b695f/src/common/windows/string_utils.cc#L59-L69 The few other ones I checked randomly are also present in |
|
It seems harder to maintain, people reading the code including myself may not be familiar. Everyone who writes C can probably head parse a fopen but not a fopen_s, strncpy but not strncpy_s, etc. Regarding the warnings though, are you referring to deprecation warnings? MS "deprecation" for stuff like this is basically they aren't going to remove the functions:
If those are the warnings you mean I suggest silence them with _CRT_SECURE_NO_WARNINGS etc. The reason I took issue with security is because IMO it's only an improvement in some limited circumstances that aren't really conducive to the way we all write. There's no reason for a strncpy_s to receive the size of the destination buffer because the size being copied is already passed as the count. Here's an example from the pr:
If we calculated |
|
I agree with these being unfamiliar and also not a silver bullet for more secure code, ...unless they come with properties that are breaking, and I suspect we may see this We already silence these warnings, and that's not the issue, but I think it'd be nicer |
6c0140a to
6ef4187
Compare
|
+1 for using these in code that's Windows-specific anyway. I think the reason we couldn't use them in the past was legacy MinGW. |
_s counterparts (Windows)mbstowcs/wcstombs with their _s counterparts (Windows)
mbstowcs/wcstombs with their _s counterparts (Windows)mbstowcs/wcstombs with _s counterparts (Windows)
…ndows) Replace: - curl_sspi: macro `_tcsncpy()` with `_tcsncpy_s()`. - curlx/fopen: `wcsncpy()` with `wcsncpy_s()`. - curlx/fopen: `wcscpy()` with `wcscpy_s()`. Use of the pre-existing functions were safe. This patch aims to use the recommended Windows CRT functions. Handle errors returned by them. Also to avoid the compiler warnings silenced via `_CRT_SECURE_NO_WARNINGS`: ``` lib/curl_sspi.c(152): warning C4996: 'wcsncpy': This function or variable may be unsafe. Consider using wcsncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. lib/curlx/fopen.c(161): warning C4996: 'wcsncpy': This function or variable may be unsafe. Consider using wcsncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. lib/curlx/fopen.c(162): warning C4996: 'wcscpy': This function or variable may be unsafe. Consider using wcscpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. lib/curlx/fopen.c(174): warning C4996: 'wcsncpy': This function or variable may be unsafe. Consider using wcsncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. lib/curlx/fopen.c(175): warning C4996: 'wcscpy': This function or variable may be unsafe. Consider using wcscpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. ``` Refs: https://learn.microsoft.com/cpp/c-runtime-library/reference/strncpy-strncpy-l-wcsncpy-wcsncpy-l-mbsncpy-mbsncpy-l https://learn.microsoft.com/cpp/c-runtime-library/reference/strncpy-s-strncpy-s-l-wcsncpy-s-wcsncpy-s-l-mbsncpy-s-mbsncpy-s-l https://learn.microsoft.com/cpp/c-runtime-library/security-features-in-the-crt Cherry-picked from #19581 (in part) Closes #19589
mbstowcs/wcstombs with _s counterparts (Windows)mbstowcs/wcstombs with _s counterparts (Windows)
https://learn.microsoft.com/cpp/c-runtime-library/reference/mbstowcs-mbstowcs-l https://learn.microsoft.com/cpp/c-runtime-library/reference/mbstowcs-s-mbstowcs-s-l https://learn.microsoft.com/cpp/c-runtime-library/reference/wcstombs-wcstombs-l https://learn.microsoft.com/cpp/c-runtime-library/reference/wcstombs-s-wcstombs-s-l Apply Jay's suggestions from code review Co-authored-by: Jay Satiro <raysatiro@yahoo.com> apply Jay's patch to the mbstowcs_s case
They are used in Windows-specific
fopen(),freopen,open()andcurlx_get_winapi_error()calls, and infix_excessive_path()inUnicode builds.
Refs:
https://learn.microsoft.com/cpp/c-runtime-library/reference/mbstowcs-mbstowcs-l
https://learn.microsoft.com/cpp/c-runtime-library/reference/mbstowcs-s-mbstowcs-s-l
https://learn.microsoft.com/cpp/c-runtime-library/reference/wcstombs-wcstombs-l
https://learn.microsoft.com/cpp/c-runtime-library/reference/wcstombs-s-wcstombs-s-l
Also ban these functions via checksrc.
Co-authored-by: Jay Satiro
https://github.com/curl/curl/pull/19581/files?w=1
curlx_freopen()#19598_tcsncpy/wcsncpy/wcscpywith_scounterparts (Windows) #19589