This is the mail archive of the newlib@sources.redhat.com mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: printf bug: printf("%.f",0.1) == 2 !?


Hello,

The problem doesnt remain by giving %1f in the following program right?

In the  patch you have given  for precision  setting , I wanted to clarify
something...

The variable "prec"  is of type  int  . Hence format  %.f  is  writing 2
bytes into array buf?
Hence returning 2 bytes ?
 ~~~~~
  Also I want to know ,

   Is there a bound checking done in sprintf() ?
  Anyone can  please brief me the implementation of sprintf  on Newlib-1.11
that supports  C99 std also ?

  Thanks,
     ^      _
    | \/ () | |-| | .
(_//


----- Original Message -----
From: "Honda Hiroki" <hhonda@ipflex.com>
To: <newlib@sources.redhat.com>
Sent: Tuesday, July 29, 2003 3:58 PM
Subject: printf bug: printf("%.f",0.1) == 2 !?


> Hi,
>
> I ported newlib-1.11.0 to an inhouse embedded system (with an original
> CPU) and noticed that
>
>   #include <stdio.h>
>   char buf[16];
>   int main(void) {
>     int  n = sprintf(buf, "%.f", 0.1);
>     printf("[%s] %d\n", buf, n);       /* should print "[0] 1" */
>     return 0;
>   }
>
> prints "[0] 2".
>
> The attached one-line patch fixed the problem,
> but I am not sure whether or not the patch has evil side effects.
>
> Is there any moderator who will investigate this problem?
>
> Regards,
>
> ====
> HONDA Hiroki
> R&D Center, IP Flex Inc.
> http://www.ipflex.com/english/index.html
>
> ================================================================
> --- newlib-1.11.0/newlib/libc/stdio/vfprintf.c.orig  2 Jul 2003
02:27:30 -0000
> +++ newlib-1.11.0/newlib/libc/stdio/vfprintf.c  29 Jul 2003 10:04:04 -0000
> @@ -830,7 +830,7 @@
>                                         if (prec || flags & ALT)
>                                                 size += prec + 1;
>                                 } else  /* "0.X" */
> -                                       size = prec + 2;
> +                                       size = (prec || flags & ALT) ? 2 +
prec : 1;
>                         } else if (expt >= ndig) {      /* fixed g fmt */
>                                 size = expt;
>                                 if (flags & ALT)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]