basic/strv: avoid potential UB with references to array[-1]
""" Given an array a[N] of N elements of type T: - Forming a pointer &a[i] (or a + i) with 0 ≤ i ≤ N is safe. - Forming a pointer &a[i] with i < 0 or i > N causes undefined behavior. - Dereferencing a pointer &a[i] with 0 ≤ i < N is safe. - Dereferencing a pointer &a[i] with i < 0 or i ≥ N causes undefined behavior. """ As pointed by by @medhefgo, here we were forming a pointer to a[-1]. a itself wasn't NULL, so a > 0, and a-1 was also >= 0, and this didn't seem to cause any problems. But it's better to be formally correct, especially if we move the code to src/fundamental/ later on and compile it differently. Compilation shows no size change (with -O0 -g) on build/systemd, so this should have no effect whatsoever.
This commit is contained in:
committed by
Lennart Poettering
parent
e7949be790
commit
9b01798b98
@@ -639,8 +639,13 @@ TEST(strv_foreach_backwards) {
|
||||
STRV_FOREACH_BACKWARDS(check, (char**) NULL)
|
||||
assert_not_reached();
|
||||
|
||||
STRV_FOREACH_BACKWARDS(check, (char**) { NULL })
|
||||
STRV_FOREACH_BACKWARDS(check, STRV_MAKE_EMPTY)
|
||||
assert_not_reached();
|
||||
|
||||
unsigned count = 0;
|
||||
STRV_FOREACH_BACKWARDS(check, STRV_MAKE("ONE"))
|
||||
count++;
|
||||
assert_se(count == 1);
|
||||
}
|
||||
|
||||
TEST(strv_foreach_pair) {
|
||||
|
||||
Reference in New Issue
Block a user