Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: The Visual Studio example now compiles; also, added push and pop pragmas to ensure other structures do not accidentally get the same packing.

...

Microsoft Visual Studio supports the #pragma pack() instead of the __packed__ attribute to ensure no padding bytes are added [MSDN].

Code Block
bgColor#CCCCFF
borderStylesolid
#include <stddef.h>

#pragma pack(push, 1) /* 1 byte */
struct test{
  int a;
  char b;
  int c;
};
#pragma pack(pop)
 
/* ... 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 = {.a=1,.b= 2,.c= 3};

  /* ... perform operations on arg ... */

  /* copy arg to user space */
  copy_to_user(usr_buf, &arg, sizeof(arg));

  /* ... */
}

...

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

DCL39-C

low

unlikely

medium

P2

L3

Bibliography

[ISO/IEC 9899:2011]Section 6.2.6.1, "General" (para. 6)