-
Notifications
You must be signed in to change notification settings - Fork 284
Add and use strnlen_a() instead of more complex code #1298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" |
| 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 | ||
| #define strnlen_a(strn) strnlen(strn, countof(strn)) | ||
|
|
||
|
|
||
| #endif // include guard | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add additional information explaining the benefits of this new API?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have some such information at the top of the README: |
||
|
|
||
| strftime.h | ||
| strftime_a() | ||
| Like strftime(3), but takes an array. | ||
Uh oh!
There was an error while loading. Please reload this page.