|
|
572a44 |
From 44d380fa1042c56eee959c8cf8477aa62b8d8a0e Mon Sep 17 00:00:00 2001
|
|
|
572a44 |
From: Stefan Beller <stefanbeller@googlemail.com>
|
|
|
572a44 |
Date: Mon, 30 Dec 2013 17:43:52 +0100
|
|
|
572a44 |
Subject: [PATCH] sleep-config: Dereference pointer before check for NULL
|
|
|
572a44 |
|
|
|
572a44 |
This fixes a bug pointed out by http://css.csail.mit.edu/stack/
|
|
|
572a44 |
(Optimization-unstable code)
|
|
|
572a44 |
It is a similar fix as f146f5e159 (2013-12-30, core:
|
|
|
572a44 |
Forgot to dereference pointer when checking for NULL)
|
|
|
572a44 |
|
|
|
572a44 |
To explain this bug consider the following similar, but simpler code:
|
|
|
572a44 |
if (!p)
|
|
|
572a44 |
free(*p)
|
|
|
572a44 |
|
|
|
572a44 |
Assume the if condition evaluates to true, then we will access *p,
|
|
|
572a44 |
which means the compiler can assume p is a valid pointer, so it could
|
|
|
572a44 |
dereference p and use the value *p.
|
|
|
572a44 |
Assuming p as a valid pointer, !p will be false.
|
|
|
572a44 |
But initally we assumed the condition evaluates to true.
|
|
|
572a44 |
|
|
|
572a44 |
By this reasoning the optimizing compiler can deduce, we have dead code.
|
|
|
572a44 |
("The if will never be taken, as *p must be valid, because otherwise
|
|
|
572a44 |
accessing *p inside the if would segfault")
|
|
|
572a44 |
|
|
|
572a44 |
This led to an error message of the static code checker, so I checked the
|
|
|
572a44 |
code in question.
|
|
|
572a44 |
|
|
|
572a44 |
As we access *modes and *states before the check in the changed line of
|
|
|
572a44 |
this patch, I assume the line to be wrong and we actually wanted to check
|
|
|
572a44 |
for *modes and *states being both non null.
|
|
|
572a44 |
---
|
|
|
572a44 |
src/shared/sleep-config.c | 2 +-
|
|
|
572a44 |
1 file changed, 1 insertion(+), 1 deletion(-)
|
|
|
572a44 |
|
|
|
572a44 |
diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c
|
|
|
572a44 |
index d76e3ad..b2a0787 100644
|
|
|
572a44 |
--- a/src/shared/sleep-config.c
|
|
|
572a44 |
+++ b/src/shared/sleep-config.c
|
|
|
572a44 |
@@ -94,7 +94,7 @@ int parse_sleep_config(const char *verb, char ***modes, char ***states) {
|
|
|
572a44 |
} else
|
|
|
572a44 |
assert_not_reached("what verb");
|
|
|
572a44 |
|
|
|
572a44 |
- if (!modes || !states) {
|
|
|
572a44 |
+ if (!*modes || !*states) {
|
|
|
572a44 |
strv_free(*modes);
|
|
|
572a44 |
strv_free(*states);
|
|
|
572a44 |
return log_oom();
|