Skip to content

Commit

Permalink
Add non-PRODUCTION/TESTING overflow asserts to various string helpe…
Browse files Browse the repository at this point in the history
…r-functions (issue 6759)
  • Loading branch information
Snuffleupagus committed Jun 27, 2021
1 parent d644b66 commit 273d8cb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3788,6 +3788,9 @@ class PartialEvaluator {
firstChar,
lastChar,
toUnicode,
xHeight: 0,
capHeight: 0,
italicAngle: 0,
isType3Font,
};
const widths = dict.get("Widths");
Expand Down Expand Up @@ -3919,10 +3922,10 @@ class PartialEvaluator {
bbox: descriptor.getArray("FontBBox") || dict.getArray("FontBBox"),
ascent: descriptor.get("Ascent"),
descent: descriptor.get("Descent"),
xHeight: descriptor.get("XHeight"),
capHeight: descriptor.get("CapHeight"),
xHeight: descriptor.get("XHeight") || 0,
capHeight: descriptor.get("CapHeight") || 0,
flags: descriptor.get("Flags"),
italicAngle: descriptor.get("ItalicAngle"),
italicAngle: descriptor.get("ItalicAngle") || 0,
isType3Font,
cssFontInfo,
scaleFactors: glyphScaleFactors,
Expand Down
20 changes: 19 additions & 1 deletion src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,28 @@ function int32(b0, b1, b2, b3) {
}

function string16(value) {
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
typeof value === "number" && Math.abs(value) < 2 ** 16,
`string16: Unexpected input "${value}".`
);
}
return String.fromCharCode((value >> 8) & 0xff, value & 0xff);
}

function safeString16(value) {
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
typeof value === "number" && !Number.isNaN(value),
`safeString16: Unexpected input "${value}".`
);
}
// clamp value to the 16-bit int range
if (value > 0x7fff) {
value = 0x7fff;
Expand Down Expand Up @@ -751,7 +769,7 @@ function createPostTable(properties) {
string32(angle) + // italicAngle
"\x00\x00" + // underlinePosition
"\x00\x00" + // underlineThickness
string32(properties.fixedPitch) + // isFixedPitch
string32(properties.fixedPitch ? 1 : 0) + // isFixedPitch
"\x00\x00\x00\x00" + // minMemType42
"\x00\x00\x00\x00" + // maxMemType42
"\x00\x00\x00\x00" + // minMemType1
Expand Down
9 changes: 9 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,15 @@ function arraysToBytes(arr) {
}

function string32(value) {
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
typeof value === "number" && Math.abs(value) < 2 ** 32,
`string32: Unexpected input "${value}".`
);
}
return String.fromCharCode(
(value >> 24) & 0xff,
(value >> 16) & 0xff,
Expand Down
3 changes: 3 additions & 0 deletions test/font/font_fpgm_spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/font/font_os2_spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/font/font_post_spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 273d8cb

Please sign in to comment.