printf: properly handle negative field width or precision arguments.

If the field width is given by an argument (when * is used in the format specification) and the value given is negative, it should be treated like a positive field width with the - flag. If a negative argument is given for the precision, it should be treated as if the precision was not specified.

The following is an example of code that behaved incorrectly:

#include <stdio.h>
int main(void) {
        printf("%*iXX\n", -50, 123);
}
This commit is contained in:
Stephen Heumann 2021-01-30 16:54:54 -06:00
parent 71a25f0fef
commit 7c4141f03d
1 changed files with 11 additions and 1 deletions

View File

@ -4544,7 +4544,17 @@ GetSize stz val assume a value of 0
bne gs1
inc4 format skip the '*' char
lda [argp] fetch the value
sta val
bpl fv1 do adjustments if negative:
ldx ~precisionSpecified
bne fv0
eor #$ffff negative field width is like
inc a positive with - flag
ldx #1
stx ~leftJustify
bra fv1
fv0 lda #0 negative precision is ignored
stz ~precisionSpecified
fv1 sta val
inc argp remove it from the argument list
inc argp
gs0 lda val