@@ -101,6 +101,12 @@ static bool expect_context_not_null(const enum handcrafted_file_type hft) {
101101
102102typedef std::pair<enum ggml_type, std::array<int64_t , GGML_MAX_DIMS>> tensor_config_t ;
103103
104+ // Helper function to safely cast to gguf_type, suppressing sanitizer warnings for intentional invalid values
105+ static inline enum gguf_type __attribute__ ((no_sanitize(" undefined" )))
106+ safe_cast_to_gguf_type(int value) {
107+ return static_cast <enum gguf_type>(value);
108+ }
109+
104110static std::vector<tensor_config_t > get_tensor_configs (std::mt19937 & rng) {
105111 std::vector<tensor_config_t > tensor_configs;
106112 tensor_configs.reserve (100 );
@@ -140,7 +146,9 @@ static std::vector<std::pair<enum gguf_type, enum gguf_type>> get_kv_types(std::
140146 continue ;
141147 }
142148
143- kv_types.push_back (std::make_pair (type, gguf_type (-1 )));
149+ // Intentionally create invalid enum value for testing error handling
150+ // Suppress sanitizer warning as this is intentional undefined behavior for testing
151+ kv_types.push_back (std::make_pair (type, safe_cast_to_gguf_type (-1 )));
144152 }
145153 std::shuffle (kv_types.begin (), kv_types.end (), rng);
146154
@@ -232,8 +240,10 @@ static FILE * get_handcrafted_file(const unsigned int seed, const enum handcraft
232240 }
233241
234242 for (int i = 0 ; i < int (kv_types.size ()); ++i) {
235- const enum gguf_type type = gguf_type (hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].first );
236- const enum gguf_type type_arr = gguf_type (hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].second );
243+ // Intentionally create invalid enum values for testing error handling
244+ // Suppress sanitizer warning as this is intentional undefined behavior for testing
245+ const enum gguf_type type = safe_cast_to_gguf_type (hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].first );
246+ const enum gguf_type type_arr = safe_cast_to_gguf_type (hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].second );
237247
238248 const std::string key = " my_key_" + std::to_string ((hft == HANDCRAFTED_KV_DUPLICATE_KEY ? i/2 : i));
239249
@@ -463,8 +473,9 @@ static bool handcrafted_check_kv(const gguf_context * gguf_ctx, const unsigned i
463473 bool ok = true ;
464474
465475 for (int i = 0 ; i < int (kv_types.size ()); ++i) {
466- const enum gguf_type type = gguf_type (kv_types[i].first );
467- const enum gguf_type type_arr = gguf_type (kv_types[i].second );
476+ // Suppress sanitizer warning for intentional invalid enum values in test data
477+ const enum gguf_type type = safe_cast_to_gguf_type (kv_types[i].first );
478+ const enum gguf_type type_arr = safe_cast_to_gguf_type (kv_types[i].second );
468479
469480 const std::string key = " my_key_" + std::to_string (i);
470481
0 commit comments