printf: string precision "%.*s" give (null) string

Lennart Sorensen lsorense at csclub.uwaterloo.ca
Mon Nov 9 15:26:35 UTC 2009


On Mon, Nov 09, 2009 at 10:00:22AM +0100, Peter Lukac wrote:
> On Friday 06 November 2009 04:49:53 pm you wrote:
> > On Fri, Nov 06, 2009 at 04:15:14PM +0100, Peter Lukac wrote:
> > > Hello,
> > > I use uclibc version 0.9.29 on arm.
> > > I think that function printf not work right with precision for string.
> > >
> > > here is my simple program:
> > >
> > > #include <stdio.h>
> > > int main()
> > > {
> > >     char *p = NULL;
> > >     printf("\n--->%.*s<---", 0, p);
> > >     printf("\n--->%.*s<---", 3, p);
> > >     printf("\n--->%.*s<---\n", 6, p);
> > >
> > >     return 0;
> > > }
> > >
> > > on glibc it works fine. i get output :
> > > ---><---
> > > ---><---
> > > --->(null)<---
> > >
> > > but in uclibc i get
> > >
> > > --->(null)<---
> > > --->(null)<---
> > > --->(null)<---
> > >
> > > I think that if precision .* parameter is 0 or is less as string length,
> > > printf should be print empty string "" and not "(null)" string.
> > >
> > > Is it bug or feature? :)
> > >
> > > i'm using pjlib where is used lot of code with "%.*s" argument which
> > > don't work correctly.
> > >
> > > ...have a nice day
> >
> > What does it do with real strings rather than the odd '(null)' label
> > for a non existant string?
> 
> Hello,
> Hmm...do you want know how it works with real string? here is example:
> 
> #include <stdio.h>
> int main()
> {
>     char *p = NULL;
>     const char *p2 = "Hello World";
> 
>     printf("\n--->%.*s<---", 0, p);
>     printf("\n--->%.*s<---", 3, p);
>     printf("\n--->%.*s<---\n", 6, p);
> 
>     printf("\n--->%.*s<---", 0, p2);
>     printf("\n--->%.*s<---", 4, p2);
>     printf("\n--->%.*s<---\n", 11, p2);
>     return 0;
> }
> 
> with glibc on i386 :
> 
> ---><---
> ---><---
> --->(null)<---
> 
> ---><---
> --->Hell<---
> --->Hello World<---
> 
> and on ARM with uclibc:
> 
> --->(null)<---
> --->(null)<---
> --->(null)<---
> 
> ---><---
> --->Hell<---
> --->Hello World<---
> 
> 
> i think with real string it works fine...

I have never personally noticed you could pass NULL to printf and have
it print out that string.  I guess I learned something new (and in my
opinion completely useless).  I wonder if such is even defined anywhere on
how to handle it with limited space.  To some extent the glibc way you
can't tell the difference between printing the first up to 3 characters
of an empty string and a NULL pointer.  With uclibc you can.  That seems
almost like a feature to me.  Maybe glibc should have printed '(nu'
instead.  Of course printing out (null) is just about a debug feature
to tell the programmer they screwed up and tried to print a null pointer.
In that case what uclibc does it more useful than what glibc does.

-- 
Len Sorensen


More information about the uClibc mailing list