1
1
Fork 1
mirror of https://github.com/oddlama/nixos-extra-modules.git synced 2025-10-11 06:10:39 +02:00

assign{Ips,Macs}: Fix integer overflow

Half of SHA256 sums truncated to 16 chars actually go over 2^63,
which is beyond the range for hexToDec.
This fixes the integer overflow error in Lix 2.91+ and Nix 2.25+,
as well as undefined behaviour for versions below
This commit is contained in:
Geoffrey “Frogeye” Preud'homme 2025-04-07 23:14:37 +02:00
parent c2dfcdd9f7
commit 612e974ace
No known key found for this signature in database
GPG key ID: C72403E7F82E6AD8

View file

@ -230,7 +230,7 @@ in {
# Generates a hash (i.e. offset value) for a given hostname # Generates a hash (i.e. offset value) for a given hostname
hashElem = x: hashElem = x:
builtins.bitAnd (capacity - 1) builtins.bitAnd (capacity - 1)
(hexToDec (builtins.substring 0 16 (builtins.hashString "sha256" x))); (hexToDec (builtins.substring 0 15 (builtins.hashString "sha256" x)));
# Do linear probing. Returns the first unused value at or after the given value. # Do linear probing. Returns the first unused value at or after the given value.
probe = avoid: value: probe = avoid: value:
if elem value avoid if elem value avoid
@ -317,7 +317,7 @@ in {
# Generates a hash (i.e. offset value) for a given hostname # Generates a hash (i.e. offset value) for a given hostname
hashElem = x: hashElem = x:
builtins.bitAnd (capacity - 1) builtins.bitAnd (capacity - 1)
(hexToDec (substring 0 16 (builtins.hashString "sha256" x))); (hexToDec (substring 0 15 (builtins.hashString "sha256" x)));
# Do linear probing. Returns the first unused value at or after the given value. # Do linear probing. Returns the first unused value at or after the given value.
probe = avoid: value: probe = avoid: value:
if elem value avoid if elem value avoid