简体   繁体   中英

strtok_s return incorrect data inside windbg

(Hello Everyone) I have some problem with strtok_s. I wrote this code(x64).

    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>


    BOOL TestMD5(CONST WCHAR* MD5_DATABASE_FILE)
    {
    HANDLE  hFile = INVALID_HANDLE_VALUE;
    DWORD   FileSize = 0;
    DWORD   dwReaded = 0;
    PBYTE   pData = NULL;
    BOOL    bRead = FALSE;
    PCHAR   token_string = NULL;
    PCHAR   context = NULL;
    CONST   PCHAR delimeter = "\r\n";

    hFile = CreateFileW(
        MD5_DATABASE_FILE,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );

    if (hFile == INVALID_HANDLE_VALUE)
    {
        wprintf(L"Can't open md5 database file: ");
        return FALSE;
    }

    FileSize = GetFileSize(hFile, NULL);
    if (FileSize == 0 || FileSize == INVALID_FILE_SIZE)
    {

        CloseHandle(hFile);
        return FALSE;
    }

    pData = (PBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (SIZE_T)FileSize);
    if (pData == NULL)
    {

        CloseHandle(hFile);
        return FALSE;
    }

    bRead = ReadFile(hFile, pData, FileSize, &dwReaded, NULL);
    if (bRead != TRUE || dwReaded != FileSize)
    {

        HeapFree(GetProcessHeap(), 0, pData);
        CloseHandle(hFile);
        return FALSE;
    }


    token_string = (PCHAR)strtok_s(pData, delimeter, &context);
    if (token_string == NULL)
    {

        HeapFree(GetProcessHeap(), 0, pData);
        CloseHandle(hFile);
    return FALSE;
    }

    do {

        printf("%s\n", token_string);

    } while (token_string = (PCHAR)strtok_s(NULL, delimeter, &context));


    HeapFree(GetProcessHeap(), 0, pData);
    CloseHandle(hFile);
    return TRUE;
    }

    int main(void)
    {
    WCHAR* MD5_DATABASE_FILE = L"c:\\md5.txt";

    TestMD5(MD5_DATABASE_FILE);


    }

When I run exe this gives me a incorrect data. Content of md5.txt (DC288E0B39EA16B4E9455F82FF265A67:1213:TestDBG + (\r\n)

output:

D:\repos\TestWindbg\x64\Debug>TestWindbg.exe DC288E0B39EA16B4E9455F82FF265A67:1213:TestDBG áááááááááááááááá

I open exe in windbg and I saw while(token_string) is not NULL after first time. But is must?

WinDbg image: " https://i.ibb.co/60nHk5S/Untitled.png "

What is problem? Thanks for reading

I just change some types of pData and token_string .

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>


BOOL TestMD5(CONST WCHAR* MD5_DATABASE_FILE)
{
    HANDLE  hFile = INVALID_HANDLE_VALUE;
    DWORD   FileSize = 0;
    DWORD   dwReaded = 0;
    char*   pData = NULL;
    BOOL    bRead = FALSE;
    char*   token_string = NULL;
    PCHAR   context = NULL;
    CONST   PCHAR delimeter = "\r\n";

    hFile = CreateFileW(
        MD5_DATABASE_FILE,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );

    if (hFile == INVALID_HANDLE_VALUE)
    {
        wprintf(L"Can't open md5 database file: ");
        return FALSE;
    }

    FileSize = GetFileSize(hFile, NULL);
    if (FileSize == 0 || FileSize == INVALID_FILE_SIZE)
    {

        CloseHandle(hFile);
        return FALSE;
    }

    pData = (char*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(SIZE_T)FileSize + 1);
    if (pData == NULL)
    {

        CloseHandle(hFile);
        return FALSE;
    }

    bRead = ReadFile(hFile, pData, FileSize, &dwReaded, NULL);
    if (bRead != TRUE || dwReaded != FileSize)
    {
        HeapFree(GetProcessHeap(), 0, pData);
        CloseHandle(hFile);
        return FALSE;
    }


    token_string = strtok_s(pData, delimeter, &context);
    if (token_string == NULL)
    {

        HeapFree(GetProcessHeap(), 0, pData);
        CloseHandle(hFile);
        return FALSE;
    }

    do {

        printf("%s\n", token_string);

    } while (token_string = strtok_s(NULL, delimeter, &context));


    HeapFree(GetProcessHeap(), 0, pData);
    CloseHandle(hFile);
    return TRUE;
}

int main(void)
{
    WCHAR* MD5_DATABASE_FILE = L"c:\\md5.txt";

    TestMD5(MD5_DATABASE_FILE);

}

Output: DC288E0B39EA16B4E9455F82FF265A67:1213:TestDBG + (\r\n)

Jeffrey Shao - MSFT:Thank you for reply but this is not solution(but I changed my code PBYTE TO PCHAR). The problem is that strtok_s is a string function for this reason you must add NULL byte after buff. Like HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(SIZE_T)FileSize + 1) #1 for NULL character. HeapAlloc alloc buff size:FileSize and +1 For Null...

Thanks for blabb and Daniel Sęk:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM