From e9ba44d4b298952ec69493b6ff49a29fec7edb5f Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" Date: Sun, 5 Mar 2023 14:07:48 +0100 Subject: [PATCH] sa: add sa_struct_get_size() to check size The application and library must use the same build flags when building struct sa. If different build flags are used, random and mysterious crashes may occur. --- include/re_sa.h | 1 + src/sa/sa.c | 11 +++++++++++ test/sa.c | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/include/re_sa.h b/include/re_sa.h index 21f9c68ef..9670bfbcd 100644 --- a/include/re_sa.h +++ b/include/re_sa.h @@ -72,6 +72,7 @@ bool sa_is_any(const struct sa *sa); void sa_set_scopeid(struct sa *sa, uint32_t scopeid); uint32_t sa_scopeid(const struct sa *sa); +size_t sa_struct_get_size(void); struct re_printf; int sa_print_addr(struct re_printf *pf, const struct sa *sa); diff --git a/src/sa/sa.c b/src/sa/sa.c index 5e04ea64d..4b5c6410f 100644 --- a/src/sa/sa.c +++ b/src/sa/sa.c @@ -753,3 +753,14 @@ uint32_t sa_scopeid(const struct sa *sa) #endif return 0; } + + +/** + * Get the size of 'struct sa' as compiled by the library + * + * @return Size of 'struct sa' in bytes + */ +size_t sa_struct_get_size(void) +{ + return sizeof(struct sa); +} diff --git a/test/sa.c b/test/sa.c index f3eced9f9..0acdc0f0a 100644 --- a/test/sa.c +++ b/test/sa.c @@ -212,6 +212,12 @@ int test_sa_class(void) uint32_t i; int err = 0; + /* + * NOTE: The application and library must use the same build flags, + * so that the size of "struct sa" is the same. + */ + ASSERT_EQ(sizeof(struct sa), sa_struct_get_size()); + for (i=0; i