Re: void* arithmnetic

From: Arnaldo Carvalho de Melo
Date: Wed Nov 28 2007 - 19:23:16 EST


Em Thu, Nov 29, 2007 at 01:05:31AM +0100, J.A. MagallÃn escreveu:
> Hi all...
>
> Since begin of the ages the build of the nvidia driver says things like
> this:
>
> include/asm/compat.h:210: warning: pointer of type 'void *' used in arithmetic
>
> There are several of this warnings. The code in question for this example
> is:
>
> static __inline__ void __user *compat_alloc_user_space(long len)
> {
> struct pt_regs *regs = task_pt_regs(current);
> return (void __user *)regs->rsp - len;
> }
>
> As this is dealing with mem blocks, I suppose it's counting in bytes, so
> we could do something like:
>
> return (void __user *)((u8*)regs->rsp - len);
>
> so the arithmetic knows how to inc/dec for each unity...
> I think the warning is correct and that void* arithmetic is undefined in C,
> isn't it ?

Yes, but not in gcc, the language the kernel is written 8)

It is allowed and the size of a void is 1. -Wpointer-arith disables
this.

[acme@doppio ~]$ cat voidptr.c
#include <stdio.h>

int main(int argc, char *argv[])
{
void *ptr = argv[argc - 1];

puts(ptr + 4);
return 0;
}
[acme@doppio ~]$ gcc -Wall voidptr.c -o voidptr
[acme@doppio ~]$ ./a MagallÃn
llÃn
[acme@doppio ~]$ gcc -Wall -Wpointer-arith voidptr.c -o voidptr
voidptr.c: In function âmainâ:
voidptr.c:7: warning: pointer of type âvoid *â used in arithmetic
[acme@doppio ~]$ ./a MagallÃn
llÃn
[acme@doppio ~]$

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