...
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/* usinginitializes memsetall suchbytes that(including padding bytes are initialized memset_s) of the struct to zero */ memset(&arg, 0, 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(ptrusr_buf, &arg, sizeof(arg)); /* ... */ } |
Here, the compiler could implement arg.b =2 in the following way,
...