From 223acf44bab3016f4bc8fc01c3a038a2a7c3527a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 16 Jan 2024 02:29:53 +0100 Subject: [PATCH] gitypelib: Replace gi_typelib_check_format with compile-time static checks only There's no much time to have checks at runtime when the compiler can make sure that the structures size match the expected ones. --- girepository/gitypelib-internal.h | 3 - girepository/gitypelib.c | 95 +++++++++++-------------------- 2 files changed, 32 insertions(+), 66 deletions(-) diff --git a/girepository/gitypelib-internal.h b/girepository/gitypelib-internal.h index 26de62916..94626055d 100644 --- a/girepository/gitypelib-internal.h +++ b/girepository/gitypelib-internal.h @@ -1337,9 +1337,6 @@ gboolean gi_typelib_matches_gtype_name_prefix (GITypelib *typelib, const char *gtype_name); -GI_AVAILABLE_IN_ALL -void gi_typelib_check_format (void); - /** * gi_typelib_get_string: * @typelib: TODO diff --git a/girepository/gitypelib.c b/girepository/gitypelib.c index 8d56836cb..e2d1abec6 100644 --- a/girepository/gitypelib.c +++ b/girepository/gitypelib.c @@ -423,69 +423,39 @@ gi_typelib_get_dir_entry_by_error_domain (GITypelib *typelib, return NULL; } -/** - * gi_typelib_check_format: +/* When changing the size of a typelib structure, you are required to update + * the hardcoded size here. Do NOT change these to use sizeof(); these + * should match whatever is defined in the text specification and serve as + * a sanity check on structure modifications. * - * Check compile-time sizes of various typelib file format types are as - * expected. - * - * Since: 2.80 + * Everything else in the code however should be using sizeof(). */ -void -gi_typelib_check_format (void) -{ -#ifndef G_DISABLE_ASSERT - /* Check that struct layout is as we expect */ - - gboolean size_check_ok = TRUE; - -#define CHECK_SIZE(s,n) \ - if (sizeof(s) != n) \ - { \ - g_printerr ("sizeof("#s") is expected to be %d but is %zu.\n", \ - n, sizeof (s)); \ - size_check_ok = FALSE; \ - } - - /* When changing the size of a typelib structure, you are required to update - * the hardcoded size here. Do NOT change these to use sizeof(); these - * should match whatever is defined in the text specification and serve as - * a sanity check on structure modifications. - * - * Everything else in the code however should be using sizeof(). - */ - - CHECK_SIZE (Header, 112); - CHECK_SIZE (DirEntry, 12); - CHECK_SIZE (SimpleTypeBlob, 4); - CHECK_SIZE (ArgBlob, 16); - CHECK_SIZE (SignatureBlob, 8); - CHECK_SIZE (CommonBlob, 8); - CHECK_SIZE (FunctionBlob, 20); - CHECK_SIZE (CallbackBlob, 12); - CHECK_SIZE (InterfaceTypeBlob, 4); - CHECK_SIZE (ArrayTypeBlob, 8); - CHECK_SIZE (ParamTypeBlob, 4); - CHECK_SIZE (ErrorTypeBlob, 4); - CHECK_SIZE (ValueBlob, 12); - CHECK_SIZE (FieldBlob, 16); - CHECK_SIZE (RegisteredTypeBlob, 16); - CHECK_SIZE (StructBlob, 32); - CHECK_SIZE (EnumBlob, 24); - CHECK_SIZE (PropertyBlob, 16); - CHECK_SIZE (SignalBlob, 16); - CHECK_SIZE (VFuncBlob, 20); - CHECK_SIZE (ObjectBlob, 60); - CHECK_SIZE (InterfaceBlob, 40); - CHECK_SIZE (ConstantBlob, 24); - CHECK_SIZE (AttributeBlob, 12); - CHECK_SIZE (UnionBlob, 40); -#undef CHECK_SIZE - - g_assert (size_check_ok); -#endif /* !G_DISABLE_ASSERT */ -} +G_STATIC_ASSERT (sizeof (Header) == 112); +G_STATIC_ASSERT (sizeof (DirEntry) == 12); +G_STATIC_ASSERT (sizeof (SimpleTypeBlob) == 4); +G_STATIC_ASSERT (sizeof (ArgBlob) == 16); +G_STATIC_ASSERT (sizeof (SignatureBlob) == 8); +G_STATIC_ASSERT (sizeof (CommonBlob) == 8); +G_STATIC_ASSERT (sizeof (FunctionBlob) == 20); +G_STATIC_ASSERT (sizeof (CallbackBlob) == 12); +G_STATIC_ASSERT (sizeof (InterfaceTypeBlob) == 4); +G_STATIC_ASSERT (sizeof (ArrayTypeBlob) == 8); +G_STATIC_ASSERT (sizeof (ParamTypeBlob) == 4); +G_STATIC_ASSERT (sizeof (ErrorTypeBlob) == 4); +G_STATIC_ASSERT (sizeof (ValueBlob) == 12); +G_STATIC_ASSERT (sizeof (FieldBlob) == 16); +G_STATIC_ASSERT (sizeof (RegisteredTypeBlob) == 16); +G_STATIC_ASSERT (sizeof (StructBlob) == 32); +G_STATIC_ASSERT (sizeof (EnumBlob) == 24); +G_STATIC_ASSERT (sizeof (PropertyBlob) == 16); +G_STATIC_ASSERT (sizeof (SignalBlob) == 16); +G_STATIC_ASSERT (sizeof (VFuncBlob) == 20); +G_STATIC_ASSERT (sizeof (ObjectBlob) == 60); +G_STATIC_ASSERT (sizeof (InterfaceBlob) == 40); +G_STATIC_ASSERT (sizeof (ConstantBlob) == 24); +G_STATIC_ASSERT (sizeof (AttributeBlob) == 12); +G_STATIC_ASSERT (sizeof (UnionBlob) == 40); static gboolean is_aligned (uint32_t offset) @@ -614,9 +584,8 @@ validate_header_basic (const uint8_t *memory, /* This is a sanity check for a specific typelib; it * prevents us from loading an incompatible typelib. * - * The hardcoded checks in gi_typelib_check_format to - * protect against inadvertent or buggy changes to the typelib format - * itself. + * The hardcoded static checks to protect against inadvertent + * or buggy changes to the typelib format itself. */ if (header->entry_blob_size != sizeof (DirEntry) ||