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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

ng boon khai
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.

Reply | Threaded
Open this post in threaded view
|

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

ng boon khai
i'm using new lib version
#define _NEWLIB_VERSION "3.1.0"
________________________________
From: ng boon khai
Sent: Saturday, March 7, 2020 4:30 PM
To: [hidden email] <[hidden email]>
Subject: There is a bug on sprintf newlib when using specifier %-*.llu

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.

Reply | Threaded
Open this post in threaded view
|

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

Richard Damon
In reply to this post by ng boon khai
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