From 1cb8279a64f4f79615ac8358927b11237df29bf2 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Wed, 12 Aug 2026 11:20:14 -0700 Subject: Real argc/argv args Beef up BubbleSort error handling --- chapter25/src/main/smp/sys/Scan.smp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'chapter25/src/main/smp/sys/Scan.smp') diff --git a/chapter25/src/main/smp/sys/Scan.smp b/chapter25/src/main/smp/sys/Scan.smp index acf366f7..acf9fbb8 100644 --- a/chapter25/src/main/smp/sys/Scan.smp +++ b/chapter25/src/main/smp/sys/Scan.smp @@ -3,6 +3,7 @@ // Scanner; a simple string parser. struct Scan { int !x; // Cursor + bool !bad; // True after a malformed token u8[~] buf; // Immutable byte array, must be set in 2nd constructor new Scan = { u8[~] b -> @@ -19,6 +20,7 @@ struct Scan { // True if find required character, and skip it; else False. val match = { u8 c -> skip(); + if( x >= buf# ) return false; if( buf[x] != c ) return false; x++; return true; @@ -28,8 +30,12 @@ struct Scan { val number = { -> skip(); bool neg = match('-'); + if( x >= buf# || buf[x] < '0' || '9' < buf[x] ) { + bad = true; + return 0; + } i64 n = 0; - while( '0' <= buf[x] & buf[x] <= '9' ) + while( x < buf# && '0' <= buf[x] & buf[x] <= '9' ) n = n*10 + (buf[x++] - '0'); return neg ? -n : n; }; -- cgit v1.2.3