From 717cf99071d3a9093fd9cdaccb08c274055f5398 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Mon, 17 Feb 2020 17:37:59 -0600 Subject: [PATCH] Detect invalid base values in strtol and strtoul. They will now return 0 and set errno to EINVAL if an invalid base value (anything other than 0 or 2..36) is detected. This behavior is required by POSIX. It's not required by the C standards (which leave the behavior in this case undefined), but it seems reasonable to do. --- stdlib.asm | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/stdlib.asm b/stdlib.asm index dfa36f7..edd8c8a 100644 --- a/stdlib.asm +++ b/stdlib.asm @@ -982,12 +982,17 @@ db1c lda base if the base is zero then asl base base 16 bra db3 db2 cmp #16 if the base is 16 then - bne cn1 + bne db4 lda [str] if the first two chars are 0x or 0X then and #$DFFF cmp #'X0' bne cn1 db3 add4 str,#2 skip them + bra cn1 +db4 cmp #37 check for invalid base value + bge cn6 + dec a + beq cn6 ; ; Convert the number ; @@ -1041,7 +1046,7 @@ cn4 inc4 str next char cn5 lda foundOne if no digits were found, flag the error bne rt1 - lda #EINVAL +cn6 lda #EINVAL sta >errno bra rt2a ;