fix unrelated bug when fromStr input is short hex

For example, 0x0 will generate an unhandled ValueError. This was caught
during a random unit test run for 3.9, so I figure I'd fix it.

This is unrelated to the PR otherwise.
This commit is contained in:
Derek Arnold
2024-09-15 16:20:50 -05:00
parent e561222ea7
commit 34f9be255e
2 changed files with 9 additions and 1 deletions

View File

@@ -650,3 +650,11 @@ def test_fuzz_fromStr(valstr):
assert isinstance(result, int)
except ValueError:
assert isinstance(result, str)
def test_shorthex():
result = fromStr('0x0')
assert result == b'\x00'
result = fromStr('0x5')
assert result == b'\x05'
result = fromStr('0xffff')
assert result == b'\xff\xff'

View File

@@ -82,7 +82,7 @@ def fromStr(valstr):
val = bytes()
elif valstr.startswith("0x"):
# if needed convert to string with asBytes.decode('utf-8')
val = bytes.fromhex(valstr[2:])
val = bytes.fromhex(valstr[2:].zfill(2))
elif valstr.startswith("base64:"):
val = base64.b64decode(valstr[7:])
elif valstr.lower() in {"t", "true", "yes"}: