Menu

#558 TCoolSearchWnd::CmEditFind throws an error under C++Builder (regression on trunk [r6572])

8
pending
Regression (14)
1
2023-09-18
2023-09-18
No

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.

Related

Commit: [r6572]

Discussion

  • Ognyan Chernokozhev

    Fix in [r6577].

     

    Related

    Commit: [r6577]

  • Ognyan Chernokozhev

    • status: open --> pending
     
  • Vidar Hasfjord

    Vidar Hasfjord - 2023-09-18

    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)

     
    • Ognyan Chernokozhev

      Right. Weren't the _s functions supposed to be 'safe' and prevent buffer overflows?

       
  • Vidar Hasfjord

    Vidar Hasfjord - 2023-09-18
    • labels: --> Regression
    • summary: TCoolSearchWnd::CmEditFind() throws an error under C++ Builder --> TCoolSearchWnd::CmEditFind throws an error under C++Builder (regression on trunk [r6572])
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,8 +1,8 @@
     When the RichEdit example is built with Embarcadero C++ Builder 11.3, trying to use the Search-&gt;Find functionality for source files (based on TCoolEditView) results in showing an error message box with just the text &#34;strncpy_s&#34; and then terminating the program.
    
    -The issue is caused by this code line in TCoolSearchWnd::CmEditFind
    +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](https://learn.microsoft.com/en-us/cpp/c-runtime-library/truncate?view=msvc-170) (defined as  `((size_t)-1)`) can be passed as the last parameter.
    +Note that the Visual C++ documentation states that the constant [\_TRUNCATE](https://learn.microsoft.com/en-us/cpp/c-runtime-library/truncate?view=msvc-170) (defined as  `((size_t)-1)`) can be passed as the last parameter.
    
     

    Related

    Commit: [r6572]

  • Vidar Hasfjord

    Vidar Hasfjord - 2023-09-18

    @jogybl wrote:

    Right. Weren't the _s functions supposed to be 'safe' and prevent buffer overflows?

    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

Log in to post a comment.