Skip to content

Commit

Permalink
Use _Static_assert to check macro lengths
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
  • Loading branch information
pguyot committed Jan 14, 2025
1 parent 13b327d commit 2bb0dd7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
13 changes: 8 additions & 5 deletions src/libAtomVM/defaultatoms.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@
#include <stdlib.h>
#include <string.h>

// About X macro: https://en.wikipedia.org/wiki/X_macro
#if __has_builtin(__builtin_strlen)
#define X(name, lenstr, str) \
_Static_assert(lenstr[0] == __builtin_strlen(str), "Macro length mismatch for " # name);
#include "defaultatoms.def"
#undef X
#endif

void defaultatoms_init(GlobalContext *glb)
{

// About X macro: https://en.wikipedia.org/wiki/X_macro
#define X(name, lenstr, str) \
lenstr str,

Expand All @@ -39,10 +46,6 @@ void defaultatoms_init(GlobalContext *glb)
#undef X

for (int i = 0; i < PLATFORM_ATOMS_BASE_INDEX; i++) {
if (UNLIKELY((size_t) atoms[i][0] != strlen(atoms[i] + 1))) {
AVM_ABORT();
}

if (UNLIKELY(globalcontext_insert_atom(glb, atoms[i]) != i)) {
AVM_ABORT();
}
Expand Down
13 changes: 8 additions & 5 deletions src/platforms/esp32/components/avm_sys/platform_defaultatoms.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
#include <stdlib.h>
#include <string.h>

// About X macro: https://en.wikipedia.org/wiki/X_macro
#if __has_builtin(__builtin_strlen)
#define X(name, lenstr, str) \
_Static_assert(lenstr[0] == __builtin_strlen(str), "Macro length mismatch for " # name);
#include "platform_defaultatoms.def"
#undef X
#endif

void platform_defaultatoms_init(GlobalContext *glb)
{
// About X macro: https://en.wikipedia.org/wiki/X_macro
#define X(name, lenstr, str) \
lenstr str,

Expand All @@ -38,10 +45,6 @@ void platform_defaultatoms_init(GlobalContext *glb)
#undef X

for (int i = 0; i < ATOM_FIRST_AVAIL_INDEX - PLATFORM_ATOMS_BASE_INDEX; i++) {
if (UNLIKELY((size_t) atoms[i][0] != strlen(atoms[i] + 1))) {
AVM_ABORT();
}

if (UNLIKELY(globalcontext_insert_atom(glb, atoms[i]) != i + PLATFORM_ATOMS_BASE_INDEX)) {
AVM_ABORT();
}
Expand Down
13 changes: 8 additions & 5 deletions src/platforms/generic_unix/lib/platform_defaultatoms.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
#include <stdlib.h>
#include <string.h>

// About X macro: https://en.wikipedia.org/wiki/X_macro
#if __has_builtin(__builtin_strlen)
#define X(name, lenstr, str) \
_Static_assert(lenstr[0] == __builtin_strlen(str), "Macro length mismatch for " # name);
#include "platform_defaultatoms.def"
#undef X
#endif

void platform_defaultatoms_init(GlobalContext *glb)
{
// About X macro: https://en.wikipedia.org/wiki/X_macro
#define X(name, lenstr, str) \
lenstr str,

Expand All @@ -38,10 +45,6 @@ void platform_defaultatoms_init(GlobalContext *glb)
#undef X

for (int i = 0; i < ATOM_FIRST_AVAIL_INDEX - PLATFORM_ATOMS_BASE_INDEX; i++) {
if (UNLIKELY((size_t) atoms[i][0] != strlen(atoms[i] + 1))) {
AVM_ABORT();
}

if (UNLIKELY(globalcontext_insert_atom(glb, atoms[i]) != i + PLATFORM_ATOMS_BASE_INDEX)) {
AVM_ABORT();
}
Expand Down

0 comments on commit 2bb0dd7

Please sign in to comment.