Add and use strnlen_a() instead of more complex code - #1298
Add and use strnlen_a() instead of more complex code#1298alejandro-colomar wants to merge 3 commits into
Conversation
bb5de4c to
e7383e9
Compare
b3cf6f6 to
fd97871
Compare
83fbc58 to
166e380
Compare
0fd00af to
1db0235
Compare
1db0235 to
1218c16
Compare
1218c16 to
d2657a7
Compare
9fc9da5 to
ca7a891
Compare
a2e44f5 to
f199c2c
Compare
f199c2c to
979b0aa
Compare
979b0aa to
4c2b5ff
Compare
0dd1f2d to
8c6f14a
Compare
8c6f14a to
3613524
Compare
3613524 to
ee77619
Compare
ee77619 to
11fc0de
Compare
11fc0de to
d61a48c
Compare
d61a48c to
2a6a836
Compare
2a6a836 to
ff4949a
Compare
ff4949a to
b916035
Compare
|
Cc: @kees |
b916035 to
d432752
Compare
d432752 to
5e69906
Compare
5e69906 to
4fcee5e
Compare
4fcee5e to
7c40975
Compare
7d9243b to
7186a48
Compare
7186a48 to
788fa0c
Compare
788fa0c to
fa7ae64
Compare
| strlen/ - String length | ||
|
|
||
| strnlen_a() | ||
| Like strnlen(3), but take an array. |
There was a problem hiding this comment.
Can you add additional information explaining the benefits of this new API?
There was a problem hiding this comment.
The benefits of all _a() macros is that they do the countof() internally, avoiding human mistakes.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
...
fa7ae64 to
1d72861
Compare
Signed-off-by: Alejandro Colomar <alx@kernel.org>
sizeof() worked because it's an array of char, but it's brittle. Signed-off-by: Alejandro Colomar <alx@kernel.org>
ut_syslen is the length of the nonstring stored in ut_host. Let's measure it in the obvious way: strnlen(ut_host, countof(ut_host)), aka, strnlen_a(ut_host). MIN(strlen(hostname), countof(ut_host)) works because we've just copied hostname into ut_host, and the countof() field makes sure we take truncation into account. But strnlen_a(ut_host) is just simpler, and more obviously correct. Signed-off-by: Alejandro Colomar <alx@kernel.org>
1d72861 to
6e8c56f
Compare
Cc: @uecker
Revisions:
v2
SIZEOF_ARRAY.v2b
v2c
v3
v4
v4b
v4c
v4d
v4e
v4f
v5
s/STRNLEN/strnlen_a/v5b
v5c
v5d
v5e
v5f
v5g
v6
v6b
v6c
v6d
v6e
v6f
v7
v8
v9
v10
v10b