Fix static analysis issues (#11466)
authorKoundinya Veluri <kouvel@microsoft.com>
Tue, 9 May 2017 20:56:40 +0000 (13:56 -0700)
committerGitHub <noreply@github.com>
Tue, 9 May 2017 20:56:40 +0000 (13:56 -0700)
Fix static analysis issues

13 files changed:
src/debug/shared/amd64/primitives.cpp
src/dlls/mscorpe/iceefilegen.cpp
src/ilasm/prebuilt/asmparse.cpp
src/inc/winrt/paraminstanceapi.h
src/md/ceefilegen/cceegen.cpp
src/md/enc/mdinternalrw.cpp
src/vm/ceeload.cpp
src/vm/corhost.cpp
src/vm/dwreport.cpp
src/vm/jitinterface.cpp
src/vm/methodtable.h
src/zap/zapheaders.cpp
src/zap/zapimage.cpp

index fb5d95b0d6f9f62dd698b96694f2bc1541cc094a..6fead570cf5e4e72f9abf8fa6a427bf1e40e2f79 100644 (file)
@@ -63,7 +63,7 @@ void CORDbgCopyThreadContext(DT_CONTEXT* pDst, const DT_CONTEXT* pSrc)
     if ((dstFlags & srcFlags & CONTEXT_FLOATING_POINT) == CONTEXT_FLOATING_POINT)
     {
         // Xmm0-Xmm15
-        CopyContextChunk(&(pDst->Xmm0), &(pSrc->Xmm0), &(pDst->Xmm15) + sizeof(M128A),
+        CopyContextChunk(&(pDst->Xmm0), &(pSrc->Xmm0), &(pDst->Xmm15) + 1,
                          CONTEXT_FLOATING_POINT);
 
         // MxCsr
index f4323b9e8c670209f88d4293bb216173db6a864d..c48ae7e09406a4a09a6fc9f164670bc1f234cb75 100644 (file)
@@ -151,7 +151,9 @@ HRESULT ICeeFileGen::CreateCeeFileFromICeeGen(ICeeGen *pICeeGen, HCEEFILE *ceeFi
         return E_POINTER;
     CCeeGen *genFrom = reinterpret_cast<CCeeGen*>(pICeeGen);
     CeeFileGenWriter *gen = NULL;
-    if (FAILED(CeeFileGenWriter::CreateNewInstance(genFrom, gen, createFlags))) return FALSE;
+    HRESULT hr = CeeFileGenWriter::CreateNewInstance(genFrom, gen, createFlags);
+    if (FAILED(hr))
+        return hr;
     TESTANDRETURN(gen != NULL, E_OUTOFMEMORY);
     *ceeFile = gen;
     return S_OK;
index b3571c72b75420cc663713e46245cf6d035a2145..389546edb28746f5f61d7d6bb88b3a2253cd06b1 100644 (file)
@@ -1834,9 +1834,16 @@ YYSTATIC char    *yyscpy(register char*t, register char*f)
 
        YYSTATIC short  yyn;
        YYSTATIC short  yystate = 0;
-       YYSTATIC short  *yyps= &yys[-1];
+#ifdef _PREFAST_
+#pragma warning(push)
+#pragma warning(disable: 6200) // Index '-1' is out of valid index range...for non-stack buffer...
+#endif
+    YYSTATIC short     *yyps= &yys[-1];
        YYSTATIC YYSTYPE        *yypv= &yyv[-1];
-       YYSTATIC short  yyj;
+#ifdef _PREFAST_
+#pragma warning(pop)
+#endif
+    YYSTATIC short     yyj;
        YYSTATIC short  yym;
 
 #endif
index 062c7f3d08ff3e3a739dd01d73fe8b42911b2d0d..81ee4c51c908d9df1795089137861e86dfea8069 100644 (file)
@@ -1642,7 +1642,14 @@ namespace Ro { namespace detail {
             DWORD dwcb;
             DWORD dwcbResult;
 
+#ifdef _PREFAST_
+#pragma warning(push)
+#pragma warning(disable: 33098) // "Banned hash algorithm is used" - SHA-1 is required for compatibility
+#endif // _PREFAST_
             CHKNT(BCryptOpenAlgorithmProvider(&_hAlg, BCRYPT_SHA1_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0));
+#ifdef _PREFAST_
+#pragma warning(pop)
+#endif // _PREFAST_
 
             CHKNT(BCryptGetProperty(_hAlg, BCRYPT_OBJECT_LENGTH, reinterpret_cast<PBYTE>(&dwcb), sizeof(dwcb), &dwcbResult, 0));
 
index 0cf0780d150314abba612400804df8db81361db3..bd69c8daed41edfb630fa274007726f0efad6edc 100644 (file)
@@ -38,22 +38,30 @@ HRESULT STDMETHODCALLTYPE CreateICeeGen(REFIID riid, void **pCeeGen)
 
 HRESULT CCeeGen::CreateNewInstance(CCeeGen* & pGen) // static, public
 {
-    pGen = new CCeeGen();
-    _ASSERTE(pGen != NULL);
-    TESTANDRETURNMEMORY(pGen);
+    NewHolder<CCeeGen> pGenHolder(new CCeeGen());
+    _ASSERTE(pGenHolder != NULL);
+    TESTANDRETURNMEMORY(pGenHolder);
     
-    pGen->m_peSectionMan = new PESectionMan;    
-    _ASSERTE(pGen->m_peSectionMan != NULL);
-    TESTANDRETURNMEMORY(pGen->m_peSectionMan);
+    pGenHolder->m_peSectionMan = new PESectionMan;    
+    _ASSERTE(pGenHolder->m_peSectionMan != NULL);
+    TESTANDRETURNMEMORY(pGenHolder->m_peSectionMan);
 
-    HRESULT hr = pGen->m_peSectionMan->Init();
-    TESTANDRETURNHR(hr);
+    HRESULT hr = pGenHolder->m_peSectionMan->Init();
+    if (FAILED(hr))
+    {
+        pGenHolder->Cleanup();
+        return hr;
+    }
 
-    hr = pGen->Init();
-    TESTANDRETURNHR(hr);
+    hr = pGenHolder->Init();
+    if (FAILED(hr))
+    {
+        // Init() calls Cleanup() on failure
+        return hr;
+    }
 
+    pGen = pGenHolder.Extract();
     return hr;
-
 }
 
 STDMETHODIMP CCeeGen::QueryInterface(REFIID riid, void** ppv)
index 02fb40735807154a2ef1306a5f99aa84b087644b..75c793967edc15f7ab9d09d815afa27fb5ad3043 100644 (file)
@@ -2393,7 +2393,7 @@ HRESULT MDInternalRW::GetItemGuid(      // return hresult
 
     // Get the GUID, if any.
     hr = GetCustomAttributeByName(tkObj, INTEROP_GUID_TYPE, (const void**)&pBlob, &cbBlob);
-    if (hr != S_FALSE)
+    if (SUCCEEDED(hr) && hr != S_FALSE)
     {
         // Should be in format.  Total length == 41
         // <0x0001><0x24>01234567-0123-0123-0123-001122334455<0x0000>
index 5de7114eb11b0d35be87553afa8609d21c2a3dd0..41ea693d0329599817e9e4c9ec31e0050f4fe01e 100644 (file)
@@ -12770,6 +12770,11 @@ void Module::LogTokenAccess(mdToken token, SectionFormat format, ULONG flagnum)
     if (!m_nativeImageProfiling)
         return;
 
+    if (flagnum >= CORBBTPROF_TOKEN_MAX_NUM_FLAGS)
+    {
+        return;
+    }
+
     mdToken rid = RidFromToken(token);
     CorTokenType  tkType  = (CorTokenType) TypeFromToken(token);
     SectionFormat tkKind  = (SectionFormat) (tkType >> 24);
@@ -12798,8 +12803,9 @@ void Module::LogTokenAccess(mdToken token, SectionFormat format, ULONG flagnum)
     else if (tkKind == (SectionFormat) (ibcMethodSpec >> 24))
         tkKind = IbcMethodSpecSection;
 
+    _ASSERTE(tkKind >= 0);
     _ASSERTE(tkKind < SectionFormatCount);
-    if (tkKind >= SectionFormatCount)
+    if (tkKind < 0 || tkKind >= SectionFormatCount)
     {
         return;
     }
index fd27a7a4e7fa9d5941ba66570899de809675585a..3f53de2acb83d2a3791098bc036c3196ccb4c617 100644 (file)
@@ -2570,7 +2570,7 @@ HRESULT CCLRErrorReportingManager::BucketParamsCache::SetAt(BucketParameterIndex
 {
     LIMITED_METHOD_CONTRACT;
 
-    if (index >= InvalidBucketParamIndex)
+    if (index < 0 || index >= InvalidBucketParamIndex)
     {
         _ASSERTE(!"bad bucket parameter index");
         return E_INVALIDARG;
index b95c59ff8da6a22ed3e52718462b1dd7e3019d9c..57d67e7c22bce2d9951b8d7d14f91f5681820594 100644 (file)
@@ -1526,30 +1526,28 @@ BOOL RunWatson(
         return false;
     }
 
+    {
+        BOOL ret = WszCreateProcess(watsonAppName,
+                                    watsonCommandLine,
+                                    NULL,
+                                    NULL,
+                                    TRUE,
+                                    NULL,
+                                    NULL,
+                                    NULL,
+                                    &startupInfo,
+                                    &processInformation);
+
+        if (FALSE == ret)
         {
-            BOOL ret = WszCreateProcess(watsonAppName,
-                                        watsonCommandLine,
-                                        NULL,
-                                        NULL,
-                                        TRUE,
-                                        NULL,
-                                        NULL,
-                                        NULL,
-                                        &startupInfo,
-                                        &processInformation);
-
-            if (FALSE == ret)
-            {
-                //
-                // Watson failed to start up.
-                //
-                // This can happen if e.g. Watson wasn't installed on the machine.
-                //
-                 return  E_FAIL;
-                 
-            }
-
+            //
+            // Watson failed to start up.
+            //
+            // This can happen if e.g. Watson wasn't installed on the machine.
+            //
+            return FALSE;
         }
+    }
 
     
 
index b67ab0c3978d1a2546b5bc93ec8d1fcf3eff9057..6de4163c69e0cc5e8c102c6baf3b909c6a29338e 100644 (file)
@@ -360,7 +360,7 @@ CorInfoType CEEInfo::asCorInfoType(CorElementType eeType,
     _ASSERTE((CorInfoType) map[ELEMENT_TYPE_PTR] == CORINFO_TYPE_PTR);
     _ASSERTE((CorInfoType) map[ELEMENT_TYPE_TYPEDBYREF] == CORINFO_TYPE_REFANY);
 
-    CorInfoType res = ((unsigned)eeType < ELEMENT_TYPE_MAX) ? ((CorInfoType) map[eeType]) : CORINFO_TYPE_UNDEF;
+    CorInfoType res = ((unsigned)eeType < ELEMENT_TYPE_MAX) ? ((CorInfoType) map[(unsigned)eeType]) : CORINFO_TYPE_UNDEF;
 
     if (clsRet)
         *clsRet = CORINFO_CLASS_HANDLE(typeHndUpdated.AsPtr());
index 1e557c4253d3a9ea881a96ba2993e8160b7f1936..2ce9f2a883454d17c214eb130c8027270f59fefb 100644 (file)
@@ -663,7 +663,7 @@ SystemVClassificationType CorInfoType2UnixAmd64Classification(CorElementType eeT
     _ASSERTE((SystemVClassificationType)toSystemVAmd64ClassificationTypeMap[ELEMENT_TYPE_TYPEDBYREF] == SystemVClassificationTypeTypedReference);
     _ASSERTE((SystemVClassificationType)toSystemVAmd64ClassificationTypeMap[ELEMENT_TYPE_BYREF] == SystemVClassificationTypeIntegerByRef);
 
-    return (((unsigned)eeType) < ELEMENT_TYPE_MAX) ? (toSystemVAmd64ClassificationTypeMap[eeType]) : SystemVClassificationTypeUnknown;
+    return (((unsigned)eeType) < ELEMENT_TYPE_MAX) ? (toSystemVAmd64ClassificationTypeMap[(unsigned)eeType]) : SystemVClassificationTypeUnknown;
 };
 
 #define SYSTEMV_EIGHT_BYTE_SIZE_IN_BYTES                    8 // Size of an eightbyte in bytes.
index acec36bf2adb2cb63a85780fb0dd5712fff18fe0..8960798981216cdd57fb59f7b1a801c7788d727a 100644 (file)
@@ -325,7 +325,7 @@ ZapPEExports::ZapPEExports(LPCWSTR dllPath)
 
 DWORD ZapPEExports::GetSize()
 {
-       return DWORD(sizeof(IMAGE_EXPORT_DIRECTORY) + wcslen(m_dllFileName) + 1);
+       return DWORD(sizeof(IMAGE_EXPORT_DIRECTORY) + wcslen(m_dllFileName) * sizeof(BYTE) + 1);
 }
 
 void ZapPEExports::Save(ZapWriter * pZapWriter)
index 27b46520be04d10a441019fe3e7f2117c6ee3bd1..61cf099898651ab4ec1b0edda6ee8950eb05abd0 100644 (file)
@@ -2620,6 +2620,12 @@ HRESULT ZapImage::parseProfileData()
         READ(entry,CORBBTPROF_SECTION_TABLE_ENTRY);
 
         SectionFormat format = sectionHeader->Entries[i].FormatID;
+        _ASSERTE(format >= 0);
+        if (format < 0)
+        {
+            continue;
+        }
+
         if (convertFromV1)
         {
             if (format < LastTokenFlagSection)