2016-04-01 54 views
1

看起來像SAL錯誤。代碼:代碼分析不理解_In_opt_參數註釋?

PAAFILEFILTER_PROTECTED_FILE curFile = NULL; 

    try 
     { 
      status = GetProtectedFile(FileIdInfo, instanceContext, &curFile); 
      if(!NT_SUCCESS(status)) 
      { 
       TraceError("Can't GetProtectedFile with status: %!STATUS!\n", status); 
       leave; 
      } 
     ... 


    finally 
    { 
     if(NT_SUCCESS(status)) 
     { 
      LogMessage(AAFILEFILTER_FILE_UNPROTECTED, NULL, NULL, NULL, 0, (PUCHAR)FileIdInfo, sizeof(AAFILE_ID_INFORMATION)); 
     } 
     else 
     { 
      TraceProtectedFile(curFile); 
     } 
    } 

和代碼觀給我C6102 - Using variable from failed function call

在行TraceProtectedFile(curFile);但TraceProtectedFile有原型

_In_opt_ PAAFILEFILTER_PROTECTED_FILE protectedFile 

_In_opt_意味着"_In_opt_ is the same as _In_, except that the input parameter is allowed to be NULL and, therefore, the function should check for this." ..如果CA不能處理這樣簡單的事情不那麼已瞭解什麼可以:(

回答

1

這看起來與方式錯誤處理問題是結構化的,而不是_In_opt_參數。

,我不會感到驚訝,如果leave,與標準的C++異常處理混合,混淆SAL不夠,它沒有認識到finally將永遠不會被擊中。leave不是標準C++異常的一部分,而是MSVC特定的,用於structured exception handling

好處是,SAL的混淆暗示着其他開發人員也會因此類似的錯誤處理而感到驚訝。你或許應該考慮移動電話GetProtectedFiletry/finally之外,因爲所有的代碼都假定curFile被初始化成功:

PAAFILEFILTER_PROTECTED_FILE curFile = NULL; 

status = GetProtectedFile(FileIdInfo, instanceContext, &curFile); 
if(!NT_SUCCESS(status)) 
{ 
    TraceError("Can't GetProtectedFile with status: %!STATUS!\n", status); 
    return; // Return whatever is appropriate here 
} 

// The rest of your code can assume curFile initialized successfully 

try 
{ 
    ... 
} 
finally 
{ 
    if(NT_SUCCESS(status)) 
    { 
     LogMessage(AAFILEFILTER_FILE_UNPROTECTED, NULL, NULL, NULL, 0, (PUCHAR)FileIdInfo, sizeof(AAFILE_ID_INFORMATION)); 
    } 
    else 
    { 
     TraceProtectedFile(curFile); 
    } 
} 
+0

同意你的看法......這是更清潔。但仍然這是CA錯誤:) C6102絕不能爲_In_opt_參數恕我直言。 –