This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: [Patch, Newlib-nano]Improve printf to support non nul-terminated string
- From: "Bin.Cheng" <amker dot cheng at gmail dot com>
- To: "newlib at sourceware dot org" <newlib at sourceware dot org>
- Date: Thu, 6 Nov 2014 17:41:55 +0800
- Subject: Re: [Patch, Newlib-nano]Improve printf to support non nul-terminated string
- Authentication-results: sourceware.org; auth=none
- References: <000001cff582$34765e80$9d631b80$ at arm dot com> <20141105091542 dot GS28636 at calimero dot vinschen dot de> <CAHFci2-i-XGfCZegHGkc1vMZ5eZkSBB1nWBVnGAYDA3FTLYdDQ at mail dot gmail dot com> <001e01cff976$25581360$70083a20$ at arm dot com> <CAL0py27+a0gkNU4_j0spi6ar0XAEuzKO6pge68UvMYPfLizEaw at mail dot gmail dot com> <CAHFci29-J2fnt-2fxiGLGjGTz4k=nnsGB-ur=M2oZ665vf2_HQ at mail dot gmail dot com> <002801cff985$f19d7af0$d4d870d0$ at arm dot com> <20141106090103 dot GB28195 at calimero dot vinschen dot de>
On Thu, Nov 6, 2014 at 5:01 PM, Corinna Vinschen <vinschen@redhat.com> wrote:
>
> [Please don't CC me. I'm reading the mailing list anyway. Thanks]
>
> On Nov 6 13:53, Terry Guo wrote:
>> Thanks. Here is updated patch. Is this one OK?
>
> It's fine from my POV. Bin?
Hi,
We don't have write access, please help us apply it. Thanks very much.
Thanks,
bin
>
>
> Corinna
>
>>
>> BR,
>> Terry
>
>> diff --git a/newlib/libc/stdio/nano-vfprintf_i.c b/newlib/libc/stdio/nano-vfprintf_i.c
>> index b1b0d1d..b75a142 100644
>> --- a/newlib/libc/stdio/nano-vfprintf_i.c
>> +++ b/newlib/libc/stdio/nano-vfprintf_i.c
>> @@ -211,15 +211,15 @@ number:
>> case 's':
>> cp = GET_ARG (N, *ap, char_ptr_t);
>> /* Precision gives the maximum number of chars to be written from a
>> - string, and take prec == -1 into consideration. */
>> - if ((u_int)(pdata->size = strlen (cp)) > (u_int)(pdata->prec))
>> - pdata->size = pdata->prec;
>> - /* Below code is kept for reading. The check is redundant because
>> - pdata->prec will be set to pdata->size if it is -1 previously. */
>> -#if 0
>> - if (pdata->prec > pdata->size)
>> -#endif
>> - pdata->prec = pdata->size;
>> + string, and take prec == -1 into consideration.
>> + Use normal Newlib approach here to support case where cp is not
>> + nul-terminated. */
>> + char *p = memchr (cp, 0, pdata->prec);
>> +
>> + if (p != NULL)
>> + pdata->prec = p - cp;
>> +
>> + pdata->size = pdata->prec;
>> goto non_number_nosign;
>> default:
>> /* "%?" prints ?, unless ? is NUL. */
>> diff --git a/newlib/testsuite/newlib.stdio/nulprintf.c b/newlib/testsuite/newlib.stdio/nulprintf.c
>> new file mode 100644
>> index 0000000..5e4131b
>> --- /dev/null
>> +++ b/newlib/testsuite/newlib.stdio/nulprintf.c
>> @@ -0,0 +1,17 @@
>> +/*
>> + * Copyright (C) 2014 by ARM Ltd. All rights reserved.
>> + *
>> + * Permission to use, copy, modify, and distribute this software
>> + * is freely granted, provided that this notice is preserved.
>> + */
>> +
>> +#include <stdio.h>
>> +#include "check.h"
>> +
>> +const char m[8] = {'M','M','M','M','M','M','M','M'};
>> +
>> +int main()
>> +{
>> + printf ("%.*s\n", 8, m);
>> + exit (0);
>> +}
>
>
> --
> Corinna Vinschen
> Cygwin Maintainer
> Red Hat