Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -386,35 +386,21 @@ runTests {
expected = 9223372036854775807;
};

testFromHexStringLeadingZeroes = {
expr = fromHexString "00ffffffffffffff";
expected = 72057594037927935;
};

testFromHexStringWithPrefix = {
expr = fromHexString "0Xf";
expr = fromHexString "0xf";
expected = 15;
};

# FIXME: This might be bad and should potentially be deprecated.
testFromHexStringQuestionableMixedCase = {
testFromHexStringMixedCase = {
expr = fromHexString "eEeEe";
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 = {
expr = toBaseDigits 2 6;
expected = [
Expand Down
19 changes: 13 additions & 6 deletions lib/trivial.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1119,14 +1119,21 @@ in
```
*/
fromHexString =
value:
str:
let
noPrefix = lib.strings.removePrefix "0x" (lib.strings.toLower value);
match = builtins.match "(0x)?([0-7]?[0-9A-Fa-f]{1,15})" str;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe should be case insensitive in the 0x part? idk. we did have a test for it working.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn’t find any examples of people using that in my GitHub search, and it’s inconsistent with the syntax of TOML and C (although Python does allow it). I’d prefer to err on the side of conservatism here so that we can buy ourselves back maximum freedom to determine the input space without (hopefully) breaking any existing users, though I’m not super fussed one way or the other here. We’re already in the territory of breaking the tests/examples out of necessity. Honestly I don’t even like the optional 0x here, but there are users of it in the wild, so we shouldn’t break them without good reason; I think this is the most rigid non‐arbitrary format that covers everything I saw. (Well, possibly we could disallow mixed case in the actual digits too, but I don’t see the point.)

in
let
parsed = builtins.fromTOML "v=0x${noPrefix}";
in
parsed.v;
if match != null then
(builtins.fromTOML "v=0x${builtins.elemAt match 1}").v
else
# 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
Expand Down
Loading