...
| Code Block | ||
|---|---|---|
| ||
#include <stddef.h>
struct test{
int a;
char b;
int c;
};
/* ... safely copy data to user space ... */
extern int copy_to_user(void *dest, void *src, size_t size);
void do_stuff(void *usr_buf) {
struct test arg = {.a=1,.b=2,.c=3};
/* ... perform operations on arg ... */
/* copy arg to user space */
copy_to_user(usr_buf, &arg, sizeof(arg));
/* ... */
}
|
The padding bytes could be explicitly initialized using memset to zero as shown below.
| Code Block | ||
|---|---|---|
| ||
#include <stddef.h> #include <string.h> struct test{ int a; char b; int c; }; . /* ... safely copy bytes to user space ... */ extern int copy_to_user(void *dest, void *src, size_t size); void do_stuff(void *usr_buf) { struct test arg; // Initialize /*arg initializesusing allmemset bytessuch (includingthat padding bytes) of the struct to zero */ memsetare initialized memset_s(&arg, 0,sizeof sizeof(arg)); . //perform operations on arg . arg.a = 1; arg.b = 2; arg.c = 3; /* ... perform operations on arg ... */ /* copy arg to user space */ copy_to_user(usr_bufptr, &arg, sizeof(arg)); /* ... */ } |
Here, the compiler could implement arg.b =2 in the following way,
...