{"record":{"id":"48df8a2f0d34c43f","repo":"FuelLabs/fuels-ts","slug":"invalid-data-48df8a","errorCode":"INVALID_DATA","errorMessage":"cannot slice beyond data bounds","messagePattern":"cannot slice beyond data bounds","errorType":"exception","errorClass":"FuelError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/utils/dataSlice.ts","lineNumber":18,"sourceCode":"import { ErrorCode, FuelError } from '@fuel-ts/errors';\n\nimport { arrayify } from './arrayify';\nimport type { BytesLike } from './arrayify';\nimport { hexlify } from './hexlify';\n\n/**\n *  Returns a hex string by slicing data from the start offset to the end offset.\n *\n * @param data - the data to be sliced.\n * @param start - the start offset (default: 0).\n * @param end - the end offset (default: length of data).\n * @returns - a sliced hex string from start to end.\n */\nexport function dataSlice(data: BytesLike, start?: number, end?: number): string {\n  const bytes = arrayify(data);\n  if (end != null && end > bytes.length) {\n    throw new FuelError(ErrorCode.INVALID_DATA, 'cannot slice beyond data bounds');\n  }\n  return hexlify(bytes.slice(start == null ? 0 : start, end == null ? bytes.length : end));\n}\n","sourceCodeStart":1,"sourceCodeEnd":22,"githubUrl":"https://github.com/FuelLabs/fuels-ts/blob/b3f37c91aca4aa9d5e4c0d3967f66237190826ea/packages/utils/src/utils/dataSlice.ts#L1-L22","documentation":"Thrown by dataSlice when an explicit end offset is greater than the length of the underlying byte array (after arrayify converts the input). Only the upper bound is validated; a start beyond length is not checked here (it would just yield an empty slice). Protects callers from reading past the end of the data buffer.","triggerScenarios":"Calling dataSlice(data, start, end) with end > bytes.length. For example dataSlice('0x1234', 0, 4) when only 2 bytes exist, or dataSlice(shortBytes, 0, 32) expecting a 32-byte word from a shorter value.","commonSituations":"Assuming a fixed-size (e.g. 32-byte) value when the input is shorter. Off-by-one in offset math. Slicing a partially-received or truncated payload. Hardcoding end=32 for b256-sized reads on a value that has fewer bytes.","solutions":["Check the actual byte length before slicing and clamp end to bytes.length.","Compute end from the real data length rather than a hardcoded constant.","Pad short values to the expected size before slicing if a fixed-width read is required.","Use arrayify first to learn the length, then derive a safe end."],"exampleFix":"// before\ndataSlice('0x1234', 0, 32); // throws: only 2 bytes\n\n// after\nconst bytes = arrayify('0x1234');\nconst end = Math.min(32, bytes.length);\ndataSlice(bytes, 0, end);","handlingStrategy":"validation","validationCode":"import { arrayify } from '@fuel-ts/utils';\nfunction safeSlice(data: BytesLike, start = 0, end?: number) {\n  const len = arrayify(data).length;\n  const e = end == null ? len : Math.min(end, len);\n  if (start > len) start = len;\n  return dataSlice(data, start, e);\n}","typeGuard":null,"tryCatchPattern":"try {\n  const hex = dataSlice(data, start, end);\n} catch (e) {\n  if (e instanceof FuelError && e.code === ErrorCode.INVALID_DATA) {\n    // clamp end to actual length and retry, or report short payload\n  }\n  throw e;\n}","preventionTips":["Compute end from the real byte length rather than a hardcoded constant.","Pad short values to the expected width before fixed-size reads.","Always arrayify first to learn the length when reading from variable-size data."],"tags":["dataslice","bytes","offset","bounds-check"],"backgroundTag":null,"analyzedSha":"b3f37c91aca4aa9d5e4c0d3967f66237190826ea","analyzedAt":"2026-08-12T20:30:56.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}