Fix issues reported by PREfast static analysis tool (#24577)
authorJan Kotas <jkotas@microsoft.com>
Wed, 15 May 2019 02:07:53 +0000 (19:07 -0700)
committerGitHub <noreply@github.com>
Wed, 15 May 2019 02:07:53 +0000 (19:07 -0700)
21 files changed:
src/binder/applicationcontext.cpp
src/binder/clrprivbinderassemblyloadcontext.cpp
src/binder/inc/applicationcontext.hpp
src/binder/inc/assemblyidentity.hpp
src/binder/inc/fusionassemblyname.hpp
src/binder/inc/stringlexer.inl
src/classlibnative/bcltype/arraynative.cpp
src/debug/ildbsymlib/symwrite.cpp
src/gc/env/gcenv.os.h
src/gc/env/gcenv.unix.inl
src/gc/env/gcenv.windows.inl
src/gc/gc.cpp
src/gc/handletablecore.cpp
src/gc/softwarewritewatch.h
src/gcinfo/gcinfoencoder.cpp
src/inc/metadatatracker.h
src/inc/utilcode.h
src/vm/classcompat.cpp
src/vm/method.hpp
src/vm/methodtablebuilder.cpp
src/zap/zapimage.cpp

index dc7b9ad..f768ab9 100644 (file)
@@ -144,8 +144,6 @@ namespace BINDER_SPACE
 
             m_pFailureCache = pFailureCache;
         }
-
-        m_fCanExplicitlyBindToNativeImages = false;
         
     Exit:
         BINDER_LOG_LEAVE_HR(W("ApplicationContext::Init"), hr);
index 03819b8..1681115 100644 (file)
@@ -216,10 +216,7 @@ HRESULT CLRPrivBinderAssemblyLoadContext::SetupContext(DWORD      dwAppDomainId,
             {
                 // Save the reference to the AppDomain in which the binder lives
                 pBinder->m_appContext.SetAppDomainId(dwAppDomainId);
-                
-                // Mark that this binder can explicitly bind to native images
-                pBinder->m_appContext.SetExplicitBindToNativeImages(true);
-                
+
                 // Save reference to the TPABinder that is required to be present.
                 _ASSERTE(pTPABinder != NULL);
                 pBinder->m_pTPABinder = pTPABinder;
index cd1a245..83893f3 100644 (file)
@@ -173,19 +173,6 @@ namespace BINDER_SPACE
 
         SimpleNameToFileNameMap * m_pTrustedPlatformAssemblyMap;
         TpaFileNameHash    * m_pFileNameHash;
-        
-        bool m_fCanExplicitlyBindToNativeImages;
-public:        
-        inline void SetExplicitBindToNativeImages(bool fCanExplicitlyBindToNativeImages)
-        {
-            m_fCanExplicitlyBindToNativeImages = fCanExplicitlyBindToNativeImages;
-        }
-        
-        inline bool CanExplicitlyBindToNativeImages()
-        {
-            return m_fCanExplicitlyBindToNativeImages;
-        }
-protected:        
     };
 
 #include "applicationcontext.inl"
index 4d7c7f1..16a4484 100644 (file)
@@ -40,7 +40,7 @@ namespace BINDER_SPACE
             IDENTITY_FLAG_CONTENT_TYPE           = 0x800,
             IDENTITY_FLAG_FULL_NAME              = (IDENTITY_FLAG_SIMPLE_NAME |
                                                     IDENTITY_FLAG_VERSION)
-        } IdentityFlags;
+        };
 
         AssemblyIdentity()
         {
index f0f69d3..fcfea95 100644 (file)
@@ -52,8 +52,6 @@ private:
     LPWSTR       _pwzTextualIdentity;
     LPWSTR       _pwzTextualIdentityILFull;
 
-    DWORD _dw;
-
 public:
     // IUnknown methods
     STDMETHODIMP            QueryInterface(REFIID riid,void ** ppv);
index f8bc381..a3954bd 100644 (file)
@@ -129,7 +129,7 @@ WCHAR StringLexer::DecodeUTF16Character()
     // See http://www.ietf.org/rfc/rfc2781.txt for details on UTF-16 encoding.
 
     WCHAR wcCurrentChar = 0;
-    SIZE_T nCharacters = m_end - m_cursor + 1;
+    SCOUNT_T nCharacters = m_end - m_cursor + 1;
     WCHAR wcChar1 = GetRawCharacter();
 
     if (wcChar1 < 0xd800)
index 528105a..605cd29 100644 (file)
@@ -591,7 +591,7 @@ void ArrayNative::UnBoxEachElement(BASEARRAYREF pSrc, unsigned int srcIndex, BAS
     MethodTable * pDestMT = destTH.GetMethodTable();
     PREFIX_ASSUME(pDestMT != NULL);
 
-    const unsigned int destSize = pDestMT->GetNumInstanceFieldBytes();
+    SIZE_T destSize = pDest->GetComponentSize();
     BYTE* srcData = (BYTE*) pSrc->GetDataPtr() + srcIndex * sizeof(OBJECTREF);
     BYTE* data = (BYTE*) pDest->GetDataPtr() + destIndex * destSize;
 
index 4d3fb53..80b09a7 100644 (file)
@@ -202,7 +202,7 @@ HRESULT SymWriter::GetOrCreateDocument(
 {
     ULONG UrlEntry;
     DWORD strLength = WszWideCharToMultiByte(CP_UTF8, 0, wcsUrl, -1, 0, 0, 0, 0);
-    LPSTR multiByteURL = (LPSTR) new char [strLength+1];
+    LPSTR multiByteURL = (LPSTR) new char [strLength];
     HRESULT hr  = S_OK;
 
     if (multiByteURL == NULL)
@@ -210,7 +210,7 @@ HRESULT SymWriter::GetOrCreateDocument(
         return E_OUTOFMEMORY;
     }
 
-    WszWideCharToMultiByte(CP_UTF8, 0, wcsUrl, -1, multiByteURL, strLength+1, 0, 0);
+    WszWideCharToMultiByte(CP_UTF8, 0, wcsUrl, -1, multiByteURL, strLength, 0, 0);
 
     if (m_pStringPool->FindString(multiByteURL, &UrlEntry) == S_FALSE) // no file of that name has been seen before
     {
index 7fa1ba7..393bd1f 100644 (file)
@@ -444,7 +444,7 @@ public:
     static void GetMemoryStatus(uint32_t* memory_load, uint64_t* available_physical, uint64_t* available_page_file);
 
     // Get size of an OS memory page
-    static uint32_t GetPageSize();
+    static size_t GetPageSize();
 
     //
     // Misc
index 50683ae..42b8a43 100644 (file)
@@ -11,7 +11,7 @@ extern uint32_t g_pageSizeUnixInl;
 
 #define OS_PAGE_SIZE GCToOSInterface::GetPageSize()
 
-__forceinline uint32_t GCToOSInterface::GetPageSize()
+__forceinline size_t GCToOSInterface::GetPageSize()
 {
     return g_pageSizeUnixInl;
 }
index 3b15dfd..7e81016 100644 (file)
@@ -10,7 +10,7 @@
 
 #define OS_PAGE_SIZE GCToOSInterface::GetPageSize()
 
-__forceinline uint32_t GCToOSInterface::GetPageSize()
+__forceinline size_t GCToOSInterface::GetPageSize()
 {
     return 0x1000;
 }
index e8f2400..c6a858c 100644 (file)
@@ -28077,15 +28077,7 @@ BOOL gc_heap::find_card(uint32_t* card_table,
             {
                 card_word_value = *(++last_card_word);
             } while ((last_card_word < &card_table [card_word_end]) &&
-
-#ifdef _MSC_VER
-                     (card_word_value == (1 << card_word_width)-1)
-#else
-                     // if left shift count >= width of type,
-                     // gcc reports error.
-                     (card_word_value == ~0u)
-#endif // _MSC_VER
-                );
+                     (card_word_value == ~0u /* (1 << card_word_width)-1 */));
             bit_position = 0;
         }
     } while (card_word_value & 1);
@@ -30477,7 +30469,7 @@ size_t gc_heap::joined_youngest_desired (size_t new_allocation)
             uint32_t memory_load = 0;
             get_memory_info (&memory_load);
             settings.exit_memory_load = memory_load;
-            dprintf (2, ("Current emory load: %d", memory_load));
+            dprintf (2, ("Current memory load: %d", memory_load));
 
             size_t final_total = 
                 trim_youngest_desired (memory_load, total_new_allocation, total_min_allocation);
@@ -37418,7 +37410,7 @@ void GCHeap::DiagScanDependentHandles (handle_scan_fn fn, int gen_number, ScanCo
 // Go through and touch (read) each page straddled by a memory block.
 void TouchPages(void * pStart, size_t cb)
 {
-    const uint32_t pagesize = OS_PAGE_SIZE;
+    const size_t pagesize = OS_PAGE_SIZE;
     _ASSERTE(0 == (pagesize & (pagesize-1))); // Must be a power of 2.
     if (cb)
     {
index 7e531f9..5fc661c 100644 (file)
@@ -1409,7 +1409,7 @@ uint32_t SegmentInsertBlockFromFreeListWorker(TableSegment *pSegment, uint32_t u
                 void * pvCommit = pSegment->rgValue + (uCommitLine * HANDLE_HANDLES_PER_BLOCK);
 
                 // we should commit one more page of handles
-                uint32_t dwCommit = OS_PAGE_SIZE;
+                size_t dwCommit = OS_PAGE_SIZE;
 
                 // commit the memory
                 if (!GCToOSInterface::VirtualCommit(pvCommit, dwCommit))
@@ -1774,7 +1774,7 @@ BOOL DoesSegmentNeedsToTrimExcessPages(TableSegment *pSegment)
     if (uEmptyLine < uDecommitLine)
     {
         // derive some useful info about the page size
-        uintptr_t dwPageRound = (uintptr_t)OS_PAGE_SIZE - 1;
+        uintptr_t dwPageRound = OS_PAGE_SIZE - 1;
         uintptr_t dwPageMask  = ~dwPageRound;
 
         // compute the address corresponding to the empty line
index e59fd61..92ffbf8 100644 (file)
@@ -8,7 +8,7 @@
 #include "gcinterface.h"
 #include "gc.h"
 
-#define WRITE_WATCH_UNIT_SIZE 0x1000
+#define WRITE_WATCH_UNIT_SIZE ((size_t)0x1000)
 
 #ifdef FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
 #ifndef DACCESS_COMPILE
index 090c089..bb989d6 100644 (file)
@@ -1802,7 +1802,7 @@ void GcInfoEncoder::Build()
         const size_t sizeofEncodedNumBitsPerPointer = BitStreamWriter::SizeofVarLengthUnsigned(numBitsPerPointer, POINTER_SIZE_ENCBASE);
         const size_t sizeofNoIndirection = m_NumCallSites * (numRegisters + numStackSlots);
         const size_t sizeofIndirection = sizeofEncodedNumBitsPerPointer  // Encode the pointer sizes
-                                         + (m_NumCallSites * numBitsPerPointer) // Encoe the pointers
+                                         + (m_NumCallSites * numBitsPerPointer) // Encode the pointers
                                          + 7 // Up to 7 bits of alignment padding
                                          + sizeofSets; // Encode the actual live sets
 
index 1427b18..3b0059b 100644 (file)
@@ -71,8 +71,7 @@ public:
 
         m_ModuleName = NULL;
 
-        DWORD len = (DWORD)wcslen(modName);
-        _ASSERTE(len + 1 != 0);      // Prevent Overflow
+        size_t len = wcslen(modName);
         m_ModuleName = new wchar_t[len + 1];
         NewArrayHolder<wchar_t> moduleNameHolder(m_ModuleName);
         wcscpy_s((wchar_t *)m_ModuleName, len + 1, (wchar_t *)modName);
index cf4b8dd..17c8b14 100644 (file)
@@ -1899,12 +1899,12 @@ public:
     {
         WRAPPER_NO_CONTRACT;
         _ASSERTE(iIndex < m_iCount);
-        return ((void *) ((size_t) Ptr() + (iIndex * m_iElemSize)));
+        return (BYTE*) Ptr() + (iIndex * (size_t)m_iElemSize);
     }
-    int Size()
+    size_t Size()
     {
         LIMITED_METHOD_CONTRACT;
-        return (m_iCount * m_iElemSize);
+        return (m_iCount * (size_t)m_iElemSize);
     }
     int Count()
     {
@@ -2408,7 +2408,7 @@ protected:
     HASHENTRY *EntryPtr(ULONG iEntry)
     {
         LIMITED_METHOD_DAC_CONTRACT;
-        return (PTR_HASHENTRY(m_pcEntries + (iEntry * m_iEntrySize)));
+        return (PTR_HASHENTRY(m_pcEntries + (iEntry * (size_t)m_iEntrySize)));
     }
 
     ULONG     ItemIndex(HASHENTRY *p)
@@ -2859,7 +2859,7 @@ void CHashTableAndData<MemMgr>::InitFreeChain(
     BYTE* pcPtr;
     _ASSERTE(iEnd > iStart);
 
-    pcPtr = (BYTE*)m_pcEntries + iStart * m_iEntrySize;
+    pcPtr = (BYTE*)m_pcEntries + iStart * (size_t)m_iEntrySize;
     for (++iStart; iStart < iEnd; ++iStart)
     {
         ((FREEHASHENTRY *) pcPtr)->iFree = iStart;
@@ -3117,13 +3117,13 @@ class CClosedHashBase
     BYTE *EntryPtr(int iEntry)
     {
         LIMITED_METHOD_CONTRACT;
-        return (m_rgData + (iEntry * m_iEntrySize));
+        return (m_rgData + (iEntry * (size_t)m_iEntrySize));
     }
 
     BYTE *EntryPtr(int iEntry, BYTE *rgData)
     {
         LIMITED_METHOD_CONTRACT;
-        return (rgData + (iEntry * m_iEntrySize));
+        return (rgData + (iEntry * (size_t)m_iEntrySize));
     }
 
 public:
index 473e328..b677045 100644 (file)
@@ -3290,7 +3290,7 @@ HRESULT MethodTableBuilder::FindMethodDeclarationForMethodImpl(
         if (TypeFromToken(typeref) == mdtMethodDef)
         {
             mdTypeDef typeDef;
-            hr = pMDInternalImport->GetParentToken(typeref, &typeDef);
+            IfFailRet(pMDInternalImport->GetParentToken(typeref, &typeDef));
 
             // Make sure it is a typedef
             if (TypeFromToken(typeDef) != mdtTypeDef)
index 68626c7..cf532f8 100644 (file)
@@ -210,16 +210,13 @@ class MethodDesc
 
 public:
 
-    enum
-    {
 #ifdef _WIN64
-        ALIGNMENT_SHIFT = 3,
+    static const int ALIGNMENT_SHIFT = 3;
 #else
-        ALIGNMENT_SHIFT = 2,
+    static const int ALIGNMENT_SHIFT = 2;
 #endif
-        ALIGNMENT       = (1<<ALIGNMENT_SHIFT),
-        ALIGNMENT_MASK  = (ALIGNMENT-1)
-    };
+    static const size_t ALIGNMENT = (1 << ALIGNMENT_SHIFT);
+    static const size_t ALIGNMENT_MASK = (ALIGNMENT - 1);
 
 #ifdef _DEBUG 
 
index 609eda0..e8a7e93 100644 (file)
@@ -2434,7 +2434,7 @@ HRESULT MethodTableBuilder::FindMethodDeclarationForMethodImpl(
         if (TypeFromToken(typeref) == mdtMethodDef)
         {   // If parent is a method def then this is a varags method
             mdTypeDef typeDef;
-            hr = pMDInternalImport->GetParentToken(typeref, &typeDef);
+            IfFailRet(pMDInternalImport->GetParentToken(typeref, &typeDef));
 
             if (TypeFromToken(typeDef) != mdtTypeDef)
             {   // A mdtMethodDef must be parented by a mdtTypeDef
index 196337d..2dce179 100644 (file)
@@ -2238,20 +2238,20 @@ BOOL ZapImage::ShouldCompileMethodDef(mdMethodDef md)
     mdToken tkExtends;
     if (td != mdTypeDefNil)
     {
-        m_pMDImport->GetTypeDefProps(td, NULL, &tkExtends);
+        IfFailThrow(m_pMDImport->GetTypeDefProps(td, NULL, &tkExtends));
         
         mdAssembly tkAssembly;
         DWORD dwAssemblyFlags;
         
-        m_pMDImport->GetAssemblyFromScope(&tkAssembly);
+        IfFailThrow(m_pMDImport->GetAssemblyFromScope(&tkAssembly));
         if (TypeFromToken(tkAssembly) == mdtAssembly)
         {
-            m_pMDImport->GetAssemblyProps(tkAssembly,
+            IfFailThrow(m_pMDImport->GetAssemblyProps(tkAssembly,
                                             NULL, NULL,     // Public Key
                                             NULL,           // Hash Algorithm
                                             NULL,           // Name
                                             NULL,           // MetaData
-                                            &dwAssemblyFlags);
+                                            &dwAssemblyFlags));
             
             if (IsAfContentType_WindowsRuntime(dwAssemblyFlags))
             {
@@ -2259,7 +2259,7 @@ BOOL ZapImage::ShouldCompileMethodDef(mdMethodDef md)
                 {
                     LPCSTR szNameSpace = NULL;
                     LPCSTR szName = NULL;
-                    m_pMDImport->GetNameOfTypeRef(tkExtends, &szNameSpace, &szName);
+                    IfFailThrow(m_pMDImport->GetNameOfTypeRef(tkExtends, &szNameSpace, &szName));
                     
                     if (!strcmp(szNameSpace, "System") && !_stricmp((szName), "Attribute"))
                     {