Skip to content

Commit 627fe2d

Browse files
Merge pull request #18017 from Snuffleupagus/validate-widths
Add more validation of width-data
2 parents 234067e + d411a07 commit 627fe2d

File tree

1 file changed

+58
-27
lines changed

1 file changed

+58
-27
lines changed

src/core/evaluator.js

+58-27
Original file line numberDiff line numberDiff line change
@@ -3892,66 +3892,97 @@ class PartialEvaluator {
38923892
let defaultWidth = 0;
38933893
const glyphsVMetrics = [];
38943894
let defaultVMetrics;
3895-
let i, ii, j, jj, start, code, widths;
38963895
if (properties.composite) {
3897-
defaultWidth = dict.has("DW") ? dict.get("DW") : 1000;
3896+
const dw = dict.get("DW");
3897+
defaultWidth = Number.isInteger(dw) ? dw : 1000;
3898+
3899+
const widths = dict.get("W");
3900+
if (Array.isArray(widths)) {
3901+
for (let i = 0, ii = widths.length; i < ii; i++) {
3902+
let start = xref.fetchIfRef(widths[i++]);
3903+
if (!Number.isInteger(start)) {
3904+
break; // Invalid /W data.
3905+
}
3906+
const code = xref.fetchIfRef(widths[i]);
38983907

3899-
widths = dict.get("W");
3900-
if (widths) {
3901-
for (i = 0, ii = widths.length; i < ii; i++) {
3902-
start = xref.fetchIfRef(widths[i++]);
3903-
code = xref.fetchIfRef(widths[i]);
39043908
if (Array.isArray(code)) {
3905-
for (j = 0, jj = code.length; j < jj; j++) {
3906-
glyphsWidths[start++] = xref.fetchIfRef(code[j]);
3909+
for (const c of code) {
3910+
const width = xref.fetchIfRef(c);
3911+
if (typeof width === "number") {
3912+
glyphsWidths[start] = width;
3913+
}
3914+
start++;
39073915
}
3908-
} else {
3916+
} else if (Number.isInteger(code)) {
39093917
const width = xref.fetchIfRef(widths[++i]);
3910-
for (j = start; j <= code; j++) {
3918+
if (typeof width !== "number") {
3919+
continue;
3920+
}
3921+
for (let j = start; j <= code; j++) {
39113922
glyphsWidths[j] = width;
39123923
}
3924+
} else {
3925+
break; // Invalid /W data.
39133926
}
39143927
}
39153928
}
39163929

39173930
if (properties.vertical) {
3918-
let vmetrics = dict.getArray("DW2") || [880, -1000];
3931+
const dw2 = dict.getArray("DW2");
3932+
let vmetrics = isNumberArray(dw2, 2) ? dw2 : [880, -1000];
39193933
defaultVMetrics = [vmetrics[1], defaultWidth * 0.5, vmetrics[0]];
39203934
vmetrics = dict.get("W2");
3921-
if (vmetrics) {
3922-
for (i = 0, ii = vmetrics.length; i < ii; i++) {
3923-
start = xref.fetchIfRef(vmetrics[i++]);
3924-
code = xref.fetchIfRef(vmetrics[i]);
3935+
if (Array.isArray(vmetrics)) {
3936+
for (let i = 0, ii = vmetrics.length; i < ii; i++) {
3937+
let start = xref.fetchIfRef(vmetrics[i++]);
3938+
if (!Number.isInteger(start)) {
3939+
break; // Invalid /W2 data.
3940+
}
3941+
const code = xref.fetchIfRef(vmetrics[i]);
3942+
39253943
if (Array.isArray(code)) {
3926-
for (j = 0, jj = code.length; j < jj; j++) {
3927-
glyphsVMetrics[start++] = [
3944+
for (let j = 0, jj = code.length; j < jj; j++) {
3945+
const vmetric = [
39283946
xref.fetchIfRef(code[j++]),
39293947
xref.fetchIfRef(code[j++]),
39303948
xref.fetchIfRef(code[j]),
39313949
];
3950+
if (isNumberArray(vmetric, null)) {
3951+
glyphsVMetrics[start] = vmetric;
3952+
}
3953+
start++;
39323954
}
3933-
} else {
3955+
} else if (Number.isInteger(code)) {
39343956
const vmetric = [
39353957
xref.fetchIfRef(vmetrics[++i]),
39363958
xref.fetchIfRef(vmetrics[++i]),
39373959
xref.fetchIfRef(vmetrics[++i]),
39383960
];
3939-
for (j = start; j <= code; j++) {
3961+
if (!isNumberArray(vmetric, null)) {
3962+
continue;
3963+
}
3964+
for (let j = start; j <= code; j++) {
39403965
glyphsVMetrics[j] = vmetric;
39413966
}
3967+
} else {
3968+
break; // Invalid /W2 data.
39423969
}
39433970
}
39443971
}
39453972
}
39463973
} else {
3947-
const firstChar = properties.firstChar;
3948-
widths = dict.get("Widths");
3949-
if (widths) {
3950-
j = firstChar;
3951-
for (i = 0, ii = widths.length; i < ii; i++) {
3952-
glyphsWidths[j++] = xref.fetchIfRef(widths[i]);
3974+
const widths = dict.get("Widths");
3975+
if (Array.isArray(widths)) {
3976+
let j = properties.firstChar;
3977+
for (const w of widths) {
3978+
const width = xref.fetchIfRef(w);
3979+
if (typeof width === "number") {
3980+
glyphsWidths[j] = width;
3981+
}
3982+
j++;
39533983
}
3954-
defaultWidth = parseFloat(descriptor.get("MissingWidth")) || 0;
3984+
const missingWidth = descriptor.get("MissingWidth");
3985+
defaultWidth = typeof missingWidth === "number" ? missingWidth : 0;
39553986
} else {
39563987
// Trying get the BaseFont metrics (see comment above).
39573988
const baseFontName = dict.get("BaseFont");

0 commit comments

Comments
 (0)