Date: Fri, 8 Apr 2016 09:51:00 +0200 From: Pieter de Goeje <pieter@degoeje.nl> To: Edward Tomasz Napierala <trasz@FreeBSD.org>, src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r297633 - in head: sys/cddl/contrib/opensolaris/uts/common/fs/zfs sys/fs/ext2fs sys/kern sys/sys sys/ufs/ffs sys/ufs/ufs sys/vm usr.bin/rctl Message-ID: <570762E4.6080706@degoeje.nl> In-Reply-To: <201604070423.u374NP0Z021115@repo.freebsd.org> References: <201604070423.u374NP0Z021115@repo.freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
Op 2016-04-07 om 06:23 schreef Edward Tomasz Napierala: > +static uint64_t > +xmul(uint64_t a, uint64_t b) > +{ > + uint64_t c; > + > + if (a == 0 || b == 0) > + return (0); > + > + c = a * b; > + > + if (c < a || c < b) > + return (UINT64_MAX); If the intent is to check for overflow, then this check is insufficient. It fails for example if a = 2^32+1 and b = 2^32. This works for all cases, assuming a != 0: if(UINT64_MAX / a > b) return (UINT64_MAX); If the extra division is too expensive, GCC and clang provide __builtin_mul_overflow(). -- Pieter de Goeje
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?570762E4.6080706>