Skip to content

Commit

Permalink
test: fix use of ERRNO_IS_PRIVILEGE()
Browse files Browse the repository at this point in the history
Given that ERRNO_IS_PRIVILEGE() also matches positive values,
make sure this macro is not called with arguments that do not have
errno semantics.

In this case the arguments passed to ERRNO_IS_PRIVILEGE() are the values
returned by read_one_line_file() which can legitimately return positive
values without errno semantics, so fix this by moving ERRNO_IS_PRIVILEGE()
invocations to the branches where the return values are known to be negative.
  • Loading branch information
ldv-alt committed Jul 16, 2023
1 parent 0bdea17 commit fce846e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/test/test-capability.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static void test_last_cap_file(void) {
int r;

r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r)) /* kernel pre 3.2 or no access */
if (r == -ENOENT || (r < 0 && ERRNO_IS_PRIVILEGE(r))) /* kernel pre 3.2 or no access */
return;
assert_se(r >= 0);

Expand Down Expand Up @@ -235,7 +235,7 @@ static void test_ensure_cap_64_bit(void) {
int r;

r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r)) /* kernel pre 3.2 or no access */
if (r == -ENOENT || (r < 0 && ERRNO_IS_PRIVILEGE(r))) /* kernel pre 3.2 or no access */
return;
assert_se(r >= 0);

Expand Down
2 changes: 1 addition & 1 deletion src/test/test-fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ TEST(write_string_file_verify) {
int r;

r = read_one_line_file("/proc/version", &buf);
if (ERRNO_IS_PRIVILEGE(r))
if (r < 0 && ERRNO_IS_PRIVILEGE(r))
return;
assert_se(r >= 0);
assert_se(buf2 = strjoin(buf, "\n"));
Expand Down

0 comments on commit fce846e

Please sign in to comment.