From 9a675079290758a7f68e497146cc3688f1c3d364 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 23 Sep 2023 00:34:00 +0000 Subject: Fix arithmetic overflow when bits per sample is > 16 git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4679 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- lib/libpm.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/libpm.c b/lib/libpm.c index 78d941fa..b27b47d5 100644 --- a/lib/libpm.c +++ b/lib/libpm.c @@ -442,12 +442,20 @@ pm_maxvaltobits(int const maxval) { assert(false); } + + int -pm_bitstomaxval(int const bits) { - return ( 1 << bits ) - 1; +pm_bitstomaxval(int const bitCt) { + + if (bitCt > 16) + pm_error("Bits per sample %u too large. 16 is maximum acceptable", + bitCt); + + return (1 << bitCt) - 1; } + unsigned int PURE_FN_ATTR pm_lcm(unsigned int const x, unsigned int const y, -- cgit 1.4.1