{"record":{"id":"4e411b125df662ba","repo":"lionsoul2014/ip2region","slug":"invalid-bytes-ip-with-length-not-4-or-16","errorCode":null,"errorMessage":"invalid bytes ip with length not 4 or 16","messagePattern":"invalid bytes ip with length not 4 or 16","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"binding/javascript/util.js","lineNumber":228,"sourceCode":"            _.push('');\n        }\n    }\n\n    return _.join(':');\n}\n\n// bytes ip to humen-readable string ip\nexport function ipToString(ipBytes, compress) {\n    if (!Buffer.isBuffer(ipBytes)) {\n        throw new Error('invalid bytes ip, not a Buffer');\n    }\n\n    if (ipBytes.length == 4) {\n        return _ipv4_to_string(ipBytes, compress);\n    } else if (ipBytes.length == 16) {\n        return _ipv6_to_string(ipBytes, compress);\n    } else {\n        throw new Error('invalid bytes ip with length not 4 or 16');\n    }\n}\n\nexport function ipBytesString(ipBytes) {\n    if (!Buffer.isBuffer(ipBytes)) {\n        throw new Error('invalid bytes ip, not a Buffer');\n    }\n\n    let ps = [];\n    for (var i = 0; i < ipBytes.length; i++) {\n        ps.push(ipBytes[i] & 0xFF);\n    }\n\n    return ps.join('.');\n}\n\n// compare two byte ips\n// ip2 = buff[offset:ip1.length]","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/javascript/util.js#L210-L246","documentation":"After confirming the input is a Buffer, ipToString dispatches on length: 4 bytes are rendered as IPv4 and 16 bytes as IPv6. Any other length means the data is not a valid binary IP, so the library throws rather than guessing a format.","triggerScenarios":"Passing a Buffer of wrong size: a truncated slice (e.g. bytes.slice(0, 8)), a 6-byte MAC-style buffer, a zero-length buffer from an empty read, or an IPv4-mapped address stored as 12 bytes.","commonSituations":"Manual offset arithmetic over the xdb content buffer slicing the wrong byte count; mixing IPv4 and IPv6 records and assuming one size; corrupt or partially written custom data blobs.","solutions":["Verify the slice length: use exactly 4 bytes for IPv4 and 16 bytes for IPv6 (Buffer.subarray(start, start+4|16)).","Store/emit IPs in a fixed width matching their family; pad IPv4 into a 16-byte IPv4-mapped form if you want a single format.","Log ipBytes.length at the call site to find which producer emits the bad length.","Validate inputs with a length check before calling."],"exampleFix":"// before\nconst ip = ipToString(buf.subarray(off)); // wrong length\n// after\nconst len = version === 4 ? 4 : 16;\nconst ip = ipToString(buf.subarray(off, off + len));","handlingStrategy":"validation","validationCode":"function assertIpLength(b) {\n  if (!Buffer.isBuffer(b) || (b.length !== 4 && b.length !== 16))\n    throw new Error(`ip bytes must be 4 or 16 long, got ${b && b.length}`);\n}\nassertIpLength(ipBytes);","typeGuard":"function isWellSizedIp(b) { return Buffer.isBuffer(b) && (b.length === 4 || b.length === 16); }","tryCatchPattern":"try {\n  return ipToString(bytes);\n} catch (e) {\n  if (String(e.message).includes('length not 4 or 16')) {\n    throw new Error(`bad ip slice near offset ${off}: length=${bytes.length}`);\n  }\n  throw e;\n}","preventionTips":["Slice with explicit end offsets (subarray(off, off+4|16)), never a bare start offset.","Store the IP family alongside the bytes and derive length from it.","Unit-test your offset arithmetic against known IPv4 and IPv6 records."],"tags":["nodejs","buffer","ipv6","ipv4","data-corruption"],"backgroundTag":"invalid-ip-bytes-length","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}