diff --git a/lib/os.js b/lib/os.js index 055710f5620ee2..0049a9af820eb2 100644 --- a/lib/os.js +++ b/lib/os.js @@ -28,7 +28,6 @@ const { ObjectDefineProperties, StringPrototypeEndsWith, StringPrototypeSlice, - StringPrototypeSplit, SymbolToPrimitive, } = primordials; @@ -225,6 +224,7 @@ function getCIDR(address, netmask, family) { let range = 10; let groupLength = 8; let hasZeros = false; + let lastPos = 0; if (family === 'IPv6') { split = ':'; @@ -232,21 +232,30 @@ function getCIDR(address, netmask, family) { groupLength = 16; } - const parts = StringPrototypeSplit(netmask, split); - for (let i = 0; i < parts.length; i++) { - if (parts[i] !== '') { - const binary = NumberParseInt(parts[i], range); - const tmp = countBinaryOnes(binary); - ones += tmp; + for (let i = 0; i < netmask.length; i++) { + if (netmask[i] !== split) { + if (i + 1 < netmask.length) { + continue; + } + i++; + } + const part = StringPrototypeSlice(netmask, lastPos, i); + lastPos = i + 1; + if (part !== '') { if (hasZeros) { - if (tmp !== 0) { + if (part !== '0') { return null; } - } else if (tmp !== groupLength) { - if ((binary & 1) !== 0) { - return null; + } else { + const binary = NumberParseInt(part, range); + const binaryOnes = countBinaryOnes(binary); + ones += binaryOnes; + if (binaryOnes !== groupLength) { + if ((binary & 1) !== 0) { + return null; + } + hasZeros = true; } - hasZeros = true; } } }