Clean-up: lttng-ctl: strnlen out of bounds access
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 15 Mar 2022 21:19:27 +0000 (17:19 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 16 Mar 2022 20:06:19 +0000 (16:06 -0400)
commitb25a59916106e5055be516f61f183a48f459b0b3
treecb6e5f4908171353cc1ca9d8c5a16441088d5863
parent62e6775c1485031cd086d8bc099f19265af44e56
Clean-up: lttng-ctl: strnlen out of bounds access

gcc 11.2 produces the following warning. The lttng_strncpy helper
assumes that 'src' is a null terminated string. As such, the use of a
string literal (of size 37) in this specific example is correct as
strnlen will not read beyond the null terminator.

Replacing strnlen by strlen eliminates this warning. strnlen was used to
short-circuit the source length check when it was larger than the
destination. This optimization is unlikely to matter. Pascal-style
strings should be used when string length computations are expected to
be prohibitively expensive.

In file included from ../../../src/common/macros.h:15,
                 from ../../../include/lttng/health-internal.h:18,
                 from lttng-ctl-health.cpp:19:
In function 'size_t lttng_strnlen(const char*, size_t)',
    inlined from 'int lttng_strncpy(char*, const char*, size_t)' at ../../../src/common/macros.h:123:19,
    inlined from 'int set_health_socket_path(lttng_health*, int)' at lttng-ctl-health.cpp:198:22,
    inlined from 'int lttng_health_query(lttng_health*)' at lttng-ctl-health.cpp:319:30:
../../../src/common/compat/string.h:19:23: warning: 'size_t strnlen(const char*, size_t)' specified bound 4096 may exceed source size 37 [-Wstringop-overread]
   19 |         return strnlen(str, max);
      |                ~~~~~~~^~~~~~~~~~

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I290109433fcae7073321f1b48ecfbb2ec6e4ad26
src/common/macros.h
This page took 0.025748 seconds and 4 git commands to generate.