Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ libshadow_la_SOURCES = \
lockpw.c \
loginprompt.c \
mail.c \
memory/strnlen/strnlen.c \
memory/strnlen/strnlen.h \
motd.c \
myname.c \
nss.c \
Expand Down
7 changes: 7 additions & 0 deletions lib/memory/strnlen/strnlen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#include "memory/strnlen/strnlen.h"
20 changes: 20 additions & 0 deletions lib/memory/strnlen/strnlen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_MEMORY_STRNLEN_STRNLEN_H_
#define SHADOW_INCLUDE_LIB_MEMORY_STRNLEN_STRNLEN_H_


#include "config.h"

#include <memory.h>

#include "sizeof.h"


// strnlen_a - nonstring length array
Comment thread
alejandro-colomar marked this conversation as resolved.
#define strnlen_a(strn) strnlen(strn, countof(strn))


#endif // include guard
5 changes: 5 additions & 0 deletions lib/string/README
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ strsep/ - String separation
Variant of strsep2ls() that allocates the array of strings.
(But the strings themselves are not duplicated.)

strnlen/ - String length

strnlen_a()
Like strnlen(3), but take an array.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add additional information explaining the benefits of this new API?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benefits of all _a() macros is that they do the countof() internally, avoiding human mistakes.

@alejandro-colomar alejandro-colomar Aug 26, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of this one, the mistake is less likely than with other string/nonstring APIs, because the others often have 2 parameters, and one can choose the argument wrongly, while in this case it's more obvious. But it still reduces mistakes a little bit. Since we can take it internally reducing code, it has less mistakes.

@alejandro-colomar alejandro-colomar Aug 26, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, one thing it does is it makes sure that the input is an array. Nonstrings from utmp are always arrays, so this reduces the chances of accidentally passing a string.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have some such information at the top of the README:

General guidelines:
===================

-  If there's an upper-case macro that wraps a function, use the macro
   if possible.  These use macro magic to add safety.

-  If there's a *_a() macro that wraps an API, use the macro if
   possible.  These use countof() to add bounds safety.

...


strftime.h
strftime_a()
Like strftime(3), but takes an array.
4 changes: 2 additions & 2 deletions lib/utmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "alloc/malloc.h"
#include "attr.h"
#include "io/syslog.h"
#include "memory/strnlen/strnlen.h"
#include "sizeof.h"
#include "string/strchr/strnul.h"
#include "string/strcmp/streq.h"
Expand Down Expand Up @@ -301,8 +302,7 @@ prepare_utmp(const char *name, const char *line, const char *host,
strncpy_a(utent->ut_host, hostname);
#endif
#if defined(HAVE_STRUCT_UTMPX_UT_SYSLEN)
utent->ut_syslen = MIN(strlen(hostname),
sizeof(utent->ut_host));
utent->ut_syslen = strnlen_a(utent->ut_host);
#endif
#if defined(HAVE_STRUCT_UTMPX_UT_ADDR) || defined(HAVE_STRUCT_UTMPX_UT_ADDR_V6)
if (getaddrinfo (hostname, NULL, NULL, &info) == 0) {
Expand Down
Loading