lib: deprecate fromHexString
on dodgy inputs (#434218)
This commit is contained in:
commit
922b6476c5
@ -392,35 +392,21 @@ runTests {
|
|||||||
expected = 9223372036854775807;
|
expected = 9223372036854775807;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testFromHexStringLeadingZeroes = {
|
||||||
|
expr = fromHexString "00ffffffffffffff";
|
||||||
|
expected = 72057594037927935;
|
||||||
|
};
|
||||||
|
|
||||||
testFromHexStringWithPrefix = {
|
testFromHexStringWithPrefix = {
|
||||||
expr = fromHexString "0Xf";
|
expr = fromHexString "0xf";
|
||||||
expected = 15;
|
expected = 15;
|
||||||
};
|
};
|
||||||
|
|
||||||
# FIXME: This might be bad and should potentially be deprecated.
|
testFromHexStringMixedCase = {
|
||||||
testFromHexStringQuestionableMixedCase = {
|
|
||||||
expr = fromHexString "eEeEe";
|
expr = fromHexString "eEeEe";
|
||||||
expected = 978670;
|
expected = 978670;
|
||||||
};
|
};
|
||||||
|
|
||||||
# FIXME: This is probably bad and should potentially be deprecated.
|
|
||||||
testFromHexStringQuestionableUnderscore = {
|
|
||||||
expr = fromHexString "F_f";
|
|
||||||
expected = 255;
|
|
||||||
};
|
|
||||||
|
|
||||||
# FIXME: This is definitely bad and should be deprecated.
|
|
||||||
testFromHexStringBadComment = {
|
|
||||||
expr = fromHexString "0 # oops";
|
|
||||||
expected = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
# FIXME: Oh my god.
|
|
||||||
testFromHexStringAwfulInjection = {
|
|
||||||
expr = fromHexString "1\nwhoops = {}";
|
|
||||||
expected = 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
testToBaseDigits = {
|
testToBaseDigits = {
|
||||||
expr = toBaseDigits 2 6;
|
expr = toBaseDigits 2 6;
|
||||||
expected = [
|
expected = [
|
||||||
|
@ -1119,14 +1119,21 @@ in
|
|||||||
```
|
```
|
||||||
*/
|
*/
|
||||||
fromHexString =
|
fromHexString =
|
||||||
value:
|
str:
|
||||||
let
|
let
|
||||||
noPrefix = lib.strings.removePrefix "0x" (lib.strings.toLower value);
|
match = builtins.match "(0x)?([0-7]?[0-9A-Fa-f]{1,15})" str;
|
||||||
in
|
in
|
||||||
let
|
if match != null then
|
||||||
parsed = builtins.fromTOML "v=0x${noPrefix}";
|
(builtins.fromTOML "v=0x${builtins.elemAt match 1}").v
|
||||||
in
|
else
|
||||||
parsed.v;
|
# TODO: Turn this into a `throw` in 26.05.
|
||||||
|
assert lib.warn "fromHexString: ${
|
||||||
|
lib.generators.toPretty { } str
|
||||||
|
} is not a valid input and will be rejected in 26.05" true;
|
||||||
|
let
|
||||||
|
noPrefix = lib.strings.removePrefix "0x" (lib.strings.toLower str);
|
||||||
|
in
|
||||||
|
(builtins.fromTOML "v=0x${noPrefix}").v;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Convert the given positive integer to a string of its hexadecimal
|
Convert the given positive integer to a string of its hexadecimal
|
||||||
|
Loading…
x
Reference in New Issue
Block a user