When the RichEdit example is built with Embarcadero C++ Builder 11.3, trying to use the Search->Find functionality for source files (based on TCoolEditView) results in showing an error message box with just the text "strncpy_s" and then terminating the program.
The issue is caused by revision [r6572] of this code line in TCoolSearchWnd::CmEditFind
tcsncpy_s(SearchData.FindWhat, SearchData.BuffSize, w.c_str(), -1);
Apparently the strncpy_s function in C++ Builder standard library does not accept -1 as the last parameter.
Note that the Visual C++ documentation states that the constant _TRUNCATE (defined as ((size_t)-1)
) can be passed as the last parameter.
Fix in [r6577].
Related
Commit: [r6577]
Good catch! However, the fix risks overflowing the buffer creating a similar runtime exception. Passing _TRUNCATE neatly replaced a verbose std::min expression to do the truncation explicitly here. That's why I used it. Shame that the Embarcadero library doesn't support it. Here is the safe, but quietly truncating expression:
min(static_cast<int>(w.size()), SearchData.BuffSize - 1)
Right. Weren't the
_s
functions supposed to be 'safe' and prevent buffer overflows?Diff:
Related
Commit: [r6572]
@jogybl wrote:
Yeah, they do — by terminating the application. The hideous thing they prevent is UB (undefined behaviour), the zombie state in which the program continues to run in a corrupted state. Silent truncation is also for the most part really bad. But here it is sufferable, I think.
Last edit: Vidar Hasfjord 2023-09-18