Rockbox.org home
release
dev builds
extras
themes manual
wiki
device status forums
mailing lists
IRC bugs
patches
dev guide



Rockbox mail archive

Subject: Re: barrywardell: r12838 - trunk/firmware/common

Re: barrywardell: r12838 - trunk/firmware/common

From: Barry Wardell <barry.wardell_at_gmail.com>
Date: Mon, 19 Mar 2007 19:51:42 +0000

On 3/19/07, Magnus Holmgren <lear_at_algonet.se> wrote:
> mailer_at_svn.rockbox.org wrote:
>
> > Log Message:
> > Add support for the .precision format in the sprintf()-like functions
> > to allow limiting the maximum length of a string.
>
> <...>
>
> > case 's':
> > str = va_arg (ap, char*);
> > + if(precision > 0)
> > + str[precision] = '\0';
>
> Should it really modify the input string like that? What if it is in
> flash? (Might not be a problem as currently used, but...)
>
> Magnus
>
>
Good point. Would this be a better way to do it:

Index: firmware/common/sprintf.c
===================================================================
--- firmware/common/sprintf.c (revision 12842)
+++ firmware/common/sprintf.c (working copy)
_at__at_ -27,6 +27,7 _at__at_
 #include <stdarg.h>
 #include <string.h>
 #include <stdbool.h>
+#include <limits.h>

 #include "file.h" /* for write(), used in fprintf() */
 #include "sprintf.h" /* to allow the simulator magic */
_at__at_ -75,6 +76,8 _at__at_
                 precision = 10*precision + ch - '0';
                 ch = *fmt++;
             }
+ } else {
+ precision = INT_MAX;
         }

         str = tmpbuf + sizeof tmpbuf - 1;
_at__at_ -86,8 +89,6 _at__at_

         case 's':
             str = va_arg (ap, char*);
- if(precision > 0)
- str[precision] = '\0';
             break;

         case 'd':
_at__at_ -160,7 +161,7 _at__at_
         while (width-- > 0 && ok)
             ok=push(userp, pad);
         }
- while (*str != '\0' && ok)
+ while (*str != '\0' && ok && precision--)
                 ok=push(userp, *str++);
     }
     else
Received on 2007-03-19

Page template was last modified "Tue Sep 7 00:00:02 2021" The Rockbox Crew -- Privacy Policy