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

Side by Side Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp

Issue 322333002: Fix the potential integer overflow from "offset + size" (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 6 years, 5 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 | « no previous file | core/src/fxcrt/extension.h » ('j') | 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 "../../../include/fpdfapi/fpdf_parser.h" 7 #include "../../../include/fpdfapi/fpdf_parser.h"
8 #include "../../../include/fpdfapi/fpdf_module.h" 8 #include "../../../include/fpdfapi/fpdf_module.h"
9 #include "../../../include/fpdfapi/fpdf_page.h" 9 #include "../../../include/fpdfapi/fpdf_page.h"
10 #include "../../../../third_party/numerics/safe_math.h" 10 #include "../../../../third_party/numerics/safe_math.h"
(...skipping 2847 matching lines...) Expand 10 before | Expand all | Expand 10 after
2858 new_obj_array.Add(value); 2858 new_obj_array.Add(value);
2859 } 2859 }
2860 } 2860 }
2861 } 2861 }
2862 break; 2862 break;
2863 case PDFOBJ_REFERENCE: { 2863 case PDFOBJ_REFERENCE: {
2864 CPDF_Reference *pRef = (CPDF_Reference*)pObj; 2864 CPDF_Reference *pRef = (CPDF_Reference*)pObj;
2865 FX_DWORD dwNum = pRef->GetRefObjNum(); 2865 FX_DWORD dwNum = pRef->GetRefObjNum();
2866 FX_FILESIZE offset; 2866 FX_FILESIZE offset;
2867 FX_DWORD size = GetObjectSize(pRef->GetRefObjNum(), offset); 2867 FX_DWORD size = GetObjectSize(pRef->GetRefObjNum(), offset);
2868 if (!size) { 2868
2869 if (size == 0 || offset < 0 || offset >= m_dwFileLen)
2869 break; 2870 break;
2870 } 2871
2871 size = (FX_DWORD)((FX_FILESIZE)(offset + size + 512) > m_dwF ileLen ? m_dwFileLen - offset : size + 512); 2872 base::CheckedNumeric<FX_DWORD> safe_size = size;
palmer 2014/07/09 18:29:14 You should check the value that you actually use,
jun_fang 2014/07/10 22:21:54 safe_size is a temporary variable which is used to
2873 safe_size += offset;
2874 safe_size += 512;
2875 if (!safe_size.IsValid())
2876 break;
2877
2878 if (safe_size.ValueOrDie() > m_dwFileLen)
2879 size = m_dwFileLen - offset;
jun_fang 2014/07/10 22:21:54 size = m_dwFileLen - offset; |size| should be vali
2880 else
2881 size = size + 512;
jun_fang 2014/07/10 22:21:54 because safe_size is valid, we can make sure size
2882
2872 if (!m_pFileAvail->IsDataAvail(offset, size)) { 2883 if (!m_pFileAvail->IsDataAvail(offset, size)) {
2873 pHints->AddSegment(offset, size); 2884 pHints->AddSegment(offset, size);
2874 ret_array.Add(pObj); 2885 ret_array.Add(pObj);
2875 count++; 2886 count++;
2876 } else if (!m_objnum_array.Find(dwNum)) { 2887 } else if (!m_objnum_array.Find(dwNum)) {
2877 m_objnum_array.AddObjNum(dwNum); 2888 m_objnum_array.AddObjNum(dwNum);
2878 CPDF_Object *pReferred = m_pDocument->GetIndirectObject( pRef->GetRefObjNum(), NULL); 2889 CPDF_Object *pReferred = m_pDocument->GetIndirectObject( pRef->GetRefObjNum(), NULL);
2879 if (pReferred) { 2890 if (pReferred) {
2880 new_obj_array.Add(pReferred); 2891 new_obj_array.Add(pReferred);
2881 } 2892 }
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
3063 CPDF_Object *pRet = NULL; 3074 CPDF_Object *pRet = NULL;
3064 if (pExistInFile) { 3075 if (pExistInFile) {
3065 *pExistInFile = TRUE; 3076 *pExistInFile = TRUE;
3066 } 3077 }
3067 if (m_pDocument == NULL) { 3078 if (m_pDocument == NULL) {
3068 FX_FILESIZE offset = m_parser.GetObjectOffset(objnum); 3079 FX_FILESIZE offset = m_parser.GetObjectOffset(objnum);
3069 if (offset < 0) { 3080 if (offset < 0) {
3070 *pExistInFile = FALSE; 3081 *pExistInFile = FALSE;
3071 return NULL; 3082 return NULL;
3072 } 3083 }
3084
3073 FX_DWORD size = (FX_DWORD)m_parser.GetObjectSize(objnum); 3085 FX_DWORD size = (FX_DWORD)m_parser.GetObjectSize(objnum);
3074 size = (FX_DWORD)(((FX_FILESIZE)(offset + size + 512)) > m_dwFileLen ? m _dwFileLen - offset : size + 512); 3086 if (size == 0 || offset < 0 || offset >= m_dwFileLen)
3087 return NULL;
3088
3089 base::CheckedNumeric<FX_DWORD> safe_size = size;
palmer 2014/07/09 18:29:14 Same as above.
3090 safe_size += offset;
3091 safe_size += 512;
3092 if (!safe_size.IsValid())
3093 return NULL;
3094
3095 if (safe_size.ValueOrDie() > m_dwFileLen)
3096 size = m_dwFileLen - offset;
3097 else
3098 size = size + 512;
3099
3075 if (!m_pFileAvail->IsDataAvail(offset, size)) { 3100 if (!m_pFileAvail->IsDataAvail(offset, size)) {
3076 pHints->AddSegment(offset, size); 3101 pHints->AddSegment(offset, size);
3077 return NULL; 3102 return NULL;
3078 } 3103 }
3079 pRet = m_parser.ParseIndirectObject(NULL, objnum); 3104 pRet = m_parser.ParseIndirectObject(NULL, objnum);
3080 if (!pRet && pExistInFile) { 3105 if (!pRet && pExistInFile) {
3081 *pExistInFile = FALSE; 3106 *pExistInFile = FALSE;
3082 } 3107 }
3083 return pRet; 3108 return pRet;
3084 } 3109 }
3085 FX_FILESIZE offset = 0; 3110 FX_FILESIZE offset = 0;
3086 FX_DWORD size = GetObjectSize(objnum, offset); 3111 FX_DWORD size = GetObjectSize(objnum, offset);
3087 size = (FX_DWORD)((FX_FILESIZE)(offset + size + 512) > m_dwFileLen ? m_dwFil eLen - offset : size + 512); 3112 if (size == 0 || offset < 0 || offset >= m_dwFileLen)
3113 return NULL;
3114
3115 base::CheckedNumeric<FX_DWORD> safe_size = size;
palmer 2014/07/09 18:29:14 Same as above.
3116 safe_size += offset;
3117 safe_size += 512;
3118 if (!safe_size.IsValid())
3119 return NULL;
3120
3121 if (safe_size.ValueOrDie() > m_dwFileLen)
3122 size = m_dwFileLen - offset;
3123 else
3124 size = size + 512;
3125
3088 if (!m_pFileAvail->IsDataAvail(offset, size)) { 3126 if (!m_pFileAvail->IsDataAvail(offset, size)) {
3089 pHints->AddSegment(offset, size); 3127 pHints->AddSegment(offset, size);
3090 return NULL; 3128 return NULL;
3091 } 3129 }
3092 CPDF_Parser *pParser = (CPDF_Parser *)(m_pDocument->GetParser()); 3130 CPDF_Parser *pParser = (CPDF_Parser *)(m_pDocument->GetParser());
3093 pRet = pParser->ParseIndirectObject(NULL, objnum, NULL); 3131 pRet = pParser->ParseIndirectObject(NULL, objnum, NULL);
3094 if (!pRet && pExistInFile) { 3132 if (!pRet && pExistInFile) {
3095 *pExistInFile = FALSE; 3133 *pExistInFile = FALSE;
3096 } 3134 }
3097 return pRet; 3135 return pRet;
(...skipping 1286 matching lines...) Expand 10 before | Expand all | Expand 10 after
4384 { 4422 {
4385 FX_INT32 iSize = m_childNode.GetSize(); 4423 FX_INT32 iSize = m_childNode.GetSize();
4386 for (FX_INT32 i = 0; i < iSize; ++i) { 4424 for (FX_INT32 i = 0; i < iSize; ++i) {
4387 CPDF_PageNode *pNode = (CPDF_PageNode*)m_childNode[i]; 4425 CPDF_PageNode *pNode = (CPDF_PageNode*)m_childNode[i];
4388 if (pNode) { 4426 if (pNode) {
4389 delete pNode; 4427 delete pNode;
4390 } 4428 }
4391 } 4429 }
4392 m_childNode.RemoveAll(); 4430 m_childNode.RemoveAll();
4393 } 4431 }
OLDNEW
« no previous file with comments | « no previous file | core/src/fxcrt/extension.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698