This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Use saturated arithmetic for overflow detection.
- From: Paul Eggert <eggert at cs dot ucla dot edu>
- To: OndÅej BÃlka <neleai at seznam dot cz>
- Cc: libc-alpha at sourceware dot org
- Date: Wed, 30 Oct 2013 11:44:18 -0700
- Subject: Re: [PATCH] Use saturated arithmetic for overflow detection.
- Authentication-results: sourceware.org; auth=none
- References: <20131030174502 dot GA18107 at domone dot podge>
On 10/30/2013 10:45 AM, OndÅej BÃlka wrote:
> - if (BE ((((SIZE_MAX - (sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX)
> - / (3 * sizeof (re_dfastate_t *)))
> - < ndests),
> - 0))
> - goto out_free;
> + size_t allocated = (sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX;
> + size_t needed_memory = MUL_S (MUL_S (ndests, 3), sizeof (re_dfastate_t *));
>
> - if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX
> - + ndests * 3 * sizeof (re_dfastate_t *)))
> - dest_states = (re_dfastate_t **)
> - alloca (ndests * 3 * sizeof (re_dfastate_t *));
> + if (__libc_use_alloca (ADD_S (allocated, needed_memory)))
> + dest_states = (re_dfastate_t **) alloca (needed_memory);
This is slower than the original, since it has four conditional jumps
(MUL_S, MUL_S, ADD_S, __libc_use_alloca) instead of two (<,
__libc_use_alloca)..
All in all it's not clear that this approach is an improvement
overall, as the performance penalty may not be worth the increase in
clarity, and both approaches seem about equally error-prone.