狀況
當您執行應用程式呼叫_sntscanf_s函式時,呼叫應該會傳回EOF(-1)值。不過,當您執行應用程式在 Windows 10 企業 2015 LTSB 上時,插入的 null 字元會被忽略。這會導致接收錯誤的數值為1 ,而不是EOF (-1)值的 [ numFields ] 欄位。
例如,下列的 Visual C++ 的應用程式會發生這個問題:
#include "stdafx.h"
#include "string.h"
#include "stdlib.h"
int main()
{
TCHAR szBuff[256];
double temp = 0;
wcscpy_s(szBuff, L"77777777");
// this should force the _sntscanf_s to return an EOF (-1) value
szBuff[0] = '\0';
int numFields = _sntscanf_s(szBuff, _countof(szBuff), _T("%lf"), &temp);
if (numFields == EOF)
{
printf("Correct Behavior!!!\r\n");
}
else
{
printf("BUG: _sntscanf_s failed to return EOF!!!\r\n");
}
return 0;
}
原因
這個問題起因於 Windows 10 企業 2015 LTSB 所隨附的 Ucrtbase.dll 檔案中的common_vsscanf函式中的已知問題。這個問題會影響整個scanf系列函式:
-
_vsscanf_l
-
vsscanf
-
_sscanf_l
-
sscanf
-
_vsscanf_s_l
-
vsscanf_s
-
_sscanfs_l
-
sscanf_s
-
_snscanf_l
-
_snscanf_s
-
_snscanf_s_l
-
_snscanf_s
-
_vswscanf_l
-
vswscanf
-
_swscanf_l
-
swscanf
-
_vswscanf_s_l
-
vswscanf_s
-
_wsscanfs_l
-
swscanf_s
-
_snwscanf_l
-
_snwscanf_s
-
_snwscanf_s_l
-
_snwscanf_s
解決方案
如果要解決這個問題,安裝服務更新2018 月 8,-KB4103716 (OS 建置 10240.17861)。