casaroli commented on code in PR #21340:
URL: https://github.com/apache/nuttx/pull/21340#discussion_r4587373317
##########
libs/libc/stream/lib_libvsprintf.c:
##########
@@ -1864,19 +2155,10 @@ static int vsprintf_internal(FAR struct lib_outstream_s
*stream,
}
str_lpad:
- if ((flags & FL_LPAD) == 0)
- {
- if (width > 0 && size < width)
- {
- int pad = width - size;
-
- stream_repeat(' ', pad, stream);
- width -= pad;
- }
- }
-
- stream_puts(pnt, size, stream);
- width = width >= size ? width - size : 0;
+ total_len += vsprintf_put_field(stream, pnt, size, width,
+ flags);
+ width = FL_LPAD != 0 && width > 0 && size < (size_t)width ?
Review Comment:
The same as line 586.
Fix: `(flags & FL_LPAD) != 0`.
##########
libs/libc/stream/lib_libvsprintf.c:
##########
@@ -416,6 +416,240 @@ static int vsprintf_putnum(FAR struct lib_outstream_s
*stream,
return len;
}
+static int vsprintf_scan_literal(FAR struct lib_outstream_s *stream,
+ FAR const IPTR char **fmtp,
+ FAR unsigned char *conv)
+{
+ FAR const IPTR char *fmt = *fmtp;
+ unsigned char c;
+ int total_len = 0;
+
+#ifndef CONFIG_ARCH_ROMGETC
+ FAR const char *start = fmt;
+
+ for (; ; )
+ {
+ c = fmt_char(fmt);
+ if (c == '\0')
+ {
+# ifdef CONFIG_LIBC_NUMBERED_ARGS
+ if (stream != NULL)
+# endif
+ {
+ size_t litlen = (fmt - start) - 1;
+
+ if (litlen > 0)
+ {
+ total_len += litlen;
+ lib_stream_puts(stream, start, litlen);
+ }
+ }
+
+ break;
+ }
+
+ if (c == '%')
+ {
+ size_t litlen = (fmt - start) - 1;
+
+ c = fmt_char(fmt);
+ if (c != '%')
+ {
+# ifdef CONFIG_LIBC_NUMBERED_ARGS
+ if (stream != NULL)
+# endif
+ {
+ if (litlen > 0)
+ {
+ total_len += litlen;
+ lib_stream_puts(stream, start, litlen);
+ }
+
+ if (c == '\0')
+ {
+ total_len++;
+ lib_stream_putc(stream, '%');
+ }
+ }
+
+ break;
+ }
+
+# ifdef CONFIG_LIBC_NUMBERED_ARGS
+ if (stream != NULL)
+# endif
+ {
+ total_len += litlen + 1;
+ lib_stream_puts(stream, start, litlen + 1);
+ }
+
+ start = fmt;
+ }
+ }
+#else
+ for (; ; )
+ {
+ c = fmt_char(fmt);
+ if (c == '\0')
+ {
+ break;
+ }
+
+ if (c == '%')
+ {
+ c = fmt_char(fmt);
+ if (c != '%')
+ {
+ if (c == '\0')
+ {
+# ifdef CONFIG_LIBC_NUMBERED_ARGS
+ if (stream != NULL)
+# endif
+ {
+ total_len++;
+ lib_stream_putc(stream, '%');
+ }
+ }
+
+ break;
+ }
+ }
+
+# ifdef CONFIG_LIBC_NUMBERED_ARGS
+ if (stream != NULL)
+# endif
+ {
+ total_len++;
+ lib_stream_putc(stream, c);
+ }
+ }
+#endif
+
+ *fmtp = fmt;
+ *conv = c;
+
+ return total_len;
+}
+
+static int vsprintf_put_field(FAR struct lib_outstream_s *stream,
+ FAR const char *pnt, size_t size,
+ int width, uint16_t flags)
+{
+ int total_len = 0;
+
+ if ((flags & FL_LPAD) == 0)
+ {
+ if (width > 0 && size < (size_t)width)
+ {
+ int pad = width - size;
+
+ total_len += vsprintf_repeat(stream, ' ', pad);
+ }
+ }
+
+ total_len += size;
+ lib_stream_puts(stream, pnt, size);
+
+ return total_len;
+}
+
+static int vsprintf_put_char(FAR struct lib_outstream_s *stream,
+ uint16_t flags, int ch, FAR int *width,
+ FAR char *buf)
+{
+ FAR const char *pnt;
+ size_t size;
+
+#ifdef CONFIG_LIBC_LOCALE
+ if ((flags & FL_LONG) != 0)
+ {
+ mbstate_t ps;
+
+ /* %lc: a wide character, written as its multibyte sequence. */
+
+ memset(&ps, 0, sizeof(ps));
+ size = wcrtomb(buf, (wchar_t)ch, &ps);
+ if (size == (size_t)-1)
+ {
+ return -1;
+ }
+ }
+ else
+#endif
+ {
+ buf[0] = ch;
+ size = 1;
+ }
+
+ pnt = buf;
+ ch = vsprintf_put_field(stream, pnt, size, *width, flags);
+ *width = FL_LPAD != 0 && *width > 0 && size < (size_t)*width ?
Review Comment:
`FL_LPAD != 0` tests the macro, not the flag, so it is true whenever
LIBC_PRINTF_LEFT_JUSTIFY is on (the default). After a right-justified field,
`width` keeps `width - size`, and `tail:` pads it again on the right:
`printf("[%5s]", "ab")` prints `[ ab ]` and returns 10; master prints `[
ab]` and returns 7. The same for `%5c`, `%5lc`, `%1$5s` and the `?` of `%5a`.
Fix: `(flags & FL_LPAD) != 0` here and at line 2160.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]