[PATCH v2] Support C2X printf %b, %B

Joseph Myers joseph@codesourcery.com
Wed Nov 10 15:30:08 GMT 2021


On Wed, 10 Nov 2021, Florian Weimer wrote:

> * Joseph Myers:
> 
> > +@item @samp{%b}, @samp{%B}
> > +Print an integer as an unsigned binary number.  @samp{%b} uses
> > +lower-case @samp{b} with the @samp{#} flag and @samp{%B} uses
> > +upper-case.  @samp{%b} is an ISO C2X feature; @samp{%B} is an
> > +extension recommended by ISO C2X.  @xref{Integer Conversions}, for
> > +details.
> 
> The new tests do not contain any negative input numbers.  Do they result
> in undefined behavior?

Negative input numbers result in undefined behavior when the specified 
argument type is unsigned after the integer promotions, as with %b with no 
length modifiers, since printf is specified to behave as if it uses va_arg 
(with the promoted version of the specified argument type), and mixing 
signed / unsigned types with va_arg is only valid when the value passed is 
representable in both types.

Negative (int) input numbers are valid when the specified argument type is 
int (promoted from a narrower unsigned type), and the tests do include 
cases passing (int) 0x87654321 to %hb, %hB, %hhb, %hhB to test that.

-- 
Joseph S. Myers
joseph@codesourcery.com


More information about the Libc-alpha mailing list