I get the error (stack around the variable buf was corrupted) after the message box, how do I fix it?

Ahmed Osama 120 Reputation points
2023-03-01T14:20:22.53+00:00

How do I fix the error (stack around the variable "buf" was corrupted)

The code:

  if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) != IDM_ABOUT && LOWORD(wParam) != IDM_EXIT) {
            GetWindowTextA(hWndEdit, &fin[0], 64300);
            GetWindowTextA(hWndEdit2, &scin[0], 64300);
            if (scin == (LPSTR)"") {
                ShellExecuteA(NULL, "runas", &fin[0], NULL, NULL, SW_SHOWNORMAL);
            }
            else {
                ShellExecuteA(NULL, "runas", &fin[0], &scin[0], NULL, SW_SHOWNORMAL);
            }
            if (GetLastError() != 0) {
                char buf;
                sprintf(&buf, "%08x", (int)GetLastError());
                std::string res(&buf);
                DWORD le = GetLastError();
                std::string message = (std::string)"APC Administrative Application Execution Utility encountured an error of code 0x" + res + ": " + "\"" + std::system_category().message(GetLastError()); +"\"";
                int lastError = GetLastError();
                std::wstring hexerror = L"0x" + WCHAR(DecToHex(GetLastError()));
                const char* HEs = message.c_str();
                MessageBoxA(hWnd, LPCSTR(message.c_str()), "APC Administrative Application Execution Utility", MB_ICONERROR);
            }

It says:

Run-Time Check Failure #2 - Stack around the variable 'buf' was corrupted.

Image:

User's image

How to fix this problem and without causing errors?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,559 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 40,946 Reputation points
    2023-03-01T14:50:40.2+00:00

    The call to sprintf in the posted code is printing 8 characters to a 1 character buffer. The buffer overrun has corrupted the stack. The destination buffer should be at least 9 characters, 8 for the text and 1 for the null terminator.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Ahmed Osama 120 Reputation points
    2023-03-01T15:26:45.4033333+00:00

    Here's the final executable:

    https://1drv.ms/u/s!AmKkgU04nxaruFZRxwdLcKkmHsPR?e=Gatu9S

    WARNING: ALL THE FILES CONTAINED ARE THE INTELLECTUAL PROPERTY RIGHTS AND COPYRIGHTS OF ME, ANY REVERSE ENGINNIRING, COPYING, MODIFIYING, OR OTHERWISE TRANSFERRING THE EXECUTABLE OR MODIFIYING IT OR MOVING THE EXECUTABLE IN ANY WAY, OR REVERSE ENGINNIRING OR TRANSFERRING THE SOURCE FORM OF THE EXECUTABLE, NON-COMPILED WITHOUT THE EXPRESS WRITTEN PERMISSION OF ME, ANY THING OF THOSE OBLIGATIONS IF MADE WITHOUT EXPRESS WRITTEN PERMISSION, IT WOULD BE CONSIDERED COPYRIGHT INFIRGMENT, VIOLATORS WILL BE PROSECUTED TO THE MAXIUMUM EXTENT POSSIBLE ALLOWED BY LAW.