]> git.ipfire.org Git - thirdparty/shadow.git/commit
strlcpy.h: Add STRLCPY() macro
authorAlejandro Colomar <alx@kernel.org>
Sat, 29 Jul 2023 15:21:24 +0000 (17:21 +0200)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Fri, 1 Sep 2023 07:39:23 +0000 (09:39 +0200)
commit33abc8bcd96fcc77684ba31e7eaf3d6cac09c293
treef543e05abb243059dd7344f8b3c4146a8bd6ffcb
parentb1b5c46668385e8021e2b1faf569b1bea27e769a
strlcpy.h: Add STRLCPY() macro

It wraps strlcpy(3bsd) so that it performs some steps that one might
forget, or might be prone to accidents:

-  It calculates the size of the destination buffer, and makes sure it's
   an array (otherwise, using sizeof(dst) would be very bad).

-  It calculates if there's truncation, returning an easy-to-use value.

BTW, this macro doesn't have any issues of double evaluation, because
sizeof() doesn't evaluate its argument (unless it's a VLA, but then
the static_assert(3) within SIZEOF_ARRAY() makes sure VLAs are not
allowed).

Cc: Christian Göttsche <cgzones@googlemail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/strlcpy.h [new file with mode: 0644]