{"record":{"id":"f64d7ca0079e1853","repo":"mrdoob/three.js","slug":"invalid-length","errorCode":null,"errorMessage":"invalid length","messagePattern":"invalid length","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"manual/resources/tools/geo-picking/shapefile.js","lineNumber":86,"sourceCode":"  c.set(a);\n  c.set(b, a.length);\n  return c;\n}\n\nvar slice_read = function() {\n  var that = this, array = that._array.subarray(that._index);\n  return that._source.read().then(function(result) {\n    that._array = empty;\n    that._index = 0;\n    return result.done ? (array.length > 0\n        ? {done: false, value: array}\n        : {done: true, value: undefined})\n        : {done: false, value: concat(array, result.value)};\n  });\n};\n\nvar slice_slice = function(length) {\n  if ((length |= 0) < 0) throw new Error(\"invalid length\");\n  var that = this, index = this._array.length - this._index;\n\n  // If the request fits within the remaining buffer, resolve it immediately.\n  if (this._index + length <= this._array.length) {\n    return Promise.resolve(this._array.subarray(this._index, this._index += length));\n  }\n\n  // Otherwise, read chunks repeatedly until the request is fulfilled.\n  var array = new Uint8Array(length);\n  array.set(this._array.subarray(this._index));\n  return (function read() {\n    return that._source.read().then(function(result) {\n\n      // When done, it’s possible the request wasn’t fully fulfilled!\n      // If so, the pre-allocated array is too big and needs slicing.\n      if (result.done) {\n        that._array = empty;\n        that._index = 0;","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/mrdoob/three.js/blob/da05705fa31f02710c19772a2aa70c7454807f0c/manual/resources/tools/geo-picking/shapefile.js#L68-L104","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst recordBody = source.slice( recordLength - headerSize ); // can be negative\n\n// after\nconst bodyLen = recordLength - headerSize;\nif ( bodyLen < 0 ) throw new Error( `corrupt record: length ${recordLength}` );\nconst recordBody = source.slice( bodyLen );","handlingStrategy":"validation","validationCode":"function safeSlice( source, length ) {\n  if ( ! Number.isFinite( length ) || ( length | 0 ) < 0 ) {\n    throw new Error( `invalid slice length: ${length}` );\n  }\n  return source.slice( length );\n}","typeGuard":null,"tryCatchPattern":"try {\n  body = source.slice( computedLen );\n} catch ( err ) {\n  if ( /invalid length/.test( err.message ) ) {\n    throw new Error( `corrupt record: computed length ${computedLen}` );\n  }\n  throw err;\n}","preventionTips":["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."],"tags":["geo","manual","shapefile","stream","validation"],"backgroundTag":null,"analyzedSha":"da05705fa31f02710c19772a2aa70c7454807f0c","analyzedAt":"2026-08-12T22:51:37.160Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}