Make strtol() and strtoul() set the right end pointer in error cases.

Previously, they often did not do this. Now they do, as follows:

If there was a sequence of the expected numeric form, then they fully parse that sequence and give a pointer past the end of it, even if the number was out of range.  If there was not a sequence of the expected form, they give the starting pointer that they were passed in.
This commit is contained in:
Stephen Heumann
2020-01-23 23:33:29 -06:00
parent ebbb5b73ed
commit 14554fcdc7

View File

@@ -795,9 +795,9 @@ rtl equ 7 return address
val equ 3 value val equ 3 value
negative equ 1 is the number negative? negative equ 1 is the number negative?
pea 0 make room for & initialize negative
pea 0 make room for & initialize val pea 0 make room for & initialize val
pea 0 pea 0
pea 0 make room for & initialize negative
tsc set up direct page addressing tsc set up direct page addressing
phd phd
tcd tcd
@@ -856,14 +856,6 @@ cn3 ph4 str save the starting string
iny iny
ov1 sty val ov1 sty val
stx val+2 stx val+2
lda ptr if ptr <> NULL then
ora ptr+2
bne rt1
lda 1,S *ptr = original str
sta [ptr]
ldy #2
lda 3,S
sta [ptr],Y
; ;
; return the results ; return the results
; ;
@@ -907,11 +899,12 @@ rt2 ldx val+2 get the value
**************************************************************** ****************************************************************
* *
strtoul start strtoul start
base equ 20 base base equ 22 base
ptr equ 16 *return pointer ptr equ 18 *return pointer
str equ 12 string pointer str equ 14 string pointer
rtl equ 9 return address rtl equ 11 return address
rangeOK equ 9 was the number within range?
negative equ 7 was there a minus sign? negative equ 7 was there a minus sign?
val equ 3 value val equ 3 value
foundOne equ 1 have we found a number? foundOne equ 1 have we found a number?
@@ -922,27 +915,29 @@ foundOne equ 1 have we found a number?
~strtoul entry alt entry point called from strtol ~strtoul entry alt entry point called from strtol
ldx #1 ldx #1
init pea 0 make room for & initialize negative init pea 1 make room for & initialize rangeOK
pea 0 make room for & initialize foundOne pea 0 make room for & initialize negative
pea 0 make room for & initialize val pea 0 make room for & initialize val
pea 0 pea 0
pea 0 make room for & initialize foundOne
tsc set up direct page addressing tsc set up direct page addressing
phd phd
tcd tcd
; ;
; Skip any leading whitespace ; Skip any leading whitespace
; ;
txa just process number if called from strtol
bne db1c
lda ptr if ptr in non-null then lda ptr if ptr in non-null then
ora ptr+2 ora ptr+2
beq sw0 beq sw1
lda str initialize it to str lda str initialize it to str
sta [ptr] sta [ptr]
ldy #2 ldy #2
lda str+2 lda str+2
sta [ptr],Y sta [ptr],Y
sw0 txa just process number if called from strtol
bne db1c
sw1 lda [str] skip the white space sw1 lda [str] skip the white space
and #$00FF and #$00FF
tax tax
@@ -1025,14 +1020,16 @@ cn3 cmp base branch if the digit is too big
plx plx
ply ply
tax tax
bne returnERANGE beq cn3a
clc add in the new digit stz rangeOK
cn3a clc add in the new digit
tya tya
adc val adc val
sta val sta val
bcc cn4 bcc cn4
inc val+2 inc val+2
beq returnERANGE bne cn4
stz rangeOK
cn4 inc4 str next char cn4 inc4 str next char
bra cn1 bra cn1
@@ -1040,27 +1037,26 @@ cn5 lda foundOne if no digits were found, flag the error
bne rt1 bne rt1
lda #EINVAL lda #EINVAL
sta >errno sta >errno
bra rt2 bra rt2a
;
; flag an error
;
returnERANGE anop
lda #ERANGE errno = ERANGE
sta >errno
ldx #$FFFF return value = ULONG_MAX
txy
bra rt3 skip setting ptr
; ;
; return the results ; return the results
; ;
rt1 lda ptr if ptr is non-null then rt1 lda ptr if ptr is non-null then
ora ptr+2 ora ptr+2
beq rt2 beq rt1a
lda str set it to str lda str set it to str
sta [ptr] sta [ptr]
ldy #2 ldy #2
lda str+2 lda str+2
sta [ptr],Y sta [ptr],Y
rt1a lda rangeOK check if number was out of range
bne rt2
lda #ERANGE errno = ERANGE
sta >errno
ldx #$FFFF return value = ULONG_MAX
txy
bra rt3
rt2 lda negative if negative then rt2 lda negative if negative then
beq rt2a beq rt2a
sub4 #0,val,val val = -val sub4 #0,val,val val = -val
@@ -1073,7 +1069,7 @@ rt3 lda rtl fix the stack
pld pld
tsc tsc
clc clc
adc #18 adc #20
tcs tcs
tya return tya return
rtl rtl