freebsd-src icon indicating copy to clipboard operation
freebsd-src copied to clipboard

kvprintf(): Fix '+' conversion handling

Open sebhub opened this issue 1 year ago • 3 comments

For example, printf("%+i", 1) prints "+1". However, kvprintf() did print just "1" for this example. According to PRINTF(3):

A sign must always be placed before a number produced by a signed conversion.

For "%+r" radix conversions, keep the "+" handling as it is, since this is a non-standard conversion.

This change allows to support the ' ' conversion modifier in the future.

sebhub avatar Jul 01 '24 06:07 sebhub

This looks like it changes the meaning of both '%-p' and '%+p'. before it would always set sign to 0. Now it doesn't which seems like it would be weird.

bsdimp avatar Jul 03 '24 04:07 bsdimp

I have to admit that I did test the '%+p' using glibc. It seems that it is not defined by POSIX.

#include <stdio.h>

void print(void *p)
{
   printf("'%20p' '%-20p' '%+20p'\n", p, p, p);
}

int main()
{
  print(0);
  print((void*)1L);
  print((void*)-1L);
  return 0;
}

On FreeBSD:

cc -Wall -Wextra -pedantic main.c
main.c:5:29: warning: flag '+' results in undefined behavior with 'p' conversion specifier [-Wformat]
   printf("'%20p' '%-20p' '%+20p'\n", p, p, p);
                           ~^~~~
1 warning generated.
./a.out
'                 0x0' '0x0                 ' '                 0x0'
'                 0x1' '0x1                 ' '                 0x1'
'  0xffffffffffffffff' '0xffffffffffffffff  ' '  0xffffffffffffffff'

On glibc:

cc -Wall -Wextra -pedantic main.c
main.c: In function ‘print’:
main.c:5:32: warning: '+' flag used with ‘%p’ gnu_printf format [-Wformat=]
    printf("'%20p' '%-20p' '%+20p'\n", p, p, p);
                                ^
./a.out
'               (nil)' '(nil)               ' '               (nil)'
'                 0x1' '0x1                 ' '                +0x1'
'  0xffffffffffffffff' '0xffffffffffffffff  ' ' +0xffffffffffffffff'

sebhub avatar Jul 03 '24 05:07 sebhub

Please let me know if you are in principle fine with this change. I can change the '%+p' behaviour to match with the FreeBSD user space.

sebhub avatar Jul 04 '24 08:07 sebhub

I changed the patch to keep the current "%+p" behaviour.

sebhub avatar Jul 12 '24 06:07 sebhub

Sorry missed these comments... I think I'm fine with the change (either way, but happier with it matching user space better). I started looking at this, then got distracted and didn't get back to it.

bsdimp avatar Jul 19 '24 05:07 bsdimp

Anything left to do on my side?

sebhub avatar Aug 09 '24 13:08 sebhub

Anything left to do on my side?

No. I think it's ready to go, but am not sure. Was going to look closely on my next pr landing day... but last friday and this have gotten away from me so it will be maybe monday or tiesday this next week when i hope to do a mini one.

bsdimp avatar Aug 09 '24 15:08 bsdimp