commit 1415c2504d7eedef64e53a92da09994e4a732361 parent 2bbdd4f7980b23c8caba508cbc3460f7d5e81564 Author: Christian Grothoff <grothoff@gnunet.org> Date: Thu, 9 Jul 2026 22:00:55 +0200 protect against accidental enum reordering Diffstat:
| M | src/challenger/challenger_cm_enums.c | | | 17 | ++++++++++++++--- |
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/src/challenger/challenger_cm_enums.c b/src/challenger/challenger_cm_enums.c @@ -41,16 +41,27 @@ CHALLENGER_cm_from_string (const char *method_str) } +/* Persisted wire format: the DB stores the enum as a uint32_t using its + ordinal value. The assertions below pin those ordinals so that + reordering or inserting enum values triggers a compile-time error + rather than a silent on-disk desync. */ +_Static_assert (CHALLENGER_CM_EMPTY == 0, + "DB wire format: CHALLENGER_CM_EMPTY must be 0"); +_Static_assert (CHALLENGER_CM_PLAIN == 1, + "DB wire format: CHALLENGER_CM_PLAIN must be 1"); +_Static_assert (CHALLENGER_CM_S256 == 2, + "DB wire format: CHALLENGER_CM_S256 must be 2"); + enum CHALLENGER_CM CHALLENGER_cm_from_int (uint32_t method_int) { switch (method_int) { - case 0: + case CHALLENGER_CM_EMPTY: return CHALLENGER_CM_EMPTY; - case 1: + case CHALLENGER_CM_PLAIN: return CHALLENGER_CM_PLAIN; - case 2: + case CHALLENGER_CM_S256: return CHALLENGER_CM_S256; default: /* Invalid or unrecognized value */