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 >