{"record":{"id":"15e9b1ab0a7a4526","repo":"lionsoul2014/ip2region","slug":"incomplete-read-rbytes-read-vbytes-expecte","errorCode":null,"errorMessage":"incomplete read (${rBytes} read, ${vBytes} expected)","messagePattern":"incomplete read \\((.+?) read, (.+?) expected\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"binding/javascript/util.js","lineNumber":359,"sourceCode":"    if (rBytes != HeaderInfoLength) {\n        throw new Error(`incomplete read (${rBytes} read, ${header.HeaderInfoLength} expected)`);\n    }\n    return new Header(buffer);\n}\n\nexport function loadHeaderFromFile(dbPath) {\n    const fd = fs.openSync(dbPath, \"r\");\n    const header = loadHeader(fd);\n    fs.closeSync(fd);\n    return header;\n}\n\nexport function loadVectorIndex(fd) {\n    const vBytes = VectorIndexCols * VectorIndexRows * VectorIndexSize;\n    const buffer = Buffer.alloc(vBytes);\n    const rBytes = fs.readSync(fd, buffer, 0, vBytes, HeaderInfoLength);\n    if (rBytes != vBytes) {\n        throw new Error(`incomplete read (${rBytes} read, ${vBytes} expected)`);\n    }\n    return buffer;\n}\n\nexport function loadVectorIndexFromFile(dbPath) {\n    const fd = fs.openSync(dbPath, \"r\");\n    const vIndex = loadVectorIndex(fd);\n    fs.closeSync(fd);\n    return vIndex;\n}\n\nexport function loadContent(fd) {\n    const stats = fs.fstatSync(fd);\n    const buffer = Buffer.alloc(stats.size);\n    const rBytes = fs.readSync(fd, buffer, 0, buffer.length, 0);\n    if (rBytes != stats.size) {\n        throw new Error(`incomplete read (${rBytes} read, ${stats.size} expected)`);\n    }","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/javascript/util.js#L341-L377","documentation":"loadVectorIndex reads the vector index region (VectorIndexCols * VectorIndexRows * VectorIndexSize bytes) starting at HeaderInfoLength. A short read means the file ends before the vector index completes, so region lookups would return garbage; the library throws with read vs expected byte counts.","triggerScenarios":"loadVectorIndex(fd) / vIndex() on a truncated xdb: file smaller than HeaderInfoLength + vBytes, partial download, wrong file, or a hand-trimmed database.","commonSituations":"Failed or resumable-download artifact; copying the file while the updater writes it; slicing an xdb to ship 'only what's needed' and cutting off the index region.","solutions":["Replace the data file with a verified full copy of the official xdb file (check size and hash).","Do not edit/trim the xdb file; the header, vector index and content must stay contiguous.","Ensure the file is not being read while a writer (maker) is still producing it.","Pre-check fs.fstatSync(fd).size >= HeaderInfoLength + vBytes before loading."],"exampleFix":"// before\nconst vIndex = loadVectorIndex(fd); // throws if truncated\n// after\nconst need = HeaderInfoLength + VectorIndexCols * VectorIndexRows * VectorIndexSize;\nif (fs.fstatSync(fd).size < need) throw new Error('xdb too small for vector index');\nconst vIndex = loadVectorIndex(fd);","handlingStrategy":"validation","validationCode":"const need = HeaderInfoLength + VectorIndexCols * VectorIndexRows * VectorIndexSize;\nif (fs.fstatSync(fd).size < need)\n  throw new Error('xdb too small: vector index region missing');","typeGuard":null,"tryCatchPattern":"try {\n  const vIndex = loadVectorIndex(fd);\n} catch (e) {\n  if (String(e.message).includes('incomplete read')) {\n    // reload from a verified copy of the db file\n    throw new Error('xdb vector index truncated; restore full file');\n  }\n  throw e;\n}","preventionTips":["Treat the xdb file as immutable; never trim or edit it by hand.","Wait for the maker process to finish before deploying a freshly generated file.","Check the published file size against your local copy during CI/CD."],"tags":["nodejs","file-io","truncated-file","vector-index"],"backgroundTag":"incomplete-file-read","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}