diff mbox series

[wic,4/5] ksparser: require an explicit 0x prefix for --system-id

Message ID 20260715223226.3371498-5-twoerner@gmail.com
State New
Headers show
Series ksparser: four argparse-type fixes plus unit coverage | expand

Commit Message

Trevor Woerner July 15, 2026, 10:32 p.m. UTC
systemidtype() validates the MBR partition type as "hex between 0x1 and
0xFF", but it converts with int(arg, 16), which also accepts a bare "82"
(parsed as hex 0x82 = 130) and a signed form like "+0x82". Those inputs
contradict the documented contract and are silently returned unchanged,
so the raw string later handed to sfdisk may not be what the user meant.

Require an explicit 0x/0X-prefixed hex string before converting, so only
the documented form is accepted and every other spelling raises the
existing ArgumentTypeError.

AI-Generated: codex/claude-opus 4.8 (xhigh)
Signed-off-by: Trevor Woerner <twoerner@gmail.com>
---
 src/wic/ksparser.py | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/src/wic/ksparser.py b/src/wic/ksparser.py
index 528a8180826f..dcf26f788083 100644
--- a/src/wic/ksparser.py
+++ b/src/wic/ksparser.py
@@ -132,10 +132,12 @@  def systemidtype(arg):
     """
     error = "Invalid system type: %s. must be hex "\
             "between 0x1 and 0xFF" % arg
-    try:
-        result = int(arg, 16)
-    except ValueError:
+    # int(arg, 16) also accepts a bare "82" (-> 130) and a leading sign
+    # like "+0x82"; the documented contract is an explicit 0x-prefixed
+    # hex byte, so require that form before converting.
+    if not re.fullmatch(r"0[xX][0-9a-fA-F]+", arg or ""):
         raise ArgumentTypeError(error)
+    result = int(arg, 16)
 
     if result <= 0 or result > 0xff:
         raise ArgumentTypeError(error)