lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <200803121820.14883.mb@bu3sch.de>
Date:	Wed, 12 Mar 2008 18:20:14 +0100
From:	Michael Buesch <mb@...sch.de>
To:	Harvey Harrison <harvey.harrison@...il.com>
Cc:	Andrew Morton <akpm@...ux-foundation.org>,
	LKML <linux-kernel@...r.kernel.org>,
	Alan Cox <alan@...rguk.ukuu.org.uk>,
	Jeff Garzik <jeff@...zik.org>,
	Bartlomiej Zolnierkiewicz <bzolnier@...il.com>,
	Mauro Carvalho Chehab <mchehab@...radead.org>
Subject: Re: [PATCH 1/6] kernel: add clamp(), clamp_t() and clamp_val() macros

On Wednesday 12 March 2008 17:54:26 Harvey Harrison wrote:
> On Wed, 2008-03-12 at 16:13 +0100, Michael Buesch wrote:
> > So why not call it clamp_const()?
> > One could even use __builtin_constant_p() and make clamp() use
> > either clamp_const() or clamp_nonconst() from above automagically.
> > I'd prefer that.
> 
> Did you mean something like this?  No more clamp_val, just clamp and
> clamp_t.  clamp_t forces all the types, clamp looks at the min and max
> args, and if they are constants, uses the type of val instead.  If not
> a constant, the strict typechecking is done.

> +#define clamp(val, min, max) ({				\
> +	typeof(val) __val = (val);			\
> +							\
> +	if (__builtin_constant_p(min)) {		\
> +		typeof(val) __min = (min);		\
> +		__val = __val < __min ? __min: __val;	\
> +	} else {					\
> +		typeof(min) __min = (min);		\
> +		(void) (&__val == &__min);		\
> +		__val = __val < __min ? __min: __val;	\
> +	}						\
> +							\
> +	if (__builtin_constant_p(max)) {		\
> +		typeof(val) __max = (max);		\
> +		__val > __max ? __max: __val;		\
> +	} else {					\
> +		typeof(max) __max = (max);		\
> +		(void) (&__val == &__max);		\
> +		__val > __max ? __max: __val;		\
> +	} })

Yeah, something like that.
Does returning of the value work over an indentation level, too?
I dunno this detail of the language.
But I'd prefer the following for readability anyway:

+       if (__builtin_constant_p(max)) {                \
+               typeof(val) __max = (max);              \
+               __val = __val > __max ? __max: __val;           \
+       } else {                                        \
+               typeof(max) __max = (max);              \
+               (void) (&__val == &__max);              \
+               __val = __val > __max ? __max: __val;           \
+       }
+	__val; })

Probably you can also only put the pointer check into the constant check:

+#define clamp(val, min, max) ({                \
+       typeof(val) __val = (val);              \
+       typeof(min) __min = (min);              \
+       typeof(max) __max = (max);              \
+       if (!__builtin_constant_p(min))         \
+               (void) (&__val == &__min);      \
+       __val = __val < __min ? __min: __val;   \
+       if (!__builtin_constant_p(max))         \
+               (void) (&__val == &__max);      \
+       __val = __val > __max ? __max: __val;   \
+       __val; })

But it seems that this evaluates the arguments twice, so my idea turns out
to be not too good anyway. hm..

-- 
Greetings Michael.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ