MEGA65 / mega65-rom-public

MEGA65 ROM public issue reporting
4 stars 0 forks source link

VAL reads extra byte if string represents a negative number #115

Closed dansanderson closed 5 months ago

dansanderson commented 5 months ago

Test Environment (required) You can use MEGA65INFO to retrieve this.

Describe the bug VAL() interprets a string value as a number and returns the number. In the edge case where the string represents a negative number and the next byte in string value memory is a PETSCII digit, VAL will read the extra digit as if it were part of a number.

To Reproduce The easiest way to reproduce is to set a string variable, locate its string value memory, poke a digit at the end, then VAL(). One way to do this in BASIC:

BANK 0
A$="-5"
POKE $10000+WPEEK(POINTER(A$)+1)+2,ASC("6")
PRINT A$,VAL(A$)

This incorrectly prints: -5 -56 It should print -5 -5

Notice counter case where A$ is positive, e.g.: A$="45"

dansanderson commented 5 months ago

b65.src get_number accepts a maximum length, which in this case is the string length. But it is not decrementing the maximum length when consuming the sign. I think it just needs a dec count after 10$ inw txtptr but we should test this thoroughly, as it is used for all number parsing in BASIC.