Skip to content

Commit 2bb0dd7

Browse files
committed
Use _Static_assert to check macro lengths
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent 13b327d commit 2bb0dd7

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/libAtomVM/defaultatoms.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@
2323
#include <stdlib.h>
2424
#include <string.h>
2525

26+
// About X macro: https://en.wikipedia.org/wiki/X_macro
27+
#if __has_builtin(__builtin_strlen)
28+
#define X(name, lenstr, str) \
29+
_Static_assert(lenstr[0] == __builtin_strlen(str), "Macro length mismatch for " # name);
30+
#include "defaultatoms.def"
31+
#undef X
32+
#endif
33+
2634
void defaultatoms_init(GlobalContext *glb)
2735
{
2836

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

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

4148
for (int i = 0; i < PLATFORM_ATOMS_BASE_INDEX; i++) {
42-
if (UNLIKELY((size_t) atoms[i][0] != strlen(atoms[i] + 1))) {
43-
AVM_ABORT();
44-
}
45-
4649
if (UNLIKELY(globalcontext_insert_atom(glb, atoms[i]) != i)) {
4750
AVM_ABORT();
4851
}

src/platforms/esp32/components/avm_sys/platform_defaultatoms.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@
2323
#include <stdlib.h>
2424
#include <string.h>
2525

26+
// About X macro: https://en.wikipedia.org/wiki/X_macro
27+
#if __has_builtin(__builtin_strlen)
28+
#define X(name, lenstr, str) \
29+
_Static_assert(lenstr[0] == __builtin_strlen(str), "Macro length mismatch for " # name);
30+
#include "platform_defaultatoms.def"
31+
#undef X
32+
#endif
33+
2634
void platform_defaultatoms_init(GlobalContext *glb)
2735
{
28-
// About X macro: https://en.wikipedia.org/wiki/X_macro
2936
#define X(name, lenstr, str) \
3037
lenstr str,
3138

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

4047
for (int i = 0; i < ATOM_FIRST_AVAIL_INDEX - PLATFORM_ATOMS_BASE_INDEX; i++) {
41-
if (UNLIKELY((size_t) atoms[i][0] != strlen(atoms[i] + 1))) {
42-
AVM_ABORT();
43-
}
44-
4548
if (UNLIKELY(globalcontext_insert_atom(glb, atoms[i]) != i + PLATFORM_ATOMS_BASE_INDEX)) {
4649
AVM_ABORT();
4750
}

src/platforms/generic_unix/lib/platform_defaultatoms.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@
2323
#include <stdlib.h>
2424
#include <string.h>
2525

26+
// About X macro: https://en.wikipedia.org/wiki/X_macro
27+
#if __has_builtin(__builtin_strlen)
28+
#define X(name, lenstr, str) \
29+
_Static_assert(lenstr[0] == __builtin_strlen(str), "Macro length mismatch for " # name);
30+
#include "platform_defaultatoms.def"
31+
#undef X
32+
#endif
33+
2634
void platform_defaultatoms_init(GlobalContext *glb)
2735
{
28-
// About X macro: https://en.wikipedia.org/wiki/X_macro
2936
#define X(name, lenstr, str) \
3037
lenstr str,
3138

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

4047
for (int i = 0; i < ATOM_FIRST_AVAIL_INDEX - PLATFORM_ATOMS_BASE_INDEX; i++) {
41-
if (UNLIKELY((size_t) atoms[i][0] != strlen(atoms[i] + 1))) {
42-
AVM_ABORT();
43-
}
44-
4548
if (UNLIKELY(globalcontext_insert_atom(glb, atoms[i]) != i + PLATFORM_ATOMS_BASE_INDEX)) {
4649
AVM_ABORT();
4750
}

0 commit comments

Comments
 (0)