From: Paul Eggert <eggert@cs.ucla.edu>
To: DJ Delorie <dj@redhat.com>, Carlos O'Donell <carlos@redhat.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] [BZ 20628] make mallinfo saturating
Date: Thu, 06 Oct 2016 00:45:00 -0000 [thread overview]
Message-ID: <3bb80768-55c6-e993-8ff1-a434aed589e0@cs.ucla.edu> (raw)
In-Reply-To: <xn1szvrice.fsf@greed.delorie.com>
On 10/04/2016 01:04 PM, DJ Delorie wrote:
> +/* Saturated add - add ADD to SUM. If the result exceeds the range of
> + "int", set SUM to UINT_MAX instead ((int)-1). Assumes ADD and SUM
> + are positive. The published ABI prevents us from bumping "int" to
> + a larger type. */
> +#define SAT_ADD(SUM, ADD) \
> + ({ INTERNAL_SIZE_T tmp = (INTERNAL_SIZE_T)(SUM) + (INTERNAL_SIZE_T)(ADD); SUM = (tmp > UINT_MAX) ? -1 : tmp; })
> +
> +/* Likewise, but assign ADD to SUM. */
> +#define SAT_SET(SUM, ADD) \
> + ({ SUM = ((INTERNAL_SIZE_T)(ADD) > UINT_MAX) ? -1 : (ADD); })
These don't look right, as INTERNAL_SIZE_T might not be wider than int,
which means the + in SAT_ADD will wrap around before SAT_ADD gets a
chance to check for overflow. Also, if SUM is INT_MIN then SAT_ADD(SUM,
1) should set it to INT_MIN + 1, but that doesn't happen when
INTERNAL_SIZE_T is wider than int.
How about something like the following instead?
#define MIN(a, b) ((a)<(b)?(a):(b))
/* Return the sum of the unsigned int A and the nonnegative integer B.
If the result is not representable as an unsigned int, return
UINT_MAX.
B's type may be any integer type. */
#define UINT_SATURATED_ADD(a, b) \
({ unsigned a1 = a, b1 = MIN (b, UINT_MAX), sum1 = a1 + b1; \
sum1 < a1 ? UINT_MAX : sum1; })
and then set 'm->smblks = UINT_SATURATED_ADD (m->sm_blks, nfastblocks);'
and 'm->hblks = UINT_SATURATED_ADD (0, mp_.n_mmaps);', etc.
next prev parent reply other threads:[~2016-10-06 0:45 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-27 18:09 DJ Delorie
2016-09-27 19:07 ` Paul Eggert
2016-09-27 19:17 ` DJ Delorie
2016-09-28 20:26 ` Carlos O'Donell
2016-10-04 20:04 ` DJ Delorie
2016-10-06 0:45 ` Paul Eggert [this message]
2016-10-06 1:46 ` DJ Delorie
2016-10-06 6:34 ` Paul Eggert
2016-10-06 16:52 ` DJ Delorie
2016-10-06 20:40 ` Paul Eggert
2016-10-06 20:43 ` Joseph Myers
2016-10-06 21:21 ` Paul Eggert
2016-10-06 17:20 ` DJ Delorie
2017-01-13 2:14 DJ Delorie
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3bb80768-55c6-e993-8ff1-a434aed589e0@cs.ucla.edu \
--to=eggert@cs.ucla.edu \
--cc=carlos@redhat.com \
--cc=dj@redhat.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).