There are some risks associated with the way arguments are used in macros,
and using a function also allows check on the type of arguments and leaves
more room to the compiler for making the best optimisation choice; it also
allows writing easier to read code (and thus, to maintain).

As a side effect, this should also help Coverity in avoiding false positive
bug reports (like #109605 and #109607).

Signed-off-by: Christophe CURIS <[email protected]>
---
 src/session.c | 20 +++++++++++++++++---
 src/window.c  | 19 ++++++++++++++++---
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/src/session.c b/src/session.c
index d55732f..2af8fd0 100644
--- a/src/session.c
+++ b/src/session.c
@@ -415,7 +415,19 @@ static WSavedState *getWindowState(WScreen * scr, 
WMPropList * win_state)
        return state;
 }
 
-#define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
+static inline int is_same(const char *x, const char *y)
+{
+       if ((x == NULL) && (y == NULL))
+               return 1;
+
+       if ((x == NULL) || (y == NULL))
+               return 0;
+
+       if (strcmp(x, y) == 0)
+               return 1;
+       else
+               return 0;
+}
 
 void wSessionRestoreState(WScreen *scr)
 {
@@ -503,8 +515,10 @@ void wSessionRestoreState(WScreen *scr)
                if (dock != NULL) {
                        for (j = 0; j < dock->max_icons; j++) {
                                btn = dock->icon_array[j];
-                               if (btn && SAME(instance, btn->wm_instance) &&
-                                   SAME(class, btn->wm_class) && SAME(command, 
btn->command) && !btn->launching) {
+                               if (btn && is_same(instance, btn->wm_instance) 
&&
+                                   is_same(class, btn->wm_class) &&
+                                   is_same(command, btn->command) &&
+                                   !btn->launching) {
                                        found = 1;
                                        break;
                                }
diff --git a/src/window.c b/src/window.c
index 121cd16..7cbad9c 100644
--- a/src/window.c
+++ b/src/window.c
@@ -2621,7 +2621,19 @@ WMagicNumber wWindowAddSavedState(const char *instance, 
const char *class,
        return wstate;
 }
 
-#define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
+static inline int is_same(const char *x, const char *y)
+{
+       if ((x == NULL) && (y == NULL))
+               return 1;
+
+       if ((x == NULL) || (y == NULL))
+               return 0;
+
+       if (strcmp(x, y) == 0)
+               return 1;
+       else
+               return 0;
+}
 
 WMagicNumber wWindowGetSavedState(Window win)
 {
@@ -2637,8 +2649,9 @@ WMagicNumber wWindowGetSavedState(Window win)
 
        if (PropGetWMClass(win, &class, &instance)) {
                while (wstate) {
-                       if (SAME(instance, wstate->instance) &&
-                           SAME(class, wstate->class) && SAME(command, 
wstate->command)) {
+                       if (is_same(instance, wstate->instance) &&
+                           is_same(class, wstate->class) &&
+                           is_same(command, wstate->command)) {
                                break;
                        }
                        wstate = wstate->next;
-- 
2.1.4


-- 
To unsubscribe, send mail to [email protected].

Reply via email to