diff mbox series

[pseudo,1/2] pseudo_ipc.h: make 8 byte alignment for structure pseudo_msg_t

Message ID 20251111135951.2121065-1-hongxu.jia@windriver.com
State New
Headers show
Series [pseudo,1/2] pseudo_ipc.h: make 8 byte alignment for structure pseudo_msg_t | expand

Commit Message

Hongxu Jia Nov. 11, 2025, 1:59 p.m. UTC
typedef struct {
    pseudo_msg_type_t type;
    ...
    char path[];
} pseudo_msg_t;

The [] item at the end of the struct is a C99 feature:
Flexible Array Members in a structure in C [1]. The size
of path in structure should be 0.

On 64-bit arch, sizeof(pseudo_msg_t) = 80, but PSEUDO_HEADER_SIZE =
offsetof(pseudo_msg_t, path) = 76, the reason is 8 byte alignment
in 64-bit arch.

The differ between sizeof(pseudo_msg_t) and PSEUDO_HEADER_SIZE
made memory overlab in memcpy, such as

  memcpy(oldmsg, msg, PSEUDO_HEADER_SIZE);
  memcpy(newmsg, old, sizeof(pseudo_msg_t) + old->pathlen);

Add 4 chars to structure as reserved for 8 byte alignment, then
PSEUDO_HEADER_SIZE = sizeof(pseudo_msg_t) = 80.

[1] https://www.geeksforgeeks.org/c/flexible-array-members-structure-c/

Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
---
 pseudo_ipc.h | 1 +
 1 file changed, 1 insertion(+)
diff mbox series

Patch

diff --git a/pseudo_ipc.h b/pseudo_ipc.h
index d945257..2836cc4 100644
--- a/pseudo_ipc.h
+++ b/pseudo_ipc.h
@@ -26,6 +26,7 @@  typedef struct {
 	unsigned int pathlen;
 	int nlink;
 	int deleting;
+	char reserved[4];
 	char path[];
 } pseudo_msg_t;