diff options
author | Jeremy Huddleston <jeremyhu@apple.com> | 2011-05-07 10:16:18 -0700 |
---|---|---|
committer | Jeremy Huddleston <jeremyhu@apple.com> | 2011-05-07 10:16:18 -0700 |
commit | 047993c76a677ca12a2b575990b99e3ddbc0dd58 (patch) | |
tree | be290e550ddd6b89a3d5cd719a99e1feb7d78e87 | |
parent | bee68e54e5c3a4b9f46c81366a720531e3e07a82 (diff) |
Correct error handling in _XcursorAverageColor
Previously it would either div-zero or get stuck in a loop until int overflow
if called with a bad value.
cursor.c:214:32: warning: Division by zero
return (0xff << 24) | ((red/npixels) << 16) | ((green/npixels) << 8) | (blue/npixels);
Found-by: clang static analyzer
Signed-off-by: Jeremy Huddleston <jeremyhu@apple.com>
-rw-r--r-- | src/cursor.c | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/src/cursor.c b/src/cursor.c index ac1b2c4..3de3c8e 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -201,6 +201,9 @@ _XcursorAverageColor (XcursorPixel *pixels, int npixels) XcursorPixel red, green, blue; int n = npixels; + if (n < 1) + return 0; + blue = green = red = 0; while (n--) { @@ -209,8 +212,6 @@ _XcursorAverageColor (XcursorPixel *pixels, int npixels) green += (p >> 8) & 0xff; blue += (p >> 0) & 0xff; } - if (!n) - return 0; return (0xff << 24) | ((red/npixels) << 16) | ((green/npixels) << 8) | (blue/npixels); } |