Skip to content

Commit feda0ed

Browse files
committed
be less trusting of file lengths
1 parent 165ae82 commit feda0ed

File tree

6 files changed

+13
-2
lines changed

6 files changed

+13
-2
lines changed

lib/parseShp.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,15 @@ function ParseShp (buffer, trans) {
211211
return new ParseShp(buffer, trans);
212212
}
213213
this.buffer = buffer;
214+
this.headers = this.parseHeader();
215+
if (this.headers.length < this.buffer.byteLength) {
216+
this.buffer = this.buffer.slice(0, this.headers.length);
217+
}
214218
this.shpFuncs(trans);
215219
this.rows = this.getRows();
216220
}
217221
ParseShp.prototype.shpFuncs = function (tran) {
218-
let num = this.getShpCode();
222+
let num = this.headers.shpCode;
219223
if (num > 20) {
220224
num -= 20;
221225
}
@@ -231,7 +235,7 @@ ParseShp.prototype.getShpCode = function () {
231235
ParseShp.prototype.parseHeader = function () {
232236
const view = this.buffer.slice(0, 100);
233237
return {
234-
length: view.readInt32BE(6 << 2),
238+
length: view.readInt32BE(6 << 2) << 1,
235239
version: view.readInt32LE(7 << 2),
236240
shpCode: view.readInt32LE(8 << 2),
237241
bbox: [
@@ -249,6 +253,9 @@ ParseShp.prototype.getRows = function () {
249253
let current;
250254
while (offset < len) {
251255
current = this.getRow(offset);
256+
if (!current) {
257+
break;
258+
}
252259
offset += 8;
253260
offset += current.len;
254261
if (current.type) {

test/data/ipra_dresden_polygon.dbf

246 Bytes
Binary file not shown.

test/data/ipra_dresden_polygon.prj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

test/data/ipra_dresden_polygon.shp

656 Bytes
Binary file not shown.

test/data/ipra_dresden_polygon.shx

108 Bytes
Binary file not shown.

test/test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,8 @@ describe('Shp', function () {
301301
return item.features.length;
302302
}).should.eventually.equal(3);
303303
});
304+
it('file too long', function(){
305+
return shp('http://localhost:3000/test/data/ipra_dresden_polygon');
306+
});
304307
});
305308
});

0 commit comments

Comments
 (0)