[systemd-commits] src/systemd

David Herrmann dvdhrm at kemper.freedesktop.org
Fri Dec 12 00:47:45 PST 2014


 src/systemd/sd-hwdb.h |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

New commits:
commit bff686e2a981ccd0888cdf1981977d24320f1770
Author: David Herrmann <dh.herrmann at gmail.com>
Date:   Fri Dec 12 09:43:54 2014 +0100

    hwdb: fix dangling 'else' ambuguity
    
    Imagine the following use of hwdb:
    
        if (condition_A)
            SD_HWDB_FOREACH_PROPERTY(hwdb, modalias, key, value)
                operation_A(key, value);
        else
            log_error("...");
    
    This should work just fine, but but definitely does not what you would
    expect. Due to how SD_HWDB_FOREACH_PROPERTY is defined, the dangling
    'else' is linked to the hidden 'if' statement in the macro instead of the
    outer 'if (condition_A)'. This is unexpected and really annoying to debug.
    
    Fix this by never leaving un-finished if-statements in
    SD_HWDB_FOREACH_PROPERTY(). We simply inverse the if() statement and
    explicitly add an 'else'-branch. This way, the statement is closed and all
    ambuguities are resolved.

diff --git a/src/systemd/sd-hwdb.h b/src/systemd/sd-hwdb.h
index f41555d..3c44b98 100644
--- a/src/systemd/sd-hwdb.h
+++ b/src/systemd/sd-hwdb.h
@@ -39,8 +39,9 @@ int sd_hwdb_get(sd_hwdb *hwdb, const char *modalias, const char *key, const char
 int sd_hwdb_seek(sd_hwdb *hwdb, const char *modalias);
 int sd_hwdb_enumerate(sd_hwdb *hwdb, const char **key, const char **value);
 
+/* the inverse condition avoids ambiguity of danling 'else' after the macro */
 #define SD_HWDB_FOREACH_PROPERTY(hwdb, modalias, key, value)            \
-        if (sd_hwdb_seek(hwdb, modalias) >= 0)                          \
-                while (sd_hwdb_enumerate(hwdb, &(key), &(value)) > 0)
+        if (sd_hwdb_seek(hwdb, modalias) < 0) { }                       \
+        else while (sd_hwdb_enumerate(hwdb, &(key), &(value)) > 0)
 
 #endif



More information about the systemd-commits mailing list