Skip to content

Commit

Permalink
sa: add sa_struct_get_size() to check size
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alfredh authored and sreimers committed Mar 5, 2023
1 parent 806f3db commit e9ba44d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/re_sa.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
11 changes: 11 additions & 0 deletions src/sa/sa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
6 changes: 6 additions & 0 deletions test/sa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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<RE_ARRAY_SIZE(testv); i++) {
struct sa sa;
int lo, ll, any;
Expand Down

0 comments on commit e9ba44d

Please sign in to comment.