Skip to content

Commit 2ae1afd

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

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

src/libAtomVM/defaultatoms.c

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

26+
// About X macro: https://en.wikipedia.org/wiki/X_macro
27+
#define X(name, lenstr, str) \
28+
_Static_assert(*lenstr == strlen(str), "Macro length mismatch for " # name);
29+
#include "defaultatoms.def"
30+
#undef X
31+
2632
void defaultatoms_init(GlobalContext *glb)
2733
{
2834

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

@@ -39,10 +44,6 @@ void defaultatoms_init(GlobalContext *glb)
3944
#undef X
4045

4146
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-
4647
if (UNLIKELY(globalcontext_insert_atom(glb, atoms[i]) != i)) {
4748
AVM_ABORT();
4849
}

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

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

26+
// About X macro: https://en.wikipedia.org/wiki/X_macro
27+
#define X(name, lenstr, str) \
28+
_Static_assert(*lenstr == strlen(str), "Macro length mismatch for " # name);
29+
#include "platform_defaultatoms.def"
30+
#undef X
31+
2632
void platform_defaultatoms_init(GlobalContext *glb)
2733
{
28-
// About X macro: https://en.wikipedia.org/wiki/X_macro
2934
#define X(name, lenstr, str) \
3035
lenstr str,
3136

@@ -38,10 +43,6 @@ void platform_defaultatoms_init(GlobalContext *glb)
3843
#undef X
3944

4045
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-
4546
if (UNLIKELY(globalcontext_insert_atom(glb, atoms[i]) != i + PLATFORM_ATOMS_BASE_INDEX)) {
4647
AVM_ABORT();
4748
}

src/platforms/generic_unix/lib/platform_defaultatoms.c

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

26+
// About X macro: https://en.wikipedia.org/wiki/X_macro
27+
#define X(name, lenstr, str) \
28+
_Static_assert(*lenstr == strlen(str), "Macro length mismatch for " # name);
29+
#include "platform_defaultatoms.def"
30+
#undef X
31+
2632
void platform_defaultatoms_init(GlobalContext *glb)
2733
{
28-
// About X macro: https://en.wikipedia.org/wiki/X_macro
2934
#define X(name, lenstr, str) \
3035
lenstr str,
3136

@@ -38,10 +43,6 @@ void platform_defaultatoms_init(GlobalContext *glb)
3843
#undef X
3944

4045
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-
4546
if (UNLIKELY(globalcontext_insert_atom(glb, atoms[i]) != i + PLATFORM_ATOMS_BASE_INDEX)) {
4647
AVM_ABORT();
4748
}

0 commit comments

Comments
 (0)