On Mon, Oct 13, 2025 at 04:03:28PM +0200, Heinrich Schuchardt wrote:
> On 10/13/25 15:53, Daniel Kiper wrote:
> > On Thu, Oct 09, 2025 at 03:18:34PM +0800, Michael Chang via Grub-devel 
> > wrote:
> > > Add support for the 'z' length modifier in the printf code. This allows
> > > printing of size_t and ssize_t values using %zu, %zd and related
> > > formats. The parser maps 'z' to the correct integer width based on
> > > sizeof (size_t).
> > > 
> > > Signed-off-by: Michael Chang <[email protected]>
> > > ---
> > >   grub-core/kern/misc.c | 20 ++++++++++++++++++++
> > >   1 file changed, 20 insertions(+)
> > > 
> > > diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
> > > index 2b7922393..ff614add5 100644
> > > --- a/grub-core/kern/misc.c
> > > +++ b/grub-core/kern/misc.c
> > > @@ -26,6 +26,7 @@
> > >   #include <grub/i18n.h>
> > >   #include <grub/types.h>
> > >   #include <grub/charset.h>
> > > +#include <stddef.h>
> > > 
> > >   union printf_arg
> > >   {
> > > @@ -739,6 +740,9 @@ parse_printf_arg_fmt (const char *fmt0, struct 
> > > printf_args *args,
> > >     COMPILE_TIME_ASSERT (sizeof (long) <= sizeof (long long));
> > >     COMPILE_TIME_ASSERT (sizeof (long long) == sizeof (void *)
> > >                          || sizeof (int) == sizeof (void *));
> > > +  COMPILE_TIME_ASSERT (sizeof (size_t) == sizeof (unsigned)
> > > +                || sizeof (size_t) == sizeof (unsigned long)
> > > +                || sizeof (size_t) == sizeof (unsigned long long));
> > > 
> > >     fmt = fmt0;
> > >     while ((c = *fmt++) != 0)
> > > @@ -773,11 +777,17 @@ parse_printf_arg_fmt (const char *fmt0, struct 
> > > printf_args *args,
> > >           fmt++;
> > > 
> > >         c = *fmt++;
> > > +      if (c == 'z')
> > > + {
> > > +   c = *fmt++;
> > > +   goto do_count;
> > > + }
> > >         if (c == 'l')
> > >           c = *fmt++;
> > >         if (c == 'l')
> > >           c = *fmt++;
> > > 
> > > + do_count:
> > >         switch (c)
> > >           {
> > >           case 'p':
> > > @@ -874,6 +884,14 @@ parse_printf_arg_fmt (const char *fmt0, struct 
> > > printf_args *args,
> > >             continue;
> > >           }
> > > 
> > > +      if (c == 'z')
> > > + {
> > > +   c = *fmt++;
> > > +   if (sizeof (size_t) == sizeof (unsigned long))
> > 
> > This...
> > 
> > > +     longfmt = 1;
> > > +   else if (sizeof (size_t) == sizeof (unsigned long long))
> > 
> > ... and this should be build time check...
> 
> Hello Daniel,
> 
> The compiler will evaluate the current ifs at build time if you don't build
> with O0.
> 
> Do you prefer using #if to if?
> 
> Other projects go the opposite way. In U-Boot we try to replace #if by if
> where possible to ensure that even if a build time check is made the code in
> the unused branch is syntax checked.

Thanks a lot for the clarification.

Actually I was also thinking about using the #if preprocessor directive
at the beginning, but it does not work with the sizeof() operator
because that is evaluated by the compiler, not the preprocessor. I
prefer using sizeof() instead of a constant like SIZE_MAX, since it is
already used in COMPILE_TIME_ASSERT and looks more consistent. I also
have faith in the compiler should optimize it away so there will be no
runtime branch.

Thanks,
Michael

> 
> Best regards
> 
> Heinrich
> 
> > 
> > > +     longfmt = 2;
> > > + }
> > 
> > Daniel
> > 
> > _______________________________________________
> > Grub-devel mailing list
> > [email protected]
> > https://lists.gnu.org/mailman/listinfo/grub-devel
> 

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to