strtok_s return windbg 中的数据不正确

strtok_s return incorrect data inside windbg

(大家好)我对 strtok_s 有一些疑问。我写了这段代码(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);


    }

当我 运行 exe 时,这给了我一个不正确的数据。 md5.txt (DC288E0B39EA16B4E9455F82FF265A67:1213:TestDBG + (\r\n)

的内容

输出:

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

我在 windbg 中打开 exe,我第一次看到 while(token_string) 不是 NULL。但是是必须的吗?

WinDbg 图像:“https://i.ibb.co/60nHk5S/Untitled.png

有什么问题?感谢阅读

我只是更改了一些类型的 pDatatoken_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);

}

输出: DC288E0B39EA16B4E9455F82FF265A67:1213:TestDBG + (\r\n)

Jeffrey Shao - MSFT:感谢您的回复,但这不是解决方案(但我将代码 PBYTE 更改为 PCHAR)。问题是 strtok_s 是一个字符串函数,因此您必须在 buff 之后添加 NULL 字节。像 HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(SIZE_T)FileSize + 1) #1 代表 NULL 字符。 HeapAlloc alloc buff size:FileSize 和 +1 For Null...

感谢 blabb 和丹尼尔塞克: