]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/: Use NITEMS() instead of SIZEOF_ARRAY() where number of elements is meant
authorAlejandro Colomar <alx@kernel.org>
Thu, 23 Nov 2023 00:06:52 +0000 (01:06 +0100)
committerSerge Hallyn <serge@hallyn.com>
Mon, 27 Nov 2023 03:01:05 +0000 (21:01 -0600)
For arrays of char, both NITEMS() and SIZEOF_ARRAY() return the same
value.  However, NITEMS() is more appropriate.  Think of wide-character
equivalents of the same code; with NITEMS(), they would continue to be
valid, while with SIZEOF_ARRAY(), they would be wrong.

In the implementation of ZUSTR2STP(), we want SIZEOF_ARRAY() within the
static assert, because we're just comparing the sizes of the source and
destination buffers, and we don't care if we compare sizes or numbers of
elements, and using sizes is just simpler.  But we want NITEMS() in the
zustr2stp() call, where we want to copy a specific number of characters.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/strtcpy.h
lib/utmp.c
lib/zustr2stp.h

index 60f44f9f1c391e5e88ce7e352eb4504c76d997c6..25255493a75e3146191b7efb2151c7865dbc0e50 100644 (file)
@@ -31,7 +31,7 @@
  *     at the buffer pointed to by dst.  If the destination buffer,
  *     isn't large enough to hold the copy, the resulting string is
  *     truncated.  The size of the buffer is calculated internally via
- *     SIZEOF_ARRAY().
+ *     NITEMS().
  *
  * RETURN VALUE
  *     -1      If this call truncated the resulting string.
@@ -44,7 +44,7 @@
  */
 
 
-#define STRTCPY(dst, src)  strtcpy(dst, src, SIZEOF_ARRAY(dst))
+#define STRTCPY(dst, src)  strtcpy(dst, src, NITEMS(dst))
 
 
 inline ssize_t strtcpy(char *restrict dst, const char *restrict src,
index 7f116f3b7d16ac7859266741ffac48dccb12ae9e..bf878ce6bb7fa33e27c366bb5ccc4a20e9cf077e 100644 (file)
@@ -246,7 +246,7 @@ static
 #ifdef HAVE_STRUCT_UTMP_UT_HOST
        } else if (   (NULL != ut)
                   && ('\0' != ut->ut_host[0])) {
-               hostname = XMALLOC(SIZEOF_ARRAY(ut->ut_host) + 1, char);
+               hostname = XMALLOC(NITEMS(ut->ut_host) + 1, char);
                ZUSTR2STP(hostname, ut->ut_host);
 #endif                         /* HAVE_STRUCT_UTMP_UT_HOST */
        }
index 2160a1b0acc90925100aa4eaf3fb1d8f29d61377..436f47bcbbbe05b8e302cbbdb2533d0b1e29f9b2 100644 (file)
@@ -22,7 +22,7 @@
 ({                                                                            \
        static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
                                                                               \
-       zustr2stp(dst, src, SIZEOF_ARRAY(src));                               \
+       zustr2stp(dst, src, NITEMS(src));                                     \
 })
 
 
@@ -62,9 +62,9 @@ inline char *zustr2stp(char *restrict dst, const char *restrict src, size_t sz);
  *
  * EXAMPLES
  *     char  src[13] = "Hello, world!"  // No '\0' in this buffer!
- *     char  dst[SIZEOF_ARRAY(src) + 1];
+ *     char  dst[NITEMS(src) + 1];
  *
- *     zustr2stp(dst, src, SIZEOF_ARRAY(src));
+ *     zustr2stp(dst, src, NITEMS(src));
  *     puts(dst);
  */