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.
This commit is contained in:
Stephen Heumann 2020-02-17 17:37:59 -06:00
parent e5360c9605
commit 717cf99071
1 changed files with 7 additions and 2 deletions

View File

@ -982,12 +982,17 @@ db1c lda base if the base is zero then
asl base base 16 asl base base 16
bra db3 bra db3
db2 cmp #16 if the base is 16 then 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 lda [str] if the first two chars are 0x or 0X then
and #$DFFF and #$DFFF
cmp #'X0' cmp #'X0'
bne cn1 bne cn1
db3 add4 str,#2 skip them 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 ; Convert the number
; ;
@ -1041,7 +1046,7 @@ cn4 inc4 str next char
cn5 lda foundOne if no digits were found, flag the error cn5 lda foundOne if no digits were found, flag the error
bne rt1 bne rt1
lda #EINVAL cn6 lda #EINVAL
sta >errno sta >errno
bra rt2a bra rt2a
; ;