There is a bug on sprintf newlib when using specifier %-*.llu

Richard Damon Richard@Damon-Family.org
Sat Mar 7 11:39:33 GMT 2020


On 3/7/20 3:30 AM, ng boon khai wrote:
> Hi,
>
> I found a bug when using the newlib sprinf using the spcifier %-*.llu
>
> Below is the code snippet
>
> function (){
>    int64_t longval;
>    longval=(int64_t)(1);
>    char comparebuf[2000];
>    sprintf(comparebuf, "testing string! arg1=%-*.llu, arg2=%-*.llu",1,longval,1,longval);
>    printf(comparebuf);
> }
>
> And i get this outcome
> testing string! arg1=499644, arg2=
>
> Expected outcome should be
> testing string! arg1=1, arg2=1
>
> Regards,
> Ng Boon Khai.
>
One issue with your program is that you have a variable of type int64_t 
but using a format that expects an unsigned long long.

In some implementations, long long might be 64 bits, but in others long 
long will be 128 bits.

If you want to print an int64_t, you should be using the format 
specified from the macro PRId64

If you want to use llu, you should be using a variable of type unsigned 
long long

-- 
Richard Damon




More information about the Newlib mailing list