From 515129ccccf53d05c4fe397b4e90f8e14b264d73 Mon Sep 17 00:00:00 2001 From: Lurker00 Date: Tue, 17 Sep 2024 22:29:47 +0300 Subject: [PATCH] Bug fix: uninitialized padding eliminated (#137) --- extend.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/extend.c b/extend.c index 5f6db8b..5b592ee 100644 --- a/extend.c +++ b/extend.c @@ -49,18 +49,24 @@ int mode_add_get(struct sockaddr_ina *dst, int m) { // m < 0: get, m > 0: set, m == 0: delete assert(m >= -1 && m < params.dp_count); - struct { + #pragma pack(push,4) + struct key_struct { uint16_t port; + uint16_t pad0; // fill with 0 before use! union { struct in_addr i4; struct in6_addr i6; }; - } key = { .port = dst->in.sin_port }; - + } key = { .port = dst->in.sin_port, .pad0 = 0 }; + #pragma pack(pop) + #if defined(__GNUC__) + _Static_assert(offsetof(struct key_struct, i4) == sizeof(key.port)+sizeof(key.pad0), "key_struct"); + #endif + time_t t = 0; struct elem *val = 0; - int len = sizeof(dst->in.sin_port); - + int len = offsetof(struct key_struct, i4); + if (dst->sa.sa_family == AF_INET) { len += sizeof(dst->in.sin_addr); key.i4 = dst->in.sin_addr; @@ -69,7 +75,7 @@ int mode_add_get(struct sockaddr_ina *dst, int m) len += sizeof(dst->in6.sin6_addr); key.i6 = dst->in6.sin6_addr; } - + if (m < 0) { val = mem_get(params.mempool, (char *)&key, len); if (!val) { @@ -83,7 +89,7 @@ int mode_add_get(struct sockaddr_ina *dst, int m) return val->m; } INIT_ADDR_STR((*dst)); - + if (m == 0) { LOG(LOG_S, "delete ip: %s\n", ADDR_STR); mem_delete(params.mempool, (char *)&key, len); @@ -101,7 +107,6 @@ int mode_add_get(struct sockaddr_ina *dst, int m) val->time = t; return 0; } - }