libc: printf: fix precision for string formatting. Fixes use of format precision to truncate input string.

This commit is contained in:
Jussi Kivilinna 2017-03-17 17:32:44 -06:00 committed by Gregory Nutt
parent 0501ee4dac
commit ac0d957f26

View File

@ -1171,9 +1171,7 @@ int lib_vsprintf(FAR struct lib_outstream_s *obj, FAR const IPTR char *src,
FAR char *ptmp; FAR char *ptmp;
#ifndef CONFIG_NOPRINTF_FIELDWIDTH #ifndef CONFIG_NOPRINTF_FIELDWIDTH
int width; int width;
#ifdef CONFIG_LIBC_FLOATINGPOINT
int trunc; int trunc;
#endif
uint8_t fmt; uint8_t fmt;
#endif #endif
uint8_t flags; uint8_t flags;
@ -1215,9 +1213,7 @@ int lib_vsprintf(FAR struct lib_outstream_s *obj, FAR const IPTR char *src,
#ifndef CONFIG_NOPRINTF_FIELDWIDTH #ifndef CONFIG_NOPRINTF_FIELDWIDTH
fmt = FMT_RJUST; fmt = FMT_RJUST;
width = 0; width = 0;
#ifdef CONFIG_LIBC_FLOATINGPOINT
trunc = 0; trunc = 0;
#endif
#endif #endif
/* Process each format qualifier. */ /* Process each format qualifier. */
@ -1265,10 +1261,8 @@ int lib_vsprintf(FAR struct lib_outstream_s *obj, FAR const IPTR char *src,
int value = va_arg(ap, int); int value = va_arg(ap, int);
if (IS_HASDOT(flags)) if (IS_HASDOT(flags))
{ {
#ifdef CONFIG_LIBC_FLOATINGPOINT
trunc = value; trunc = value;
SET_HASASTERISKTRUNC(flags); SET_HASASTERISKTRUNC(flags);
#endif
} }
else else
{ {
@ -1307,9 +1301,7 @@ int lib_vsprintf(FAR struct lib_outstream_s *obj, FAR const IPTR char *src,
if (IS_HASDOT(flags)) if (IS_HASDOT(flags))
{ {
#ifdef CONFIG_LIBC_FLOATINGPOINT
trunc = n; trunc = n;
#endif
} }
else else
{ {
@ -1361,6 +1353,7 @@ int lib_vsprintf(FAR struct lib_outstream_s *obj, FAR const IPTR char *src,
{ {
#ifndef CONFIG_NOPRINTF_FIELDWIDTH #ifndef CONFIG_NOPRINTF_FIELDWIDTH
int swidth; int swidth;
int left;
#endif #endif
/* Get the string to output */ /* Get the string to output */
@ -1375,13 +1368,21 @@ int lib_vsprintf(FAR struct lib_outstream_s *obj, FAR const IPTR char *src,
*/ */
#ifndef CONFIG_NOPRINTF_FIELDWIDTH #ifndef CONFIG_NOPRINTF_FIELDWIDTH
swidth = strlen(ptmp); swidth = (IS_HASDOT(flags) && trunc >= 0)
? strnlen(ptmp, trunc) : strlen(ptmp);
prejustify(obj, fmt, 0, width, swidth); prejustify(obj, fmt, 0, width, swidth);
left = swidth;
#endif #endif
/* Concatenate the string into the output */ /* Concatenate the string into the output */
while (*ptmp) while (*ptmp)
{ {
#ifndef CONFIG_NOPRINTF_FIELDWIDTH
if (left-- <= 0)
{
break;
}
#endif
obj->put(obj, *ptmp); obj->put(obj, *ptmp);
ptmp++; ptmp++;
} }