Skip to contentSkip to conversation

{put,get}_user() side effects

DISCUSSION Andi Kleen 7 participants 9 messages Apr 6, 2004 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Andi Kleen

author
#1 lore raw
Quoted 4 lines · Geert Uytterhoeven

On most (all?) architectures {get,put}_user() has side effects:

#define put_user(x,ptr) \
__put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))

Neither typeof not sizeof are supposed to have side effects. If your compiler generates them that's a compiler bug.

-Andi

#2 lore raw
Quoted 10 lines · Andi Kleen
Quoted 4 lines · Geert Uytterhoeven

On most (all?) architectures {get,put}_user() has side effects:

#define put_user(x,ptr) \
__put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))

Neither typeof not sizeof are supposed to have side effects. If your compiler generates them that's a compiler bug.

From a simple compile test, you seem to be right... Weird, since it does expand

to 3 times 'pIndex++', but pIndex is incremented only once.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#3 lore raw
Quoted 4 lines · Geert Uytterhoeven
Quoted 3 lines · Andi Kleen
Quoted 2 lines · Geert Uytterhoeven

On most (all?) architectures {get,put}_user() has side effects:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

#define put_user(x,ptr) \
__put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))

Neither typeof not sizeof are supposed to have side effects. If your compiler generates them that's a compiler bug.

From a simple compile test, you seem to be right... Weird, since it does expand to 3 times 'pIndex++', but pIndex is incremented only once.

Better check with a C language lawyer. Maybe gcc gets it wrong, or it is undefined (in which case next gcc could screw you over, and give you a hard time finding out how...). An inline should be safe, and unless gcc still gets them wrong, equally fast/small.

Signature · 4 lines
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513
#4 lore raw
Quoted 8 lines · Horst von Brand
Quoted 7 lines · Geert Uytterhoeven
Quoted 6 lines · Andi Kleen
Quoted 4 lines · Geert Uytterhoeven

On most (all?) architectures {get,put}_user() has side effects:

#define put_user(x,ptr) \
__put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Neither typeof not sizeof are supposed to have side effects. If your compiler generates them that's a compiler bug.

From a simple compile test, you seem to be right... Weird, since it does expand to 3 times 'pIndex++', but pIndex is incremented only once.

Better check with a C language lawyer. Maybe gcc gets it wrong, or it is undefined

It's not undefined, the standard explicitly says that the argument of sizeof is not evaluated (unless its type is a VLA). I can't remember gcc ever getting that wrong.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux AG, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#5 lore raw
In reply to Andi Kleen
Quoted 9 lines · Andi Kleen
Quoted 4 lines · Geert Uytterhoeven

On most (all?) architectures {get,put}_user() has side effects:

#define put_user(x,ptr) \
__put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))

Neither typeof not sizeof are supposed to have side effects. If your compiler generates them that's a compiler bug.

Using ptr three times in a define has side effects if ptr is an expression with side effects (e.g. "p++").

Xav

#6 lore raw
Quoted 2 lines · Previously Xavier Bestel

Using ptr three times in a define has side effects if ptr is an expression with side effects (e.g. "p++").

As I understand it both typeof and sizeof don't evalutate their argument but only look at its type. Which means using p++ is perfectly safe.

Wichert.

Signature · 2 lines
Wichert Akkerman <[email protected]>    It is simple to make things.
http://www.wiggy.net/                   It is hard to make things simple.
#7 lore raw
In reply to Andi Kleen
Quoted 5 lines · Andi Kleen
Quoted 4 lines
Quoted 2 lines · Geert Uytterhoeven

On most (all?) architectures {get,put}_user() has side effects:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

#define put_user(x,ptr) \
__put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))

Neither typeof not sizeof are supposed to have side effects. If your compiler generates them that's a compiler bug.

Using ptr three times in a define has side effects if ptr is an expression with side effects (e.g. "p++").

Sorry, I read too fast. I didn't know sizeof could avoid side effects.

Xav

#8 lore raw
Quoted 2 lines · Xavier Bestel

Sorry, I read too fast. I didn't know sizeof could avoid side effects.

Both typeof and sizeof are compile-time constructs, so there is no opportunity for any expression side effects to occur. Presumably one could do:

typeof(1/0UL)

without ever causing the obvious side effect either (granted, this is a pointless piece of code :-)).

#9 lore raw
Quoted 5 lines · Kevin P. Fleming
Quoted 2 lines · Xavier Bestel

Sorry, I read too fast. I didn't know sizeof could avoid side effects.

Quoted 8 lines

Both typeof and sizeof are compile-time constructs, so there is no opportunity for any expression side effects to occur. Presumably one could do:

typeof(1/0UL)

without ever causing the obvious side effect either (granted, this is a pointless piece of code :-)).

Yeah, now that you tell it, it seems obvious when you consider macros like min/max and friends which rely on typeof to avoid side-effects.