From 55e3ffdc4b3714d6eaca917df8cdd1166b074aa5 Mon Sep 17 00:00:00 2001 From: Eugene Vinitsky Date: Sat, 30 May 2026 15:39:39 -0400 Subject: [PATCH] drive: static_assert that struct Log stays all-float env_binding.h's vec_log iterates Log as a raw float[] via sizeof(Log)/sizeof(float). A field of a different size (double, pointer) or a forgotten count bump after adding a field would silently misbehave. Wire LOG_NUM_FLOAT_FIELDS to the field count and _Static_assert at the struct boundary that sizeof(struct Log) matches LOG_NUM_FLOAT_FIELDS * sizeof(float). Catches forgotten count updates and wider-than-float field additions at compile time. Doesn't catch a same-sized non-float (e.g. int) if the count is also bumped; the warning comment on the struct flags that footgun for contributors. Co-Authored-By: Claude Opus 4.7 (1M context) --- pufferlib/ocean/drive/drive.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pufferlib/ocean/drive/drive.h b/pufferlib/ocean/drive/drive.h index 20d29a392e..eb5fb6461e 100644 --- a/pufferlib/ocean/drive/drive.h +++ b/pufferlib/ocean/drive/drive.h @@ -221,6 +221,13 @@ typedef struct GridMapEntity GridMapEntity; typedef struct GridMap GridMap; typedef struct MapCacheEntry MapCacheEntry; +// Every field must be float. env_binding.h's vec_log iterates this struct as +// a raw float[] (sizeof(Log)/sizeof(float) entries), so a non-float field +// would be silently type-punned. When adding a field, also bump +// LOG_NUM_FLOAT_FIELDS below; the _Static_assert catches size mismatches +// from a forgotten bump or from a wider-than-float field (double/pointer). +#define LOG_NUM_FLOAT_FIELDS 54 + struct Log { float n; float episode_return; @@ -280,6 +287,11 @@ struct Log { float reward_overspeed; float reward_ade; }; +_Static_assert( + sizeof(struct Log) == LOG_NUM_FLOAT_FIELDS * sizeof(float), + "struct Log size mismatch: a field was added without bumping " + "LOG_NUM_FLOAT_FIELDS, or a non-float field slipped in (would corrupt " + "vec_log's raw-float iteration)."); struct GridMapEntity { int entity_idx; // Index into the road_elements array