MEDIUM: stats-file: add some BUG_ON() guards to ensure exported structs are not changed by accident

Add two BUG_ON() in shm_stats_file_prepare() which will trigger if
exported structures (shm_stats_file_hdr and shm_stats_file_object) change
in size, because it means that they will become incompatible with older
versions and thus precautions should be taken by the developer to ensure
compatibility with olders versions, or at least detect incompatible
versions by changing the version number to prevent bugs resulting
from inconsistent mapping between versions. The BUG_ON() may be
safely adjusted then.

Please note that it doesn't protect against accidental struct member
re-ordering if the resulting struct size is equal..
This commit is contained in:
Aurelien DARRAGON 2025-09-03 16:23:33 +02:00
parent 1a1362ea0b
commit f32bc8f0a4

View File

@ -802,6 +802,13 @@ int shm_stats_file_prepare(void)
int slot;
int objects;
BUG_ON(sizeof(struct shm_stats_file_hdr) != 672, "shm_stats_file_hdr struct size changed, "
"it is part of the exported API: ensure all precautions were taken (ie: shm_stats_file "
"version change) before adjusting this");
BUG_ON(sizeof(struct shm_stats_file_object) != 544, "shm_stats_file_object struct size changed, "
"is is part of the exported API: ensure all precautions were taken (ie: shm_stats_file "
"version change) before adjusting this");
/* do nothing if master process or shm_stats_file not configured */
if (master || !global.shm_stats_file)
return ERR_NONE;