Re: [PATCH] kernel.h: Skip single-eval logic on literals in min()/max()

From: Linus Torvalds
Date: Thu Mar 08 2018 - 18:48:19 EST


On Thu, Mar 8, 2018 at 1:40 PM, Kees Cook <keescook@xxxxxxxxxxxx> wrote:
> +#define __min(t1, t2, x, y) \
> + __builtin_choose_expr(__builtin_constant_p(x) && \
> + __builtin_constant_p(y) && \
> + __builtin_types_compatible_p(t1, t2), \
> + (t1)(x) < (t2)(y) ? (t1)(x) : (t2)(y), \

I understand why you use __builtin_types_compatible_p(), but please don't.

It will mean that trivial constants like "5" and "sizeof(x)" won't
simplify, because they have different types.

The ?: will give the right combined type anyway, and if you want the
type comparison warning, just add a comma-expression with something
like like

(t1 *)1 == (t2 *)1

to get the type compatibility warning.

Yeah, yeah, maybe none of the VLA cases triggered that, but it seems
silly to not just get that obvious constant case right.

Hmm?

Linus