Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: core/fxcrt/fx_basic_maps.cpp

Issue 1991123002: Fix a potential nullptr deref in CFX_MapPtrToPtr. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: address comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fxcrt/fx_basic_list.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fxcrt/include/fx_basic.h" 7 #include "core/fxcrt/include/fx_basic.h"
8 #include "core/fxcrt/plex.h" 8 #include "core/fxcrt/plex.h"
9 9
10 CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize) 10 CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize)
11 : m_pHashTable(NULL), 11 : m_pHashTable(nullptr),
12 m_nHashTableSize(17), 12 m_nHashTableSize(17),
13 m_nCount(0), 13 m_nCount(0),
14 m_pFreeList(NULL), 14 m_pFreeList(nullptr),
15 m_pBlocks(NULL), 15 m_pBlocks(nullptr),
16 m_nBlockSize(nBlockSize) { 16 m_nBlockSize(nBlockSize) {
17 ASSERT(m_nBlockSize > 0); 17 ASSERT(m_nBlockSize > 0);
18 } 18 }
19
19 void CFX_MapPtrToPtr::RemoveAll() { 20 void CFX_MapPtrToPtr::RemoveAll() {
20 FX_Free(m_pHashTable); 21 FX_Free(m_pHashTable);
21 m_pHashTable = NULL; 22 m_pHashTable = nullptr;
22 m_nCount = 0; 23 m_nCount = 0;
23 m_pFreeList = NULL; 24 m_pFreeList = nullptr;
24 m_pBlocks->FreeDataChain(); 25 if (m_pBlocks) {
25 m_pBlocks = NULL; 26 m_pBlocks->FreeDataChain();
27 m_pBlocks = nullptr;
28 }
26 } 29 }
30
27 CFX_MapPtrToPtr::~CFX_MapPtrToPtr() { 31 CFX_MapPtrToPtr::~CFX_MapPtrToPtr() {
28 RemoveAll(); 32 RemoveAll();
29 ASSERT(m_nCount == 0); 33 ASSERT(m_nCount == 0);
30 } 34 }
31 uint32_t CFX_MapPtrToPtr::HashKey(void* key) const { 35 uint32_t CFX_MapPtrToPtr::HashKey(void* key) const {
32 return ((uint32_t)(uintptr_t)key) >> 4; 36 return ((uint32_t)(uintptr_t)key) >> 4;
33 } 37 }
34 void CFX_MapPtrToPtr::GetNextAssoc(FX_POSITION& rNextPosition, 38 void CFX_MapPtrToPtr::GetNextAssoc(FX_POSITION& rNextPosition,
35 void*& rKey, 39 void*& rKey,
36 void*& rValue) const { 40 void*& rValue) const {
(...skipping 19 matching lines...) Expand all
56 } 60 }
57 FX_BOOL CFX_MapPtrToPtr::Lookup(void* key, void*& rValue) const { 61 FX_BOOL CFX_MapPtrToPtr::Lookup(void* key, void*& rValue) const {
58 uint32_t nHash; 62 uint32_t nHash;
59 CAssoc* pAssoc = GetAssocAt(key, nHash); 63 CAssoc* pAssoc = GetAssocAt(key, nHash);
60 if (!pAssoc) { 64 if (!pAssoc) {
61 return FALSE; 65 return FALSE;
62 } 66 }
63 rValue = pAssoc->value; 67 rValue = pAssoc->value;
64 return TRUE; 68 return TRUE;
65 } 69 }
70
66 void* CFX_MapPtrToPtr::GetValueAt(void* key) const { 71 void* CFX_MapPtrToPtr::GetValueAt(void* key) const {
67 uint32_t nHash; 72 uint32_t nHash;
68 CAssoc* pAssoc = GetAssocAt(key, nHash); 73 CAssoc* pAssoc = GetAssocAt(key, nHash);
69 if (!pAssoc) { 74 return pAssoc ? pAssoc->value : nullptr;
70 return NULL;
71 }
72 return pAssoc->value;
73 } 75 }
76
74 void*& CFX_MapPtrToPtr::operator[](void* key) { 77 void*& CFX_MapPtrToPtr::operator[](void* key) {
75 uint32_t nHash; 78 uint32_t nHash;
76 CAssoc* pAssoc; 79 CAssoc* pAssoc;
77 if ((pAssoc = GetAssocAt(key, nHash)) == NULL) { 80 if ((pAssoc = GetAssocAt(key, nHash)) == NULL) {
78 if (!m_pHashTable) { 81 if (!m_pHashTable) {
79 InitHashTable(m_nHashTableSize); 82 InitHashTable(m_nHashTableSize);
80 } 83 }
81 pAssoc = NewAssoc(); 84 pAssoc = NewAssoc();
82 pAssoc->key = key; 85 pAssoc->key = key;
83 pAssoc->pNext = m_pHashTable[nHash]; 86 pAssoc->pNext = m_pHashTable[nHash];
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 150 }
148 void CFX_MapPtrToPtr::FreeAssoc(CFX_MapPtrToPtr::CAssoc* pAssoc) { 151 void CFX_MapPtrToPtr::FreeAssoc(CFX_MapPtrToPtr::CAssoc* pAssoc) {
149 pAssoc->pNext = m_pFreeList; 152 pAssoc->pNext = m_pFreeList;
150 m_pFreeList = pAssoc; 153 m_pFreeList = pAssoc;
151 m_nCount--; 154 m_nCount--;
152 ASSERT(m_nCount >= 0); 155 ASSERT(m_nCount >= 0);
153 if (m_nCount == 0) { 156 if (m_nCount == 0) {
154 RemoveAll(); 157 RemoveAll();
155 } 158 }
156 } 159 }
OLDNEW
« no previous file with comments | « core/fxcrt/fx_basic_list.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698