lionsoul2014/ip2region · error · Error

invalid ipv4 part '${ps[i]}', a valid number expected

Error message

invalid ipv4 part '${ps[i]}', a valid number expected

What it means

Octet-level parse failure in the IPv4 parser: the field had 4 dot-separated parts, but parseInt(part, 10) returned NaN for at least one octet, meaning that component is not a decimal number (e.g. '1.two.3.4' or an empty segment like '1..3.4').

Source

Thrown at binding/javascript/util.js:62

        }`;
    }
}

// ---

// parse ipv4 address
function _parse_ipv4_addr(v4String) {
    let ps = v4String.split('.', 4);
    if (ps.length != 4) {
        throw new Error('invalid ipv4 address');
    }

    var v;
    const ipBytes =  Buffer.alloc(4);
    for (var i = 0; i < ps.length; i++) {
        v = parseInt(ps[i], 10);
        if (isNaN(v)) {
            throw new Error(`invalid ipv4 part '${ps[i]}', a valid number expected`);
        }

        if (v < 0 || v > 255) {
            throw new Error(`invalid ipv4 part '${ps[i]}' should >= 0 and <= 255`);
        }

        ipBytes[i] = (v & 0xFF);
    }

    return ipBytes;
}

// parse ipv6 address
function _parse_ipv6_addr(v6String) {
    let ps = v6String.split(':', 8);
    if (ps.length < 3) {
        throw new Error('invalid ipv6 address');
    }

View on GitHub (pinned to c1a1fc7d59)

Solutions

  1. Correct the offending octet so every part is a base-10 integer
  2. Pre-validate each octet with a numeric check or a full IPv4 regex before parsing
  3. Strip whitespace and reject empty segments before handing the string to parseIP
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at binding/javascript/util.js:62 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/a16f72b80e6d7b77. Report an issue: GitHub.