Skip to content

Commit 0f39f28

Browse files
author
David Beaumont
committed
Don't throw when vt:vector isn't the expected size
Fixes #759
1 parent 2339e13 commit 0f39f28

5 files changed

Lines changed: 30 additions & 21 deletions

File tree

bits/22_xmlutils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,15 @@ var vtregex = (function(){ var vt_cache = {};
138138
return (vt_cache[bt] = new RegExp("<(?:vt:)?" + bt + ">([\\s\\S]*?)</(?:vt:)?" + bt + ">", 'g') );
139139
};})();
140140
var vtvregex = /<\/?(?:vt:)?variant>/g, vtmregex = /<(?:vt:)([^>]*)>([\s\S]*)</;
141-
function parseVector(data) {
141+
function parseVector(data, opts) {
142142
var h = parsexmltag(data);
143143

144144
var matches = data.match(vtregex(h.baseType))||[];
145-
if(matches.length != h.size) throw new Error("unexpected vector length " + matches.length + " != " + h.size);
146145
var res = [];
146+
if(matches.length != h.size) {
147+
if(opts.WTF) throw new Error("unexpected vector length " + matches.length + " != " + h.size);
148+
return res;
149+
}
147150
matches.forEach(function(x) {
148151
var v = x.replace(vtvregex,"").match(vtmregex);
149152
res.push({v:utf8read(v[2]), t:v[1]});

bits/34_extprops.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var EXT_PROPS/*:Array<Array<string> >*/ = [
1717
XMLNS.EXT_PROPS = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
1818
RELS.EXT_PROPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties';
1919

20-
function parse_ext_props(data, p) {
20+
function parse_ext_props(data, p, opts) {
2121
var q = {}; if(!p) p = {};
2222

2323
EXT_PROPS.forEach(function(f) {
@@ -32,10 +32,10 @@ function parse_ext_props(data, p) {
3232
});
3333

3434
if(q.HeadingPairs && q.TitlesOfParts) {
35-
var v = parseVector(q.HeadingPairs);
36-
var parts = parseVector(q.TitlesOfParts).map(function(x) { return x.v; });
35+
var v = parseVector(q.HeadingPairs, opts);
36+
var parts = parseVector(q.TitlesOfParts, opts).map(function (x) { return x.v; });
3737
var idx = 0, len = 0;
38-
for(var i = 0; i !== v.length; i+=2) {
38+
if(parts.length > 0) for(var i = 0; i !== v.length; i += 2) {
3939
len = +(v[i+1].v);
4040
switch(v[i].v) {
4141
case "Worksheets":

bits/85_parsezip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function parse_zip(zip/*:ZIP*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
8686
if(propdata) props = parse_core_props(propdata);
8787
if(dir.extprops.length !== 0) {
8888
propdata = getzipstr(zip, dir.extprops[0].replace(/^\//,''), true);
89-
if(propdata) parse_ext_props(propdata, props);
89+
if(propdata) parse_ext_props(propdata, props, opts);
9090
}
9191
}
9292

xlsx.flow.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,12 +1803,15 @@ var vtregex = (function(){ var vt_cache = {};
18031803
return (vt_cache[bt] = new RegExp("<(?:vt:)?" + bt + ">([\\s\\S]*?)</(?:vt:)?" + bt + ">", 'g') );
18041804
};})();
18051805
var vtvregex = /<\/?(?:vt:)?variant>/g, vtmregex = /<(?:vt:)([^>]*)>([\s\S]*)</;
1806-
function parseVector(data) {
1806+
function parseVector(data, opts) {
18071807
var h = parsexmltag(data);
18081808

18091809
var matches = data.match(vtregex(h.baseType))||[];
1810-
if(matches.length != h.size) throw new Error("unexpected vector length " + matches.length + " != " + h.size);
18111810
var res = [];
1811+
if(matches.length != h.size) {
1812+
if(opts.WTF) throw new Error("unexpected vector length " + matches.length + " != " + h.size);
1813+
return res;
1814+
}
18121815
matches.forEach(function(x) {
18131816
var v = x.replace(vtvregex,"").match(vtmregex);
18141817
res.push({v:utf8read(v[2]), t:v[1]});
@@ -3408,7 +3411,7 @@ var EXT_PROPS/*:Array<Array<string> >*/ = [
34083411
XMLNS.EXT_PROPS = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
34093412
RELS.EXT_PROPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties';
34103413

3411-
function parse_ext_props(data, p) {
3414+
function parse_ext_props(data, p, opts) {
34123415
var q = {}; if(!p) p = {};
34133416

34143417
EXT_PROPS.forEach(function(f) {
@@ -3423,10 +3426,10 @@ function parse_ext_props(data, p) {
34233426
});
34243427

34253428
if(q.HeadingPairs && q.TitlesOfParts) {
3426-
var v = parseVector(q.HeadingPairs);
3427-
var parts = parseVector(q.TitlesOfParts).map(function(x) { return x.v; });
3429+
var v = parseVector(q.HeadingPairs, opts);
3430+
var parts = parseVector(q.TitlesOfParts, opts).map(function (x) { return x.v; });
34283431
var idx = 0, len = 0;
3429-
for(var i = 0; i !== v.length; i+=2) {
3432+
if(parts.length > 0) for(var i = 0; i !== v.length; i += 2) {
34303433
len = +(v[i+1].v);
34313434
switch(v[i].v) {
34323435
case "Worksheets":
@@ -17279,7 +17282,7 @@ function parse_zip(zip/*:ZIP*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
1727917282
if(propdata) props = parse_core_props(propdata);
1728017283
if(dir.extprops.length !== 0) {
1728117284
propdata = getzipstr(zip, dir.extprops[0].replace(/^\//,''), true);
17282-
if(propdata) parse_ext_props(propdata, props);
17285+
if(propdata) parse_ext_props(propdata, props, opts);
1728317286
}
1728417287
}
1728517288

xlsx.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,12 +1739,15 @@ var vtregex = (function(){ var vt_cache = {};
17391739
return (vt_cache[bt] = new RegExp("<(?:vt:)?" + bt + ">([\\s\\S]*?)</(?:vt:)?" + bt + ">", 'g') );
17401740
};})();
17411741
var vtvregex = /<\/?(?:vt:)?variant>/g, vtmregex = /<(?:vt:)([^>]*)>([\s\S]*)</;
1742-
function parseVector(data) {
1742+
function parseVector(data, opts) {
17431743
var h = parsexmltag(data);
17441744

17451745
var matches = data.match(vtregex(h.baseType))||[];
1746-
if(matches.length != h.size) throw new Error("unexpected vector length " + matches.length + " != " + h.size);
17471746
var res = [];
1747+
if(matches.length != h.size) {
1748+
if(opts.WTF) throw new Error("unexpected vector length " + matches.length + " != " + h.size);
1749+
return res;
1750+
}
17481751
matches.forEach(function(x) {
17491752
var v = x.replace(vtvregex,"").match(vtmregex);
17501753
res.push({v:utf8read(v[2]), t:v[1]});
@@ -3336,7 +3339,7 @@ var EXT_PROPS = [
33363339
XMLNS.EXT_PROPS = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
33373340
RELS.EXT_PROPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties';
33383341

3339-
function parse_ext_props(data, p) {
3342+
function parse_ext_props(data, p, opts) {
33403343
var q = {}; if(!p) p = {};
33413344

33423345
EXT_PROPS.forEach(function(f) {
@@ -3351,10 +3354,10 @@ function parse_ext_props(data, p) {
33513354
});
33523355

33533356
if(q.HeadingPairs && q.TitlesOfParts) {
3354-
var v = parseVector(q.HeadingPairs);
3355-
var parts = parseVector(q.TitlesOfParts).map(function(x) { return x.v; });
3357+
var v = parseVector(q.HeadingPairs, opts);
3358+
var parts = parseVector(q.TitlesOfParts, opts).map(function (x) { return x.v; });
33563359
var idx = 0, len = 0;
3357-
for(var i = 0; i !== v.length; i+=2) {
3360+
if(parts.length > 0) for(var i = 0; i !== v.length; i += 2) {
33583361
len = +(v[i+1].v);
33593362
switch(v[i].v) {
33603363
case "Worksheets":
@@ -17192,7 +17195,7 @@ function parse_zip(zip, opts) {
1719217195
if(propdata) props = parse_core_props(propdata);
1719317196
if(dir.extprops.length !== 0) {
1719417197
propdata = getzipstr(zip, dir.extprops[0].replace(/^\//,''), true);
17195-
if(propdata) parse_ext_props(propdata, props);
17198+
if(propdata) parse_ext_props(propdata, props, opts);
1719617199
}
1719717200
}
1719817201

0 commit comments

Comments
 (0)