From 346071b4f165400aab7fa7d2b03ebd552fff7502 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Mon, 12 Feb 2024 10:42:08 -0800 Subject: [PATCH] math.parser: require hex-string>bytes to have even digits --- core/math/parser/parser-tests.factor | 1 + core/math/parser/parser.factor | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/core/math/parser/parser-tests.factor b/core/math/parser/parser-tests.factor index 9e3b3d2159..13e4514c43 100644 --- a/core/math/parser/parser-tests.factor +++ b/core/math/parser/parser-tests.factor @@ -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 } diff --git a/core/math/parser/parser.factor b/core/math/parser/parser.factor index 2e1d5a4b0f..778c754981 100644 --- a/core/math/parser/parser.factor +++ b/core/math/parser/parser.factor @@ -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/ [ + dup length dup even? [ invalid-hex-string-length ] unless 2/ [ [ [ digit> ] 2dip over even? [ [ 16 * ] [ 2/ ] [ set-nth-unsafe ] tri* -- 2.34.1