From 44bc9cc264ba13a6033c1145ba2b15f48395356f Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Sat, 18 Apr 2020 14:28:41 -0400 Subject: [PATCH] Fix various abs() issues. The abs() function should take an int as argument, but there were several instances in the code where it was taking an unsigned int or a double. In these case, we took one of the following approaches: * If the argument was a double, use fabs() instead. * If the argument was unsigned and was certainly going to be positive (i.e,. no subtraction), then drop abs() altogether. * If the argument was unsigned as result of adding or subtracting signed and unsigned ints, then we cast all the unsigned ints to signed ints. --- WPrefs.app/FontSimple.c | 3 ++- src/menu.c | 4 ++-- src/moveres.c | 4 ++-- src/window.c | 4 ++-- src/xinerama.c | 8 ++++---- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/WPrefs.app/FontSimple.c b/WPrefs.app/FontSimple.c index eac0411c..2147b985 100644 --- a/WPrefs.app/FontSimple.c +++ b/WPrefs.app/FontSimple.c @@ -22,6 +22,7 @@ #include "WPrefs.h" #include #include +#include /* workaround for older fontconfig, that doesn't define these constants */ #ifndef FC_WEIGHT_NORMAL @@ -521,7 +522,7 @@ static void selectedOption(WMWidget * w, void *data) WMListItem *item = WMGetListItem(panel->sizeL, i); int distance; - distance = abs(size - atoi(item->text)); + distance = fabs(size - atoi(item->text)); if (i == 0 || distance < closest) { closest = distance; diff --git a/src/menu.c b/src/menu.c index 6164e28f..ebdd2b43 100644 --- a/src/menu.c +++ b/src/menu.c @@ -1436,7 +1436,7 @@ static void getScrollAmount(WMenu * menu, int *hamount, int *vamount) } else if (xroot >= (rect.pos.x + rect.size.width - 2) && menuX2 > (rect.pos.x + rect.size.width - 1)) { /* scroll to the left */ - *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2 - rect.pos.x - rect.size.width - 1)); + *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2 - rect.pos.x - (int)rect.size.width - 1)); if (*hamount == 0) *hamount = 1; @@ -1450,7 +1450,7 @@ static void getScrollAmount(WMenu * menu, int *hamount, int *vamount) } else if (yroot >= (rect.pos.y + rect.size.height - 2) && menuY2 > (rect.pos.y + rect.size.height - 1)) { /* scroll up */ - *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2 - rect.pos.y - rect.size.height - 2)); + *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2 - rect.pos.y - (int)rect.size.height - 2)); *vamount = -*vamount; } diff --git a/src/moveres.c b/src/moveres.c index 9f7a91de..4e0d7deb 100644 --- a/src/moveres.c +++ b/src/moveres.c @@ -2002,8 +2002,8 @@ static int getResizeDirection(WWindow * wwin, int x, int y, int dy, int flags) int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN; /* How much resize space is allowed */ - int spacew = abs(wwin->client.width / 3); - int spaceh = abs(wwin->client.height / 3); + int spacew = wwin->client.width / 3; + int spaceh = wwin->client.height / 3; /* Determine where x fits */ if ((abs(x) > wwin->client.width/2 - spacew/2) && diff --git a/src/window.c b/src/window.c index 195c3e6a..a276f483 100644 --- a/src/window.c +++ b/src/window.c @@ -997,9 +997,9 @@ WWindow *wManageWindow(WScreen *scr, Window window) int head; x = transientOwner->frame_x + - abs((transientOwner->frame->core->width - width) / 2) + offs; + abs(((int)transientOwner->frame->core->width - (int)width) / 2) + offs; y = transientOwner->frame_y + - abs((transientOwner->frame->core->height - height) / 3) + offs; + abs(((int)transientOwner->frame->core->height - (int)height) / 3) + offs; /* limit transient windows to be inside their parent's head */ rect.pos.x = transientOwner->frame_x; diff --git a/src/xinerama.c b/src/xinerama.c index 483046c7..4c0beb2f 100644 --- a/src/xinerama.c +++ b/src/xinerama.c @@ -261,28 +261,28 @@ int wGetHeadRelativeToCurrentHead(WScreen *scr, int current_head, int direction) case DIRECTION_LEFT: if (rect->pos.x < crect.pos.x) { found = 1; - distance = abs((rect->pos.x + rect->size.width) + distance = abs((rect->pos.x + (int)rect->size.width) - crect.pos.x) + abs(rect->pos.y + crect.pos.y); } break; case DIRECTION_RIGHT: if (rect->pos.x > crect.pos.x) { found = 1; - distance = abs((crect.pos.x + crect.size.width) + distance = abs((crect.pos.x + (int)crect.size.width) - rect->pos.x) + abs(rect->pos.y + crect.pos.y); } break; case DIRECTION_UP: if (rect->pos.y < crect.pos.y) { found = 1; - distance = abs((rect->pos.y + rect->size.height) + distance = abs((rect->pos.y + (int)rect->size.height) - crect.pos.y) + abs(rect->pos.x + crect.pos.x); } break; case DIRECTION_DOWN: if (rect->pos.y > crect.pos.y) { found = 1; - distance = abs((crect.pos.y + crect.size.height) + distance = abs((crect.pos.y + (int)crect.size.height) - rect->pos.y) + abs(rect->pos.x + crect.pos.x); } break;