chiark / gitweb /
sleep-config: Dereference pointer before check for NULL
authorStefan Beller <stefanbeller@googlemail.com>
Mon, 30 Dec 2013 16:43:52 +0000 (17:43 +0100)
committerKay Sievers <kay@vrfy.org>
Mon, 30 Dec 2013 18:10:22 +0000 (19:10 +0100)
This fixes a bug pointed out by http://css.csail.mit.edu/stack/
(Optimization-unstable code)
It is a similar fix as f146f5e159 (2013-12-30, core:
Forgot to dereference pointer when checking for NULL)

To explain this bug consider the following similar, but simpler code:
if (!p)
free(*p)

Assume the if condition evaluates to true, then we will access *p,
which means the compiler can assume p is a valid pointer, so it could
dereference p and use the value *p.
Assuming p as a valid pointer, !p will be false.
But initally we assumed the condition evaluates to true.

By this reasoning the optimizing compiler can deduce, we have dead code.
("The if will never be taken, as *p must be valid, because otherwise
accessing *p inside the if would segfault")

This led to an error message of the static code checker, so I checked the
code in question.

As we access *modes and *states before the check in the changed line of
this patch, I assume the line to be wrong and we actually wanted to check
for *modes and *states being both non null.

src/shared/sleep-config.c

index d76e3ad0365411b46dd4c4ac7e7c985675833f2e..b2a07878442f71fc6a451dafa1e40a498e06714e 100644 (file)
@@ -94,7 +94,7 @@ int parse_sleep_config(const char *verb, char ***modes, char ***states) {
         } else
                 assert_not_reached("what verb");
 
-        if (!modes || !states) {
+        if (!*modes || !*states) {
                 strv_free(*modes);
                 strv_free(*states);
                 return log_oom();