2

Does the copy_from_user function, declared in uaccess.h, modify the (void __user *)from pointer? The pointer isn't declared as const in the function declaration, only the contents it points to.

The reason I ask is that I want to use copy_from_user twice, with the second copy_from_user copying from the place where the first one finished.

I was planning on doing something like this, is it guaranteed to work?

//buf is a user pointer that is already defined
copy_from_user(my_first_alloced_region, buf, some_size);
//do stuff
copy_from_user(my_second_alloced_region, buf + some_size, some_other_size);

Thanks in advance.

Michael
  • 5,994
  • 7
  • 44
  • 56

1 Answers1

3

The callee function can't modify the pointer itself since you're simply passing the pointer-value as an argument to the function. If the argument is declared as a pointer to a const type, then the callee can't modify what is being pointed to either (at least not without a cast that would cast away the const-ness of the pointer). The only way to modify the pointer value in the caller itself would be to pass the callee a pointer-to-pointer type.

Jason
  • 31,834
  • 7
  • 59
  • 78
  • Ah, true, that is a rather rookie oversight on my part. So using the const keyword for pointers in function declarations is pointless? – Michael Sep 18 '12 at 03:04
  • If your desire is to protect the pointer-value itself, then yes, it's pointless ... `const T*` is a pointer to a `const` object of type `T`, so it protects the object being pointed to, not the pointer-value. But since you're only passing a pointer-value, and not the pointer-by-reference, the pointer-variable itself cannot be changed in the caller even if the callee were to modify it. – Jason Sep 18 '12 at 03:20
  • I meant using the const type for the pointer itself `T* const var_name` – Michael Sep 18 '12 at 03:43
  • @Michael: It's as useless as declaring any of the parameters themselves `const` in a function declaration - eg. `int foo(const int a);` is in the same boat. It's not useless in a function definition, though. – caf Sep 18 '12 at 03:59