Merge pull request #52392 from nextcloud/fix/32bit-support

fix(32bit): make `pack` compatible with 32bit PHP
pull/52400/head
Côme Chilliet 2025-04-24 09:53:20 +07:00 committed by GitHub
commit 7a50f612d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 2 deletions

@ -41,10 +41,14 @@ class IpAddress {
$config = \OCP\Server::get(IConfig::class);
$maskSize = min(64, $config->getSystemValueInt('security.ipv6_normalized_subnet_size', 56));
$maskSize = max(32, $maskSize);
$mask = pack('VVP', (1 << 32) - 1, (1 << $maskSize - 32) - 1, 0);
if (PHP_INT_SIZE === 4) {
// as long as we support 32bit PHP we cannot use the `P` pack formatter (and not overflow 32bit integer)
$mask = pack('VVVV', 0xFFFF, $maskSize === 64 ? 0xFFFF : ((1 << $maskSize - 32) - 1), 0, 0);
} else {
$mask = pack('VVP', (1 << 32) - 1, (1 << $maskSize - 32) - 1, 0);
}
$binary = \inet_pton($ip);
return inet_ntop($binary & $mask) . '/' . $maskSize;
}