mrdoob/three.js · error
invalid length
Error message
invalid length
What it means
Thrown by slice_slice(length) in the shapefile stream reader when the requested slice length, coerced to a 32-bit integer, is negative. The function is a buffered stream slicer: it reads length bytes from the underlying source, so a negative length is treated as a programming error rather than an empty read.
Source
Thrown at manual/resources/tools/geo-picking/shapefile.js:86
c.set(a);
c.set(b, a.length);
return c;
}
var slice_read = function() {
var that = this, array = that._array.subarray(that._index);
return that._source.read().then(function(result) {
that._array = empty;
that._index = 0;
return result.done ? (array.length > 0
? {done: false, value: array}
: {done: true, value: undefined})
: {done: false, value: concat(array, result.value)};
});
};
var slice_slice = function(length) {
if ((length |= 0) < 0) throw new Error("invalid length");
var that = this, index = this._array.length - this._index;
// If the request fits within the remaining buffer, resolve it immediately.
if (this._index + length <= this._array.length) {
return Promise.resolve(this._array.subarray(this._index, this._index += length));
}
// Otherwise, read chunks repeatedly until the request is fulfilled.
var array = new Uint8Array(length);
array.set(this._array.subarray(this._index));
return (function read() {
return that._source.read().then(function(result) {
// When done, it’s possible the request wasn’t fully fulfilled!
// If so, the pre-allocated array is too big and needs slicing.
if (result.done) {
that._array = empty;
that._index = 0;View on GitHub (pinned to da05705fa3)
Solutions
- Validate the length is a non-negative finite number before calling .slice(); treat a negative computed length as a corrupt-record signal.
- If the length derives from header fields, clamp/guard against underflow (e.g. Math.max(0, declared - fixedHeader)).
- Confirm the input is actually a .shp stream and not truncated; re-export or re-download the shapefile if it is malformed.
Example fix
// before
const recordBody = source.slice( recordLength - headerSize ); // can be negative
// after
const bodyLen = recordLength - headerSize;
if ( bodyLen < 0 ) throw new Error( `corrupt record: length ${recordLength}` );
const recordBody = source.slice( bodyLen ); Defensive patterns
Strategy: validation
Validate before calling
function safeSlice( source, length ) {
if ( ! Number.isFinite( length ) || ( length | 0 ) < 0 ) {
throw new Error( `invalid slice length: ${length}` );
}
return source.slice( length );
} Try / catch
try {
body = source.slice( computedLen );
} catch ( err ) {
if ( /invalid length/.test( err.message ) ) {
throw new Error( `corrupt record: computed length ${computedLen}` );
}
throw err;
} Prevention
- Validate computed lengths against underflow (Math.max(0, ...)) before slicing.
- Treat a negative length as a sign of a corrupt or truncated .shp record.
- Confirm the input is a genuine shapefile stream, not random bytes.
When it happens
Trigger: Calling source.slice(n) with n < 0. Passing an undefined/NaN length that coerces to a negative int32 through '| 0'. A caller that computes a length from header fields (e.g. record length minus header) and underflows when the record is malformed or truncated.
Common situations: A corrupted or truncated .shp file where a record's declared byte length is smaller than the fixed header, producing a negative remaining length in the caller. Feeding a non-shapefile blob to the reader so parsed length fields are garbage.
Related errors
- unsupported shape type: ${type}
- unknown geometry type.
- "transform stack empty!
- transform stack not 0
- no diagram ${name}
AI-assisted analysis of mrdoob/three.js@da05705fa3 (2026-08-12).
Data as JSON: /api/errors/f64d7ca0079e1853.
Report an issue: GitHub.