lionsoul2014/ip2region · error · Error

invalid ip address '${ipToString(ipBytes)}' (${this.version.

Error message

invalid ip address '${ipToString(ipBytes)}' (${this.version.name} expected)

What it means

Thrown by the JS searcher when the parsed IP's byte length (4 for IPv4, 16 for IPv6) does not match the byte width of this searcher's version, e.g. feeding an IPv6 string to a searcher opened on an IPv4 xdb. It is a version-consistency guard fired after parseIP succeeds, not a syntax error.

Source

Thrown at binding/javascript/searcher.js:45

            this.cBuffer = null;
        }
    }

    getIPVersion() {
        return this.version;
    }

    getIOCount() {
        return this.ioCount;
    }

    async search(ip) {
        // check and parse the string ip
        const ipBytes = Buffer.isBuffer(ip) ? ip : parseIP(ip);

        // ip version check
        if (ipBytes.length != this.version.bytes) {
            throw new Error(`invalid ip address '${ipToString(ipBytes)}' (${this.version.name} expected)`);
        }

        // reset the global counter
        this.ioCount = 0;

        // located the segment index block based on the vector index
        let sPtr = 0, ePtr = 0;
        let il0 = ipBytes[0], il1 = ipBytes[1];
        let idx = il0 * VectorIndexCols * VectorIndexSize + il1 * VectorIndexSize;
        if (this.vectorIndex != null) {
            sPtr = this.vectorIndex.readUint32LE(idx);
            ePtr = this.vectorIndex.readUint32LE(idx + 4);
        } else if (this.cBuffer != null) {
            sPtr = this.cBuffer.readUint32LE(HeaderInfoLength + idx);
            ePtr = this.cBuffer.readUint32LE(HeaderInfoLength + idx + 4);
        } else {
            const buff = Buffer.alloc(VectorIndexSize);
            this.read(HeaderInfoLength + idx, buff);

View on GitHub (pinned to c1a1fc7d59)

Solutions

  1. Instantiate the searcher with the xdb file matching the IP family being queried (v4 xdb for 4-byte IPs, v6 xdb for 16-byte IPs)
  2. Check searcher.getIPVersion() before calling search and route the query to the right searcher
  3. Auto-detect the family from the string (presence of ':') and pick the corresponding searcher
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at binding/javascript/searcher.js:45 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02). Data as JSON: /api/errors/3419c35d494e25ef. Report an issue: GitHub.