On Fri, Nov 19, 2021 at 03:35:48PM +0100, Laszlo Ersek wrote:
(1) What is the usual style in nbdkit to highlight parameters of
functions and function-like macros in documentation (comments)? Here I
used parentheses. Normally I use quotes "".
We don't have one, so quotes are fine.
(2) In the actual functions, we return "true" for
"overflow". My
original conditions expressed "no overflow". In this source code, I
*open-coded* the negations of my original conditions, using De Morgan's
laws. That's because I dislike expressions with an outermost "!"
operator -- whenever I face such an expression, I always work the
outermost negation *into* the formula, as deeply as I can.
However, I realize the resultant conditions may not be easy to read this
way. Here's an alternative for the body of check_add_overflow(), based
on the original formulation:
register bool in_range;
*r = a + b;
in_range = a <= UINTMAX_MAX - b && *r <= max;
return !in_range;
Seems reasonable.
(3) In the overflow conditions, I used parentheses as sparingly as
possible; only when needed. I could also use as many as possible.
- Compare, for the addition:
return ((a > UINTMAX_MAX - b) || (*r > max));
or
in_range = ((a <= UINTMAX_MAX - b) && (*r <= max));
return !in_range;
- Compare, for the multiplication:
return ((b > 0) && ((a > UINTMAX_MAX / b) || (*r > max)));
or
in_range = ((b == 0) || ((a <= UINTMAX_MAX / b) && (*r <= max)));
return !in_range;
What's the preferred form?
Personally I use fewest parens, but indicate precedence by dropping
whitespace, but I guess whatever works. However you definitely don't
need the parens around the entire return value, C doesn't require
that.
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html