无法理解VS2015分析仪报告

Can't understand VS2015 Analyzer report

本文关键字:分析仪 报告 VS2015      更新时间:2023-10-16

分析器报告某个参数未初始化。我不明白为什么。

代码:

LPTSTR buffer = NULL;
DWORD reqSize = 16000;
DWORD dataType;
LPTSTR * array;
DWORD szChars;
BOOL bRegProp;
// Allocate buffer according to required size
buffer = new TCHAR[(reqSize /sizeof(TCHAR))+2];
if(!buffer)
    return NULL;
// Get the string into the buffer 
if (FALSE == SetupDiGetDeviceRegistryProperty(Devs, DevInfo, Prop, &dataType, (LPBYTE)buffer, reqSize, &reqSize))
    return NULL;
szChars = reqSize/sizeof(TCHAR);
buffer[szChars] = TEXT('');

分析仪投诉有:

  1. "buffer"未初始化
  2. 已使用"buffer",但可能尚未初始化

现在,根据这个函数的SAL注释,您需要确保它不会返回false:

_Success_(return != FALSE)
_When_((*PropertyRegDataType == REG_SZ), _At_((PSTR) PropertyBuffer,   _Post_valid_))
_When_((*PropertyRegDataType == REG_MULTI_SZ), _At_((PZZSTR) PropertyBuffer, _Post_valid_))
WINSETUPAPI
BOOL
WINAPI
SetupDiGetDeviceRegistryPropertyA(
  _In_ HDEVINFO DeviceInfoSet,
  _In_ PSP_DEVINFO_DATA DeviceInfoData,
  _In_ DWORD Property,
  _Out_opt_ PDWORD PropertyRegDataType, 
  _Out_writes_bytes_to_opt_(PropertyBufferSize, *RequiredSize) PBYTE PropertyBuffer,
  _In_ DWORD PropertyBufferSize,
  _Out_opt_ PDWORD RequiredSize 
);

也许我错过了"何时"这件事?

我认为您需要检查dataType是否为REG_SZ(如有必要,请检查REG_MULTI_SZ)。

"when"子句表示"如果dataType是REG_SZ,则buffer将已初始化"。。。但是分析器不知道它不是存储在reqSize中的REG_DWORD(是的,I知道这不是函数存储REG_DWORDs的地方,但分析器不知道)。