On Fri, Jan 21, 2022 at 12:41 AM Gregory Nutt <spudan...@gmail.com> wrote:
> This functionality is already implemented for asprintf(): It does this the > print using a "null stream" to get the size, allocates memory of that size, > then does the real print into the allocated memory. > > This logic could be easily extracted: > > https://github.com/apache/incubator-nuttx/blob/master/libs/libc/stdio/lib_vasprintf.c#L82 > > It is just a couple of lines of code and could be shared or duplicated in > snprintf. Adding the functionality is then trivial. > > snprintf already support this special case: https://github.com/apache/incubator-nuttx/blob/master/libs/libc/stdio/lib_snprintf.c#L42-L46 sprintf does not(but conform the spec): https://github.com/apache/incubator-nuttx/blob/master/libs/libc/stdio/lib_sprintf.c#L38 > On Thu, Jan 20, 2022 at 6:50 AM Xiang Xiao <xiaoxiang781...@gmail.com> > wrote: > > > On Thu, Jan 20, 2022 at 8:43 PM Arie de Muijnck <nu...@ademu.com> wrote: > > > > > > > > > > > On 2022-01-20 13:11, Xiang Xiao wrote: > > > > On Thu, Jan 20, 2022 at 7:33 PM Jukka Laitinen < > jukka.laiti...@iki.fi> > > > > wrote: > > > > > > > >> Hi, > > > >> > > > >> Sorry if this question comes several times, it seems that for some > > > >> reason my emails are not always coming through to the mailing > list... > > So > > > >> re-sending. > > > >> > > > >> I started getting this build error from the latest NuttX: > > > >> > > > >> misc/lib_execinfo.c:45:17: error: null argument where non-null > > required > > > >> (argument 1) [-Werror=nonnull] > > > >> 45 | int ret = sprintf(NULL, "%pS", *buffer++); > > > >> | ^~~~~~~ > > > >> In function 'backtrace_malloc', > > > >> inlined from 'backtrace_symbols' at misc/lib_execinfo.c:67:10: > > > >> misc/lib_execinfo.c:45:17: error: null destination pointer > > > >> [-Werror=format-overflow=] > > > >> 45 | int ret = sprintf(NULL, "%pS", *buffer++); > > > >> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > > >> cc1: all warnings being treated as errors > > > >> > > > >> What is the deal with sprintf to NULL ptr? > > > >> > > > >> > > > > The code wants to get the length of the formatted string. But, the > > right > > > > API is snprintf, not sprintf. Could you try this: > > > > https://github.com/apache/incubator-nuttx/pull/5290 > > > > > > > > > > > > > > Uhm, no, the first parameter is the target buffer location. That cannot > > > be a NULL pointer. > > > > > > > No, snprintf support this special case by spec explicitly: > > https://en.cppreference.com/w/c/io/fprintf > > > > Calling snprintf with zero bufsz and null pointer for buffer is useful to > > determine the necessary buffer size to contain the output: > > > > const char *fmt = "sqrt(2) = %f";int sz = snprintf(NULL > > <http://en.cppreference.com/w/c/types/NULL>, 0, fmt, sqrt > > <http://en.cppreference.com/w/c/numeric/math/sqrt>(2));char buf[sz + > > 1]; // note +1 for terminating null byte > > snprintf(buf, sizeof buf, fmt, sqrt > > <http://en.cppreference.com/w/c/numeric/math/sqrt>(2)); > > > > > > > > > > > I fully agree on the snprintf recommendation to prevent overflows. > > > > > > Arie > > > > > >