Lkaf Temravet
Lkaf Temravet

Reputation: 993

stack arround variable corrupted

when running this code, I code error : stack around variable window_text_hex corrupted. I can't see the error?

DWORD WINAPI fill_matrix(LPVOID lpParameter)
{
    char window_text_hex[5];
    for (int i = 0; i < 8; i++){
        int data_j = 0;
        for (int j = 0; j <8; j++)
        {   

            char window_text[5] = { mySerial.data[i][data_j], mySerial.data[i][data_j + 1], mySerial.data[i][data_j + 2], mySerial.data[i][data_j + 3], '\0' };
            for (int i = 0; i<4; i++)
            {
                sprintf(&window_text_hex[i], "%02X", window_text[i]);
            }
            SetWindowTextA(hWndLabel[i * 8 + j], window_text_hex);
            data_j += 4;
        }
    }
    return 0;
}

mySerial.data is an array of [8][36]

Upvotes: 0

Views: 53

Answers (1)

melpomene
melpomene

Reputation: 85877

In your for loop, i can be 3.

You're sprintfing to window_text_hex+3, which has room for two bytes (window_text_hex[3], windows_text_hex[4]).

sprintf %02X writes at least three bytes: Two hex digits and a '\0' terminator.
(Since you're passing a char, it may actually write much more. char is often a signed type, but %X expects an unsigned int. So negative inputs (like -1) produce long outputs (FFFFFFFF). Fix: (unsigned char)window_text[i].)

You're writing past the end of your window_text_hex array.

Upvotes: 4

Related Questions