kwo pushed a commit to branch master.

http://git.enlightenment.org/e16/e16.git/commit/?id=c839d0e14afcb074eef95a53ee24bd6a4856e173

commit c839d0e14afcb074eef95a53ee24bd6a4856e173
Author: Kim Woelders <[email protected]>
Date:   Sun Jan 26 09:02:41 2020 +0100

    src: Fix clang-analyzer complaints
    
    arrange.c: Highly unlikely, maybe impossible situations
    
    backgrounds.c: False positive (I think)
    
    dialog.c: Fix memleak/segv on realloc failure
    
    focus.c: False positive
            ewin should never be NULL with FOCUS_EWIN_NEW
    
    main.c: False positive
            eoptarg will always be set if argument requires option
    
    menus.c: Fix memleak on malloc failure
    
    session.c: Fix dead assignment
    
    stacking.c: False positive
            Changed code is nicer though
    
    pager.c: False positive
            ewin would never be NULL if hiwin was NULL
            But rearrange for clarity anyway
    
    ecompmgr.c: Avoid bogus issues
---
 src/arrange.c     | 10 ++++++++--
 src/backgrounds.c |  6 +++++-
 src/dialog.c      |  9 ++++++---
 src/ecompmgr.c    |  7 +++++++
 src/focus.c       |  5 ++++-
 src/main.c        | 10 ++++++----
 src/menus.c       | 37 ++++++++++++++++++++++++-------------
 src/pager.c       | 21 +++++++++------------
 src/session.c     |  4 ++--
 src/stacking.c    | 33 +++++++++++++++++----------------
 10 files changed, 88 insertions(+), 54 deletions(-)

diff --git a/src/arrange.c b/src/arrange.c
index 52bcd496..0b1a70d0 100644
--- a/src/arrange.c
+++ b/src/arrange.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2004-2019 Kim Woelders
+ * Copyright (C) 2004-2020 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -396,7 +396,8 @@ ArrangeRects(const RectBox * fixed, int fixed_count, 
RectBox * floating,
       goto done;
 
    /* copy "fixed" rects into the sorted list */
-   memcpy(sorted, fixed, fixed_count * sizeof(RectBox));
+   if (fixed)
+      memcpy(sorted, fixed, fixed_count * sizeof(RectBox));
    num_sorted = fixed_count;
 
    /* go through each floating rect in order and "fit" it in */
@@ -405,6 +406,9 @@ ArrangeRects(const RectBox * fixed, int fixed_count, 
RectBox * floating,
        ArrangeMakeXYArrays(tx1, tx2, ty1, ty2, floating[i].w, floating[i].h,
                            sorted, num_sorted, xarray, &xsize, yarray, &ysize);
        num_spaces = xsize * ysize;
+       if (num_spaces <= 0)
+          continue;
+
        if (alloc_spaces < num_spaces)
          {
             unsigned char      *ptr_f;
@@ -427,6 +431,8 @@ ArrangeRects(const RectBox * fixed, int fixed_count, 
RectBox * floating,
        /* create list of all "spaces" */
        ArrangeFindSpaces(xarray, xsize, yarray, ysize, filled,
                          spaces, alloc_spaces, &num_spaces, floating + i);
+       if (num_spaces <= 0)
+          continue;
 
        /* find the first space that fits */
        k = 0;
diff --git a/src/backgrounds.c b/src/backgrounds.c
index 2cbda3a9..f2406a19 100644
--- a/src/backgrounds.c
+++ b/src/backgrounds.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2004-2019 Kim Woelders
+ * Copyright (C) 2004-2020 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -1306,7 +1306,9 @@ BackgroundsCheckDups(void)
           Eprintf("Remove duplicate background %s (==%s)\n", bgx->name,
                   bg->name);
 #endif
+#ifndef __clang_analyzer__
           BackgroundDestroy(bgx);
+#endif
        }
    }
 }
@@ -1789,6 +1791,8 @@ BGSettingsGoTo(Dialog * d, Background * bg)
       return;
 
    num = LIST_GET_COUNT(&bg_list);
+   if (num <= 0)
+      return;
    i = LIST_GET_INDEX(Background, &bg_list, bg);
    if (i < 0)
       return;
diff --git a/src/dialog.c b/src/dialog.c
index cfbe307e..f3de26c9 100644
--- a/src/dialog.c
+++ b/src/dialog.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2004-2018 Kim Woelders
+ * Copyright (C) 2004-2020 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -1125,13 +1125,16 @@ DialogRealizeItem(Dialog * d, DItem * di)
                c += dii->col_span;
                if (c >= cols)
                  {
+                    int                *pi;
+
                     c = 0;
                     r++;
                     rows++;
-                    row_size = EREALLOC(int, row_size, rows);
 
-                    if (!row_size)
+                    pi = EREALLOC(int, row_size, rows);
+                    if (!pi)
                        goto bail_out;
+                    row_size = pi;
 
                     row_size[rows - 1] = 0;
                  }
diff --git a/src/ecompmgr.c b/src/ecompmgr.c
index 645bc7c8..3bc4171e 100644
--- a/src/ecompmgr.c
+++ b/src/ecompmgr.c
@@ -448,7 +448,11 @@ make_gaussian_map(int r)
    int                 x, y;
    float               t, g;
 
+#ifdef __clang_analyzer__
+   c = malloc(sizeof(conv) + size * size * sizeof(float));
+#else
    c = (conv *) EMALLOC(char, sizeof(conv) + size * size * sizeof(float));
+#endif
 
    c->size = size;
    c->data = (float *)(c + 1);
@@ -631,6 +635,9 @@ make_shadow(float opacity, int width, int height)
          }
      }
 
+#ifdef __clang_analyzer__
+   free(data);
+#endif
    return ximage;
 }
 
diff --git a/src/focus.c b/src/focus.c
index 3578b4f4..81116056 100644
--- a/src/focus.c
+++ b/src/focus.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2004-2019 Kim Woelders
+ * Copyright (C) 2004-2020 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -353,6 +353,9 @@ doFocusToEwin(EWin * ewin, int why)
        break;
 
      case FOCUS_EWIN_NEW:
+       if (!ewin)
+          return;
+
        if (Mode.place.doing_manual)
           goto check_focus_new;
 
diff --git a/src/main.c b/src/main.c
index 117e5940..f8b22c41 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2004-2018 Kim Woelders
+ * Copyright (C) 2004-2020 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -90,6 +90,8 @@ static const EOpt   Eopts[] = {
    {'X', 1, NULL, NULL, NULL},
 };
 
+#define Strtoi(s, base) ((s) ? strtol(s, NULL, base) : 0)
+
 int
 main(int argc, char **argv)
 {
@@ -189,7 +191,7 @@ main(int argc, char **argv)
             break;
          case 's':
             Mode.wm.single = 1;
-            Dpy.screen = strtoul(eoptarg, NULL, 10);
+            Dpy.screen = Strtoi(eoptarg, 10);
             break;
          case 'S':
             SetSMID(eoptarg);
@@ -212,13 +214,13 @@ main(int argc, char **argv)
             break;
 #ifdef USE_EXT_INIT_WIN
          case 'X':
-            ExtInitWinSet(strtoul(eoptarg, NULL, 0));
+            ExtInitWinSet(Strtoi(eoptarg, 0));
             Mode.wm.restart = 1;
             break;
 #endif
          case 'm':
             Mode.wm.master = 0;
-            Mode.wm.master_screen = strtoul(eoptarg, NULL, 10);
+            Mode.wm.master_screen = Strtoi(eoptarg, 10);
             break;
          }
      }
diff --git a/src/menus.c b/src/menus.c
index d0ed9cf3..ba693234 100644
--- a/src/menus.c
+++ b/src/menus.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2004-2018 Kim Woelders
+ * Copyright (C) 2004-2020 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -427,6 +427,23 @@ MenuItemCreate(const char *text, const char *icon,
    return mi;
 }
 
+static void
+MenuItemDestroy(MenuItem * mi, int destroying)
+{
+   int                 i;
+
+   Efree(mi->text);
+   Efree(mi->icon);
+   Efree(mi->params);
+   for (i = 0; i < 3; i++)
+      PmapMaskFree(&(mi->pmm[i]));
+   if (!destroying && mi->win)
+      EDestroyWindow(mi->win);
+   else
+      EventCallbackUnregister(mi->win, MenuItemHandleEvents, mi);
+   Efree(mi);
+}
+
 static int
 _MenuStyleMatchName(const void *data, const void *match)
 {
@@ -609,7 +626,7 @@ MenuDestroy(Menu * m)
 void
 MenuEmpty(Menu * m, int destroying)
 {
-   int                 i, j;
+   int                 i;
    MenuItem           *mi;
 
    for (i = 0; i < m->num; i++)
@@ -621,16 +638,7 @@ MenuEmpty(Menu * m, int destroying)
             mi->child->ref_count--;
             MenuDestroy(mi->child);
          }
-       Efree(mi->text);
-       Efree(mi->icon);
-       Efree(mi->params);
-       for (j = 0; j < 3; j++)
-          PmapMaskFree(&(mi->pmm[j]));
-       if (!destroying && mi->win)
-          EDestroyWindow(mi->win);
-       else
-          EventCallbackUnregister(mi->win, MenuItemHandleEvents, mi);
-       Efree(mi);
+       MenuItemDestroy(mi, destroying);
      }
    EFREE_NULL(m->items);
    m->num = 0;
@@ -672,7 +680,10 @@ MenuAddItem(Menu * m, MenuItem * item)
 
    items = EREALLOC(MenuItem *, m->items, m->num + 1);
    if (!items)
-      return;
+     {
+       MenuItemDestroy(item, 1);
+       return;
+     }
 
    items[m->num] = item;
    m->items = items;
diff --git a/src/pager.c b/src/pager.c
index b0c9ff5b..94747663 100644
--- a/src/pager.c
+++ b/src/pager.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2004-2018 Kim Woelders
+ * Copyright (C) 2004-2020 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -1116,14 +1116,6 @@ PagerHiwinInit(Pager * p, EWin * ewin)
    Hiwin              *phi = hiwin;
    int                 wx, wy, ww, wh, px, py;
 
-   if (!phi)
-     {
-       phi = HiwinCreate();
-       if (!phi)
-          return;
-       hiwin = phi;
-     }
-
    wx = (EwinGetVX(ewin) * p->dw) / WinGetW(VROOT);
    wy = (EwinGetVY(ewin) * p->dh) / WinGetH(VROOT);
    ww = (EoGetW(ewin) * p->dw) / WinGetW(VROOT);
@@ -1151,14 +1143,19 @@ PagerHiwinShow(Pager * p, EWin * ewin, int zoom, int 
confine)
    if (MenusActive())          /* Don't show HiWin when menu is up */
       return;
 
-   if (!phi || ewin)
+   Dprintf("%s\n", __func__);
+
+   if (!phi)
      {
-       PagerHiwinInit(p, ewin);
-       phi = hiwin;
+       phi = HiwinCreate();
        if (!phi)
           return;
+       hiwin = phi;
      }
 
+   if (ewin)
+      PagerHiwinInit(p, ewin);
+
    HiwinShow(phi, ewin, zoom, confine);
 }
 
diff --git a/src/session.c b/src/session.c
index 344603b8..83d5dd36 100644
--- a/src/session.c
+++ b/src/session.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2004-2019 Kim Woelders
+ * Copyright (C) 2004-2020 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -554,7 +554,7 @@ doSMExit(int mode, const char *params)
           l += Esnprintf(s + l, sizeof(s) - l, " -X %#x", new_init_win_ext);
 #endif
        if (ss)
-          l += Esnprintf(s + l, sizeof(s) - l, " -t %s", ss);
+          Esnprintf(s + l, sizeof(s) - l, " -t %s", ss);
 
        if (EDebug(EDBUG_TYPE_SESSION))
           Eprintf("%s: exec %s\n", __func__, s);
diff --git a/src/stacking.c b/src/stacking.c
index b0a1fe1a..1bebc2ea 100644
--- a/src/stacking.c
+++ b/src/stacking.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2004-2018 Kim Woelders
+ * Copyright (C) 2004-2020 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -256,6 +256,7 @@ EobjListFind(const EobjList * ewl, EX_Window win)
    return NULL;
 }
 
+#if 0
 static int
 EobjListTypeCount(const EobjList * ewl, int type)
 {
@@ -267,6 +268,7 @@ EobjListTypeCount(const EobjList * ewl, int type)
 
    return n;
 }
+#endif
 
 /*
  * The global object/client lists
@@ -348,16 +350,10 @@ EwinListStackGet(int *num)
    static EWin       **lst = NULL;
    static int          nalloc = 0;
    const EobjList     *ewl;
-   int                 i, j, newins;
+   int                 i, j;
    EObj               *eo;
 
    ewl = &EwinListStack;
-   newins = EobjListTypeCount(ewl, EOBJ_TYPE_EWIN);
-   if (nalloc < newins)
-     {
-       nalloc = (newins + 16) & ~0xf;  /* 16 at the time */
-       lst = EREALLOC(EWin *, lst, nalloc);
-     }
 
    for (i = j = 0; i < ewl->nwins; i++)
      {
@@ -365,6 +361,12 @@ EwinListStackGet(int *num)
        if (eo->type != EOBJ_TYPE_EWIN)
           continue;
 
+       if (nalloc <= j)
+         {
+            nalloc += 16;      /* 16 at the time */
+            lst = EREALLOC(EWin *, lst, nalloc);
+         }
+
        lst[j++] = (EWin *) eo;
      }
 
@@ -384,17 +386,10 @@ EwinListGetForDesk(int *num, Desk * dsk)
    static EWin       **lst = NULL;
    static int          nalloc = 0;
    const EobjList     *ewl;
-   int                 i, j, newins;
+   int                 i, j;
    EObj               *eo;
 
    ewl = &EwinListStack;
-   newins = EobjListTypeCount(ewl, EOBJ_TYPE_EWIN);
-   /* Too many - who cares. */
-   if (nalloc < newins)
-     {
-       nalloc = (newins + 16) & ~0xf;  /* 16 at the time */
-       lst = EREALLOC(EWin *, lst, nalloc);
-     }
 
    for (i = j = 0; i < ewl->nwins; i++)
      {
@@ -402,6 +397,12 @@ EwinListGetForDesk(int *num, Desk * dsk)
        if (eo->type != EOBJ_TYPE_EWIN || eo->desk != dsk)
           continue;
 
+       if (nalloc <= j)
+         {
+            nalloc += 16;      /* 16 at the time */
+            lst = EREALLOC(EWin *, lst, nalloc);
+         }
+
        lst[j++] = (EWin *) eo;
      }
 

-- 


Reply via email to