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

Side by Side Diff: core/fpdfapi/fpdf_font/cpdf_simplefont.cpp

Issue 2293633002: Guard against overflow when calculating font weight. (Closed)
Patch Set: review feedback Created 4 years, 3 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
OLDNEW
1 // Copyright 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 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/fpdfapi/fpdf_font/cpdf_simplefont.h" 7 #include "core/fpdfapi/fpdf_font/cpdf_simplefont.h"
8 8
9 #include "core/fpdfapi/fpdf_font/font_int.h" 9 #include "core/fpdfapi/fpdf_font/font_int.h"
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
11 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h"
12 #include "core/fxge/include/fx_freetype.h" 12 #include "core/fxge/include/fx_freetype.h"
13 #include "third_party/base/numerics/safe_math.h"
13 14
14 CPDF_SimpleFont::CPDF_SimpleFont() : m_BaseEncoding(PDFFONT_ENCODING_BUILTIN) { 15 CPDF_SimpleFont::CPDF_SimpleFont() : m_BaseEncoding(PDFFONT_ENCODING_BUILTIN) {
15 FXSYS_memset(m_CharWidth, 0xff, sizeof(m_CharWidth)); 16 FXSYS_memset(m_CharWidth, 0xff, sizeof(m_CharWidth));
16 FXSYS_memset(m_GlyphIndex, 0xff, sizeof(m_GlyphIndex)); 17 FXSYS_memset(m_GlyphIndex, 0xff, sizeof(m_GlyphIndex));
17 FXSYS_memset(m_ExtGID, 0xff, sizeof(m_ExtGID)); 18 FXSYS_memset(m_ExtGID, 0xff, sizeof(m_ExtGID));
18 for (size_t i = 0; i < FX_ArraySize(m_CharBBox); ++i) 19 for (size_t i = 0; i < FX_ArraySize(m_CharBBox); ++i)
19 m_CharBBox[i] = FX_RECT(-1, -1, -1, -1); 20 m_CharBBox[i] = FX_RECT(-1, -1, -1, -1);
20 } 21 }
21 22
22 CPDF_SimpleFont::~CPDF_SimpleFont() {} 23 CPDF_SimpleFont::~CPDF_SimpleFont() {}
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 if (width == 0) { 175 if (width == 0) {
175 width = m_CharWidth[i]; 176 width = m_CharWidth[i];
176 } else if (width != m_CharWidth[i]) { 177 } else if (width != m_CharWidth[i]) {
177 break; 178 break;
178 } 179 }
179 } 180 }
180 if (i == 256 && width) { 181 if (i == 256 && width) {
181 m_Flags |= PDFFONT_FIXEDPITCH; 182 m_Flags |= PDFFONT_FIXEDPITCH;
182 } 183 }
183 } 184 }
184 int weight = m_StemV < 140 ? m_StemV * 5 : (m_StemV * 4 + 140); 185 pdfium::base::CheckedNumeric<int> safeStemV(m_StemV);
185 m_Font.LoadSubst(m_BaseFont, IsTrueTypeFont(), m_Flags, weight, m_ItalicAngle, 186 if (m_StemV < 140)
186 0); 187 safeStemV *= 5;
188 else
189 safeStemV = safeStemV * 5 + 140;
Wei Li 2016/08/29 23:05:48 Shouldn't multiple 4 here?
dsinclair 2016/08/30 14:26:39 Doh. Good catch, thanks.
190 m_Font.LoadSubst(m_BaseFont, IsTrueTypeFont(), m_Flags,
191 safeStemV.ValueOrDefault(140), m_ItalicAngle, 0);
Wei Li 2016/08/29 23:05:48 Maybe the default value should be FXFONT_FW_NORMAL
dsinclair 2016/08/30 14:26:39 Went with normal, thanks for pointing it out.
187 } 192 }
188 193
189 FX_BOOL CPDF_SimpleFont::IsUnicodeCompatible() const { 194 FX_BOOL CPDF_SimpleFont::IsUnicodeCompatible() const {
190 return m_BaseEncoding != PDFFONT_ENCODING_BUILTIN && 195 return m_BaseEncoding != PDFFONT_ENCODING_BUILTIN &&
191 m_BaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL && 196 m_BaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL &&
192 m_BaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS; 197 m_BaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS;
193 } 198 }
194 199
195 CFX_WideString CPDF_SimpleFont::UnicodeFromCharCode(uint32_t charcode) const { 200 CFX_WideString CPDF_SimpleFont::UnicodeFromCharCode(uint32_t charcode) const {
196 CFX_WideString unicode = CPDF_Font::UnicodeFromCharCode(charcode); 201 CFX_WideString unicode = CPDF_Font::UnicodeFromCharCode(charcode);
197 if (!unicode.IsEmpty()) 202 if (!unicode.IsEmpty())
198 return unicode; 203 return unicode;
199 FX_WCHAR ret = m_Encoding.UnicodeFromCharCode((uint8_t)charcode); 204 FX_WCHAR ret = m_Encoding.UnicodeFromCharCode((uint8_t)charcode);
200 if (ret == 0) 205 if (ret == 0)
201 return CFX_WideString(); 206 return CFX_WideString();
202 return ret; 207 return ret;
203 } 208 }
204 209
205 uint32_t CPDF_SimpleFont::CharCodeFromUnicode(FX_WCHAR unicode) const { 210 uint32_t CPDF_SimpleFont::CharCodeFromUnicode(FX_WCHAR unicode) const {
206 uint32_t ret = CPDF_Font::CharCodeFromUnicode(unicode); 211 uint32_t ret = CPDF_Font::CharCodeFromUnicode(unicode);
207 if (ret) 212 if (ret)
208 return ret; 213 return ret;
209 return m_Encoding.CharCodeFromUnicode(unicode); 214 return m_Encoding.CharCodeFromUnicode(unicode);
210 } 215 }
OLDNEW
« core/fpdfapi/fpdf_font/cpdf_cidfont.cpp ('K') | « core/fpdfapi/fpdf_font/cpdf_cidfont.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698