修正:VC++ scanf 函式在 Windows 10 企業版 2015 長期維護 上無法回傳 EOF (-1)

Applies To
Win 10 Ent LTSB 2015

症狀

當你執行呼叫 _sntscanf_s 函式的應用程式時,呼叫應該會回傳一個 EOF (-1) 值。 然而,當你在 Windows 10 企業版 2015 長期維護 執行應用程式時,注入的空字元會被忽略。 這會導致 numFields 欄位錯誤地獲得 1 的值,而非 EOF (-1) 值。

例如,以下 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 長期維護中包含的 Ucrtbase.dll 檔案中common_vsscanf函式的已知問題所引起。 此問題影響整個 掃描函 數族:

  • _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
  • VSSWSCANF
  • _swscanf_l
  • SWSCANF
  • _vswscanf_s_l
  • vswscanf_s
  • _wsscanfs_l
  • swscanf_s
  • _snwscanf_l
  • _snwscanf_s
  • _snwscanf_s_l
  • _snwscanf_s

解決方式

為了解決此問題,請 於 2018 年 5 月 8 日安裝 Servicing 更新—KB4103716 (作業系統建構版 10240.17861)

參考資料

CRT () 重構的偉大 C 執行時

介紹通用CRT