Skip to content

Commit

Permalink
math.parser: require hex-string>bytes to have even digits
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Feb 12, 2024
1 parent 2761628 commit 346071b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/math/parser/parser-tests.factor
Expand Up @@ -480,6 +480,7 @@ unit-test

{ "deadbeef" } [ B{ 222 173 190 239 } bytes>hex-string ] unit-test
{ B{ 222 173 190 239 } } [ "deADbeEF" hex-string>bytes ] unit-test
[ "0" hex-string>bytes ] [ invalid-hex-string-length? ] must-fail-with

{
B{ 49 46 53 53 69 43 48 53 }
Expand Down
4 changes: 3 additions & 1 deletion core/math/parser/parser.factor
Expand Up @@ -1019,8 +1019,10 @@ M: float >base

: # ( n -- ) number>string % ; inline

ERROR: invalid-hex-string-length n ;

: hex-string>bytes ( hex-string -- bytes )
dup length 2/ <byte-array> [
dup length dup even? [ invalid-hex-string-length ] unless 2/ <byte-array> [
[
[ digit> ] 2dip over even? [
[ 16 * ] [ 2/ ] [ set-nth-unsafe ] tri*
Expand Down

0 comments on commit 346071b

Please sign in to comment.