From 9f6f7bc9c5eba5dc327dec6d2848560139357308 Mon Sep 17 00:00:00 2001 From: Colomban Wendling Date: Thu, 20 Mar 2025 21:51:12 +0100 Subject: [PATCH 1/2] Add library attribute for no-failure allocators For example GLib and GTK abort on most allocation failures, making the nullPointerOutOfMemory check produce lots of false positives. To fix this, introduce a new allocator attribute "no-fail" to notify that this allocator cannot fail and that this check is thus irrelevant for it. --- cfg/cppcheck-cfg.rng | 12 + cfg/gtk.cfg | 912 +++++++++++++++++++++---------------------- lib/library.cpp | 1 + lib/library.h | 1 + lib/valueflow.cpp | 4 +- test/cfg/gtk.c | 2 - 6 files changed, 473 insertions(+), 459 deletions(-) diff --git a/cfg/cppcheck-cfg.rng b/cfg/cppcheck-cfg.rng index 36df004cb2f..a54c967aaac 100644 --- a/cfg/cppcheck-cfg.rng +++ b/cfg/cppcheck-cfg.rng @@ -26,6 +26,9 @@ + + + @@ -38,6 +41,9 @@ + + + @@ -69,6 +75,9 @@ + + + @@ -78,6 +87,9 @@ + + + diff --git a/cfg/gtk.cfg b/cfg/gtk.cfg index 492cf592988..44741ae5b24 100644 --- a/cfg/gtk.cfg +++ b/cfg/gtk.cfg @@ -206,689 +206,689 @@ - g_thread_new - g_thread_try_new + g_thread_new + g_thread_try_new g_thread_ref g_thread_unref g_thread_join - g_variant_iter_copy - g_variant_iter_new + g_variant_iter_copy + g_variant_iter_new g_variant_iter_free - g_source_new - g_idle_source_new - g_timeout_source_new - g_timeout_source_new_seconds - g_child_watch_source_new - g_cancellable_source_new - g_io_create_watch + g_source_new + g_idle_source_new + g_timeout_source_new + g_timeout_source_new_seconds + g_child_watch_source_new + g_cancellable_source_new + g_io_create_watch g_source_ref g_source_unref - g_date_time_new - g_date_time_new_now - g_date_time_new_now_local - g_date_time_new_now_utc - g_date_time_new_from_unix_local - g_date_time_new_from_unix_utc - g_date_time_new_from_timeval_local - g_date_time_new_from_timeval_utc - g_date_time_new_local - g_date_time_new_utc - g_date_time_add - g_date_time_add_years - g_date_time_add_months - g_date_time_add_weeks - g_date_time_add_days - g_date_time_add_hours - g_date_time_add_minutes - g_date_time_add_seconds - g_date_time_add_full - g_date_time_to_timezone - g_date_time_to_local - g_date_time_to_utc + g_date_time_new + g_date_time_new_now + g_date_time_new_now_local + g_date_time_new_now_utc + g_date_time_new_from_unix_local + g_date_time_new_from_unix_utc + g_date_time_new_from_timeval_local + g_date_time_new_from_timeval_utc + g_date_time_new_local + g_date_time_new_utc + g_date_time_add + g_date_time_add_years + g_date_time_add_months + g_date_time_add_weeks + g_date_time_add_days + g_date_time_add_hours + g_date_time_add_minutes + g_date_time_add_seconds + g_date_time_add_full + g_date_time_to_timezone + g_date_time_to_local + g_date_time_to_utc g_date_time_ref g_date_time_unref - g_dir_open + g_dir_open g_dir_rewind g_dir_close - g_timer_new + g_timer_new g_timer_destroy - g_file_attribute_info_list_new - g_file_attribute_info_list_dup + g_file_attribute_info_list_new + g_file_attribute_info_list_dup g_file_attribute_info_list_ref g_file_attribute_info_list_unref - g_slist_alloc - g_slist_copy - g_slist_copy_deep + g_slist_alloc + g_slist_copy + g_slist_copy_deep g_slist_free g_slist_free_1 g_slist_free_full - g_variant_new - g_variant_new_va - g_variant_new_boolean - g_variant_new_byte - g_variant_new_int16 - g_variant_new_uint16 - g_variant_new_int32 - g_variant_new_uint32 - g_variant_new_int64 - g_variant_new_uint64 - g_variant_new_handle - g_variant_new_double - g_variant_new_string - g_variant_new_take_string - g_variant_new_printf - g_variant_new_signature - g_variant_new_object_path - g_variant_new_variant - g_variant_new_objv - g_variant_new_strv - g_variant_new_bytestring - g_variant_new_bytestring_array - g_variant_new_maybe - g_variant_new_array - g_variant_new_tuple - g_variant_new_dict_entry - g_variant_new_fixed_array - g_variant_new_from_data - g_variant_new_from_bytes - g_variant_builder_end - g_variant_new_parsed_va - g_variant_new_parsed - g_variant_byteswap - g_variant_get_child_value - g_variant_get_normal_form - g_variant_parse + g_variant_new + g_variant_new_va + g_variant_new_boolean + g_variant_new_byte + g_variant_new_int16 + g_variant_new_uint16 + g_variant_new_int32 + g_variant_new_uint32 + g_variant_new_int64 + g_variant_new_uint64 + g_variant_new_handle + g_variant_new_double + g_variant_new_string + g_variant_new_take_string + g_variant_new_printf + g_variant_new_signature + g_variant_new_object_path + g_variant_new_variant + g_variant_new_objv + g_variant_new_strv + g_variant_new_bytestring + g_variant_new_bytestring_array + g_variant_new_maybe + g_variant_new_array + g_variant_new_tuple + g_variant_new_dict_entry + g_variant_new_fixed_array + g_variant_new_from_data + g_variant_new_from_bytes + g_variant_builder_end + g_variant_new_parsed_va + g_variant_new_parsed + g_variant_byteswap + g_variant_get_child_value + g_variant_get_normal_form + g_variant_parse g_variant_ref g_variant_take_ref g_variant_ref_sink g_variant_unref - g_variant_iter_new + g_variant_iter_new g_variant_iter_free - g_variant_type_new - g_variant_type_copy - g_variant_type_new_array - g_variant_type_new_dict_entry - g_variant_type_new_maybe - g_variant_type_new_tuple + g_variant_type_new + g_variant_type_copy + g_variant_type_new_array + g_variant_type_new_dict_entry + g_variant_type_new_maybe + g_variant_type_new_tuple g_variant_type_free - g_allocator_new + g_allocator_new g_allocator_free - g_bookmark_file_new + g_bookmark_file_new g_bookmark_file_free - g_srv_target_new + g_srv_target_new g_srv_target_free - g_string_chunk_new + g_string_chunk_new g_string_chunk_free - g_test_log_buffer_new + g_test_log_buffer_new g_test_log_buffer_free - g_value_array_new + g_value_array_new g_value_array_free - g_cache_new + g_cache_new g_cache_destroy - g_cclosure_new - g_cclosure_new_swap - g_cclosure_new_object - g_cclosure_new_object_swap - g_closure_new_object - g_closure_new_simple + g_cclosure_new + g_cclosure_new_swap + g_cclosure_new_object + g_cclosure_new_object_swap + g_closure_new_object + g_closure_new_simple g_closure_ref g_closure_unref - g_array_new - g_array_sized_new + g_array_new + g_array_sized_new g_array_ref g_array_free g_array_unref - g_async_queue_new - g_async_queue_new_full + g_async_queue_new + g_async_queue_new_full g_async_queue_ref g_async_queue_unref - g_byte_array_new - g_byte_array_sized_new - g_byte_array_new_take - g_byte_array_sized_new - g_bytes_unref_to_array + g_byte_array_new + g_byte_array_sized_new + g_byte_array_new_take + g_byte_array_sized_new + g_bytes_unref_to_array g_byte_array_ref g_byte_array_free g_byte_array_unref - g_checksum_new - g_checksum_copy + g_checksum_new + g_checksum_copy g_checksum_free - g_main_loop_new - g_main_new + g_main_loop_new + g_main_new g_main_loop_ref g_main_loop_unref g_main_destroy - g_main_context_new + g_main_context_new g_main_context_ref g_main_context_unref g_main_destroy - g_thread_pool_new + g_thread_pool_new g_thread_pool_free - g_error_copy - g_error_new_valist - g_error_new_literal - g_error_new + g_error_copy + g_error_new_valist + g_error_new_literal + g_error_new g_error_free - g_string_new - g_string_new_len - g_string_sized_new - g_variant_print_string + g_string_new + g_string_new_len + g_string_sized_new + g_variant_print_string g_string_free - g_ptr_array_new - g_ptr_array_new_full - g_ptr_array_new_with_free_func + g_ptr_array_new + g_ptr_array_new_full + g_ptr_array_new_with_free_func g_ptr_array_ref g_ptr_array_free g_ptr_array_unref - g_pattern_spec_new + g_pattern_spec_new g_pattern_spec_free - g_key_file_new + g_key_file_new g_key_file_ref g_key_file_free g_key_file_unref - g_io_module_scope_new + g_io_module_scope_new g_io_module_scope_free - g_ascii_strdown - g_ascii_strup - g_base64_decode - g_base64_encode - g_bookmark_file_get_description - g_bookmark_file_get_mime_type - g_bookmark_file_get_title - g_bookmark_file_to_data - g_build_filename - g_build_filenamev - g_build_path - g_build_pathv - g_bytes_unref_to_data - g_compute_checksum_for_bytes - g_compute_checksum_for_data - g_compute_checksum_for_string - g_compute_hmac_for_data - g_compute_hmac_for_string - g_convert - g_convert_with_fallback - g_convert_with_iconv - g_credentials_to_string - g_date_time_format - g_filename_display_basename - g_filename_display_name - g_filename_from_uri - g_filename_to_uri - g_get_codeset - g_get_current_dir - g_get_locale_variants - g_key_file_get_start_group - g_key_file_to_data - g_malloc - g_realloc - g_malloc0 - g_malloc0_n - g_malloc_n - g_realloc_n - g_memdup - g_path_get_basename - g_path_get_dirname - g_slice_alloc - g_slice_alloc0 - g_slice_copy - g_strcompress - g_strconcat - g_strdup - g_strdup_printf - g_strdup_vprintf - g_strescape - g_strjoin - g_strjoinv - g_strndup - g_strnfill - g_time_val_to_iso8601 - g_try_malloc - g_try_realloc - g_try_malloc0 - g_try_malloc0_n - g_try_malloc_n - g_try_realloc_n - g_ucs4_to_utf16 - g_ucs4_to_utf8 - g_unicode_canonical_decomposition - g_utf16_to_ucs4 - g_utf16_to_utf8 - g_utf8_casefold - g_utf8_collate_key - g_utf8_collate_key_for_filename - g_utf8_normalize - g_utf8_strdown - g_utf8_strreverse - g_utf8_strup - g_utf8_substring - g_utf8_to_ucs4 - g_utf8_to_ucs4_fast - g_utf8_to_ucs4_fast - g_utf8_to_utf16 - g_key_file_get_locale_string - g_key_file_get_value - g_key_file_get_string - g_key_file_get_boolean_list - g_key_file_get_integer_list - g_key_file_get_double_list - g_key_file_get_comment - g_dbus_proxy_get_name_owner - g_file_info_get_attribute_as_string - g_file_attribute_matcher_to_string - g_app_launch_context_get_environment - g_app_launch_context_get_startup_notify_id - g_filename_completer_get_completion_suffix - g_inet_address_mask_to_string - g_variant_dup_string - g_variant_dup_bytestring - g_variant_get_objv - g_variant_get_strv - g_variant_print - g_datalist_id_dup_data - g_dir_make_tmp - g_filename_from_utf8 - g_filename_to_utf8 - g_file_read_link - g_find_program_in_path - g_format_size - g_format_size_for_display - g_format_size_full - g_hostname_to_ascii - g_hostname_to_unicode - g_locale_from_utf8 - g_locale_to_utf8 - g_markup_escape_text - g_markup_printf_escaped - g_markup_vprintf_escaped - g_match_info_expand_references - g_match_info_fetch - g_match_info_fetch_named - g_option_context_get_help - g_regex_escape_nul - g_regex_escape_string - g_regex_replace - g_regex_replace_eval - g_regex_replace_literal - g_shell_quote - g_shell_unquote - g_uri_escape_string - g_uri_parse_scheme - g_uri_unescape_segment - g_uri_unescape_string - g_variant_type_dup_string - g_value_dup_string + g_ascii_strdown + g_ascii_strup + g_base64_decode + g_base64_encode + g_bookmark_file_get_description + g_bookmark_file_get_mime_type + g_bookmark_file_get_title + g_bookmark_file_to_data + g_build_filename + g_build_filenamev + g_build_path + g_build_pathv + g_bytes_unref_to_data + g_compute_checksum_for_bytes + g_compute_checksum_for_data + g_compute_checksum_for_string + g_compute_hmac_for_data + g_compute_hmac_for_string + g_convert + g_convert_with_fallback + g_convert_with_iconv + g_credentials_to_string + g_date_time_format + g_filename_display_basename + g_filename_display_name + g_filename_from_uri + g_filename_to_uri + g_get_codeset + g_get_current_dir + g_get_locale_variants + g_key_file_get_start_group + g_key_file_to_data + g_malloc + g_realloc + g_malloc0 + g_malloc0_n + g_malloc_n + g_realloc_n + g_memdup + g_path_get_basename + g_path_get_dirname + g_slice_alloc + g_slice_alloc0 + g_slice_copy + g_strcompress + g_strconcat + g_strdup + g_strdup_printf + g_strdup_vprintf + g_strescape + g_strjoin + g_strjoinv + g_strndup + g_strnfill + g_time_val_to_iso8601 + g_try_malloc + g_try_realloc + g_try_malloc0 + g_try_malloc0_n + g_try_malloc_n + g_try_realloc_n + g_ucs4_to_utf16 + g_ucs4_to_utf8 + g_unicode_canonical_decomposition + g_utf16_to_ucs4 + g_utf16_to_utf8 + g_utf8_casefold + g_utf8_collate_key + g_utf8_collate_key_for_filename + g_utf8_normalize + g_utf8_strdown + g_utf8_strreverse + g_utf8_strup + g_utf8_substring + g_utf8_to_ucs4 + g_utf8_to_ucs4_fast + g_utf8_to_ucs4_fast + g_utf8_to_utf16 + g_key_file_get_locale_string + g_key_file_get_value + g_key_file_get_string + g_key_file_get_boolean_list + g_key_file_get_integer_list + g_key_file_get_double_list + g_key_file_get_comment + g_dbus_proxy_get_name_owner + g_file_info_get_attribute_as_string + g_file_attribute_matcher_to_string + g_app_launch_context_get_environment + g_app_launch_context_get_startup_notify_id + g_filename_completer_get_completion_suffix + g_inet_address_mask_to_string + g_variant_dup_string + g_variant_dup_bytestring + g_variant_get_objv + g_variant_get_strv + g_variant_print + g_datalist_id_dup_data + g_dir_make_tmp + g_filename_from_utf8 + g_filename_to_utf8 + g_file_read_link + g_find_program_in_path + g_format_size + g_format_size_for_display + g_format_size_full + g_hostname_to_ascii + g_hostname_to_unicode + g_locale_from_utf8 + g_locale_to_utf8 + g_markup_escape_text + g_markup_printf_escaped + g_markup_vprintf_escaped + g_match_info_expand_references + g_match_info_fetch + g_match_info_fetch_named + g_option_context_get_help + g_regex_escape_nul + g_regex_escape_string + g_regex_replace + g_regex_replace_eval + g_regex_replace_literal + g_shell_quote + g_shell_unquote + g_uri_escape_string + g_uri_parse_scheme + g_uri_unescape_segment + g_uri_unescape_string + g_variant_type_dup_string + g_value_dup_string g_register_data g_free - g_hash_table_new_full - g_hash_table_new + g_hash_table_new_full + g_hash_table_new g_hash_table_ref g_hash_table_destroy g_hash_table_unref - g_io_channel_unix_new - g_io_channel_win32_new_fd - g_io_channel_win32_new_socket - g_io_channel_win32_new_messages - g_io_channel_new_file + g_io_channel_unix_new + g_io_channel_win32_new_fd + g_io_channel_win32_new_socket + g_io_channel_win32_new_messages + g_io_channel_new_file g_io_channel_ref g_io_channel_close g_io_channel_shutdown g_io_channel_unref - g_emblemed_icon_get_emblems - g_list_alloc - g_list_copy - g_list_copy_deep - g_app_info_get_all - g_app_info_get_all_for_type - g_app_info_get_fallback_for_type - g_app_info_get_recommended_for_type - g_io_modules_load_all_in_directory - g_io_modules_load_all_in_directory_with_scope - g_hash_table_get_keys - g_hash_table_get_values + g_emblemed_icon_get_emblems + g_list_alloc + g_list_copy + g_list_copy_deep + g_app_info_get_all + g_app_info_get_all_for_type + g_app_info_get_fallback_for_type + g_app_info_get_recommended_for_type + g_io_modules_load_all_in_directory + g_io_modules_load_all_in_directory_with_scope + g_hash_table_get_keys + g_hash_table_get_values g_list_free g_list_free_1 g_list_free_full - g_regex_new + g_regex_new g_regex_ref g_regex_unref - g_node_new - g_node_copy - g_node_copy_deep + g_node_new + g_node_copy + g_node_copy_deep g_node_destroy - g_time_zone_new - g_time_zone_new_local - g_time_zone_new_utc + g_time_zone_new + g_time_zone_new_local + g_time_zone_new_utc g_time_zone_ref g_time_zone_unref - g_markup_parse_context_new + g_markup_parse_context_new g_markup_parse_context_free - g_mapped_file_new - g_mapped_file_new_from_fd + g_mapped_file_new + g_mapped_file_new_from_fd g_mapped_file_ref g_mapped_file_free g_mapped_file_unref - g_mutex_new + g_mutex_new g_mutex_free - g_mem_chunk_new + g_mem_chunk_new g_mem_chunk_free - g_option_group_new + g_option_group_new g_option_group_free - g_option_context_new + g_option_context_new g_option_context_free - g_rand_new - g_rand_copy - g_rand_new_with_seed - g_rand_new_with_seed_array + g_rand_new + g_rand_copy + g_rand_new_with_seed + g_rand_new_with_seed_array g_rand_free - g_queue_new - g_queue_copy + g_queue_new + g_queue_copy g_queue_free - g_slice_new + g_slice_new g_slice_free g_slice_free1 - g_sequence_new + g_sequence_new g_sequence_free - g_completion_new + g_completion_new g_completion_free - g_chunk_new + g_chunk_new g_chunk_free - g_bytes_new - g_bytes_new_take - g_bytes_new_static - g_bytes_new_with_free_func - g_bytes_new_from_bytes - g_byte_array_free_to_bytes - g_memory_output_stream_steal_as_bytes - g_variant_get_data_as_bytes - g_mapped_file_get_bytes + g_bytes_new + g_bytes_new_take + g_bytes_new_static + g_bytes_new_with_free_func + g_bytes_new_from_bytes + g_byte_array_free_to_bytes + g_memory_output_stream_steal_as_bytes + g_variant_get_data_as_bytes + g_mapped_file_get_bytes g_bytes_ref g_bytes_unref - g_bookmark_file_get_uris - g_bookmark_file_get_groups - g_bookmark_file_get_applications - g_key_file_get_groups - g_key_file_get_keys - g_strdupv - g_strsplit - g_strsplit_set - g_uri_list_extract_uris - g_key_file_get_string_list - g_key_file_get_locale_string_list - g_file_info_list_attributes - g_file_info_get_attribute_stringv - g_app_launch_context_get_environment - g_filename_completer_get_completions - g_io_module_query - g_variant_dup_objv - g_variant_dup_bytestring_array - g_environ_setenv - g_environ_unsetenv - g_get_environ - g_listenv - g_match_info_fetch_all - g_regex_split - g_regex_split_full - g_regex_split_simple - g_regex_split_simple - g_variant_dup_strv + g_bookmark_file_get_uris + g_bookmark_file_get_groups + g_bookmark_file_get_applications + g_key_file_get_groups + g_key_file_get_keys + g_strdupv + g_strsplit + g_strsplit_set + g_uri_list_extract_uris + g_key_file_get_string_list + g_key_file_get_locale_string_list + g_file_info_list_attributes + g_file_info_get_attribute_stringv + g_app_launch_context_get_environment + g_filename_completer_get_completions + g_io_module_query + g_variant_dup_objv + g_variant_dup_bytestring_array + g_environ_setenv + g_environ_unsetenv + g_get_environ + g_listenv + g_match_info_fetch_all + g_regex_split + g_regex_split_full + g_regex_split_simple + g_regex_split_simple + g_variant_dup_strv g_strfreev - g_hmac_new - g_hmac_copy + g_hmac_new + g_hmac_copy g_hmac_ref g_hmac_unref - g_hook_alloc + g_hook_alloc g_hook_ref g_hook_unref g_hook_destroy g_hook_free - g_date_new - g_date_new_dmy - g_date_new_julian + g_date_new + g_date_new_dmy + g_date_new_julian g_date_free - g_variant_builder_new + g_variant_builder_new g_variant_builder_ref g_variant_builder_unref - g_cond_new + g_cond_new g_cond_free - g_app_launch_context_new - g_app_info_create_from_commandline - g_app_info_dup - g_app_info_get_default_for_type - g_app_info_get_default_for_uri_scheme - g_application_new - g_application_get_dbus_connection - g_application_get_default - g_buffered_input_stream_new - g_buffered_output_stream_new - g_cancellable_new - g_charset_converter_new - g_converter_input_stream_new - g_converter_output_stream_new - g_credentials_new - g_data_input_stream_new - g_data_output_stream_new - g_dbus_auth_observer_new - g_dbus_connection_new_finish - g_dbus_connection_new_sync - g_dbus_connection_new_for_address_finish - g_dbus_connection_new_for_address_sync - g_dbus_message_new - g_dbus_message_new_signal - g_dbus_message_new_method_call - g_dbus_message_new_method_reply - g_dbus_message_new_method_error - g_dbus_message_new_method_error_valist - g_dbus_message_new_method_error_literal - g_dbus_object_manager_client_new_finish - g_dbus_object_manager_client_new_sync - g_dbus_object_manager_client_new_for_bus_finish - g_dbus_object_manager_client_new_for_bus_sync - g_dbus_object_manager_server_new - g_dbus_object_manager_server_get_connection - g_dbus_object_proxy_new - g_dbus_object_skeleton_new - g_dbus_proxy_new_finish - g_dbus_proxy_new_sync - g_dbus_proxy_new_for_bus_finish - g_dbus_proxy_new_for_bus_sync - g_emblemed_icon_new - g_emblem_new - g_emblem_new_with_origin - g_file_icon_new - g_file_icon_get_file - g_file_info_new - g_file_info_dup - g_file_info_get_icon - g_file_info_get_symbolic_icon - g_file_info_get_attribute_object - g_file_info_get_deletion_date - g_filename_completer_new - g_inet_address_mask_new - g_inet_address_mask_new_from_string - g_inet_address_mask_get_address - g_inet_socket_address_new - g_inet_socket_address_get_address - g_initable_new - g_initable_new_valist - g_initable_newv - g_io_module_new - g_io_module_scope_new - g_keyfile_settings_backend_new - g_memory_input_stream_new - g_memory_input_stream_new_from_data - g_memory_input_stream_new_from_bytes - g_memory_output_stream_new - g_memory_output_stream_new_resizable - g_memory_settings_backend_new - g_null_settings_backend_new - g_menu_item_new - g_menu_item_new_section - g_menu_item_new_submenu - g_menu_item_new_from_model - g_menu_new - g_mount_operation_new - g_network_address_new - g_network_service_new - g_object_new - g_param_spec_pool_new - g_pollable_source_new - g_private_new - g_proxy_address_new - g_ptr_array_sized_new - g_relation_new - g_scanner_new - g_settings_new - g_signal_type_cclosure_new - g_simple_action_group_new - g_simple_action_new - g_simple_async_result_new - g_simple_permission_new - g_socket_client_new - g_socket_listener_new - g_socket_new - g_socket_service_new - g_tcp_wrapper_connection_new - g_test_dbus_new - g_themed_icon_new - g_threaded_socket_service_new - g_tls_client_connection_new - g_tls_file_database_new - g_tls_password_new - g_tls_server_connection_new - g_unix_signal_source_new - g_zlib_compressor_new - g_zlib_decompressor_new + g_app_launch_context_new + g_app_info_create_from_commandline + g_app_info_dup + g_app_info_get_default_for_type + g_app_info_get_default_for_uri_scheme + g_application_new + g_application_get_dbus_connection + g_application_get_default + g_buffered_input_stream_new + g_buffered_output_stream_new + g_cancellable_new + g_charset_converter_new + g_converter_input_stream_new + g_converter_output_stream_new + g_credentials_new + g_data_input_stream_new + g_data_output_stream_new + g_dbus_auth_observer_new + g_dbus_connection_new_finish + g_dbus_connection_new_sync + g_dbus_connection_new_for_address_finish + g_dbus_connection_new_for_address_sync + g_dbus_message_new + g_dbus_message_new_signal + g_dbus_message_new_method_call + g_dbus_message_new_method_reply + g_dbus_message_new_method_error + g_dbus_message_new_method_error_valist + g_dbus_message_new_method_error_literal + g_dbus_object_manager_client_new_finish + g_dbus_object_manager_client_new_sync + g_dbus_object_manager_client_new_for_bus_finish + g_dbus_object_manager_client_new_for_bus_sync + g_dbus_object_manager_server_new + g_dbus_object_manager_server_get_connection + g_dbus_object_proxy_new + g_dbus_object_skeleton_new + g_dbus_proxy_new_finish + g_dbus_proxy_new_sync + g_dbus_proxy_new_for_bus_finish + g_dbus_proxy_new_for_bus_sync + g_emblemed_icon_new + g_emblem_new + g_emblem_new_with_origin + g_file_icon_new + g_file_icon_get_file + g_file_info_new + g_file_info_dup + g_file_info_get_icon + g_file_info_get_symbolic_icon + g_file_info_get_attribute_object + g_file_info_get_deletion_date + g_filename_completer_new + g_inet_address_mask_new + g_inet_address_mask_new_from_string + g_inet_address_mask_get_address + g_inet_socket_address_new + g_inet_socket_address_get_address + g_initable_new + g_initable_new_valist + g_initable_newv + g_io_module_new + g_io_module_scope_new + g_keyfile_settings_backend_new + g_memory_input_stream_new + g_memory_input_stream_new_from_data + g_memory_input_stream_new_from_bytes + g_memory_output_stream_new + g_memory_output_stream_new_resizable + g_memory_settings_backend_new + g_null_settings_backend_new + g_menu_item_new + g_menu_item_new_section + g_menu_item_new_submenu + g_menu_item_new_from_model + g_menu_new + g_mount_operation_new + g_network_address_new + g_network_service_new + g_object_new + g_param_spec_pool_new + g_pollable_source_new + g_private_new + g_proxy_address_new + g_ptr_array_sized_new + g_relation_new + g_scanner_new + g_settings_new + g_signal_type_cclosure_new + g_simple_action_group_new + g_simple_action_new + g_simple_async_result_new + g_simple_permission_new + g_socket_client_new + g_socket_listener_new + g_socket_new + g_socket_service_new + g_tcp_wrapper_connection_new + g_test_dbus_new + g_themed_icon_new + g_threaded_socket_service_new + g_tls_client_connection_new + g_tls_file_database_new + g_tls_password_new + g_tls_server_connection_new + g_unix_signal_source_new + g_zlib_compressor_new + g_zlib_decompressor_new g_object_ref g_object_unref gtk_widget_destroy - g_tree_new - g_tree_new_full - g_tree_new_with_data + g_tree_new + g_tree_new_full + g_tree_new_with_data g_tree_ref g_tree_unref - g_file_attribute_matcher_new - g_file_attribute_matcher_subtract + g_file_attribute_matcher_new + g_file_attribute_matcher_subtract g_file_attribute_matcher_ref g_file_attribute_matcher_unref diff --git a/lib/library.cpp b/lib/library.cpp index d926eaddfc2..7134733d9d8 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -366,6 +366,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) AllocFunc temp; temp.groupId = allocationId; + temp.noFail = memorynode->BoolAttribute("no-fail", false); temp.initData = memorynode->BoolAttribute("init", true); temp.arg = memorynode->IntAttribute("arg", -1); diff --git a/lib/library.h b/lib/library.h index c13959bc8da..d8dbbfe45a5 100644 --- a/lib/library.h +++ b/lib/library.h @@ -88,6 +88,7 @@ class CPPCHECKLIB Library { int bufferSizeArg2{}; int reallocArg{}; bool initData{}; + bool noFail{}; }; /** get allocation info for function */ diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index b34392d95d4..554a6ef853e 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -6975,7 +6975,9 @@ static void valueFlowUnknownFunctionReturn(TokenList& tokenlist, const Settings& continue; if (const auto* f = settings.library.getAllocFuncInfo(tok->astOperand1())) { - if (settings.library.returnValueType(tok->astOperand1()).find('*') != std::string::npos) { + if (f->noFail) { + // Allocation function that cannot fail + } else if (settings.library.returnValueType(tok->astOperand1()).find('*') != std::string::npos) { // Allocation function that returns a pointer ValueFlow::Value value(0); value.setPossible(); diff --git a/test/cfg/gtk.c b/test/cfg/gtk.c index 623805a002b..cd5123d2c41 100644 --- a/test/cfg/gtk.c +++ b/test/cfg/gtk.c @@ -46,12 +46,10 @@ void validCode(int argInt, GHashTableIter * hash_table_iter, GHashTable * hash_t g_printerr("err"); GString * pGStr1 = g_string_new("test"); - // cppcheck-suppress nullPointerOutOfMemory g_string_append(pGStr1, "a"); g_string_free(pGStr1, TRUE); gchar * pGchar1 = g_strconcat("a", "b", NULL); - // cppcheck-suppress nullPointerOutOfMemory printf("%s", pGchar1); g_free(pGchar1); From 8aec324679b61ec8cc0aec6904777836b6444b0f Mon Sep 17 00:00:00 2001 From: Colomban Wendling Date: Sat, 22 Mar 2025 00:53:25 +0100 Subject: [PATCH 2/2] Add support for no-fail library allocator attribute to the GUI --- gui/cppchecklibrarydata.cpp | 3 +++ gui/cppchecklibrarydata.h | 1 + .../files/memory_resource_valid.cfg | 1 + .../testcppchecklibrarydata.cpp | 16 +++++++++++++++- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/gui/cppchecklibrarydata.cpp b/gui/cppchecklibrarydata.cpp index 38c33437290..09c4ef7b689 100644 --- a/gui/cppchecklibrarydata.cpp +++ b/gui/cppchecklibrarydata.cpp @@ -272,6 +272,7 @@ static CppcheckLibraryData::MemoryResource loadMemoryResource(QXmlStreamReader & if (elementName == "alloc" || elementName == "realloc") { CppcheckLibraryData::MemoryResource::Alloc alloc; alloc.isRealloc = (elementName == "realloc"); + alloc.noFail = (xmlReader.attributes().value("no-fail").toString() == "true"); alloc.init = (xmlReader.attributes().value("init").toString() == "true"); if (xmlReader.attributes().hasAttribute("arg")) { alloc.arg = xmlReader.attributes().value("arg").toInt(); @@ -723,6 +724,8 @@ static void writeMemoryResource(QXmlStreamWriter &xmlWriter, const CppcheckLibra xmlWriter.writeStartElement("alloc"); } xmlWriter.writeAttribute("init", bool_to_string(alloc.init)); + if (alloc.noFail) + xmlWriter.writeAttribute("no-fail", bool_to_string(alloc.noFail)); if (alloc.arg != -1) { xmlWriter.writeAttribute("arg", QString("%1").arg(alloc.arg)); } diff --git a/gui/cppchecklibrarydata.h b/gui/cppchecklibrarydata.h index 739a4455274..1ec993314a2 100644 --- a/gui/cppchecklibrarydata.h +++ b/gui/cppchecklibrarydata.h @@ -142,6 +142,7 @@ class CppcheckLibraryData { struct Alloc { bool isRealloc{}; bool init{}; + bool noFail{}; int arg = -1; // -1: Has no optional "realloc-arg" attribute int reallocArg = -1; // -1: Has no optional "arg" attribute QString bufferSize; diff --git a/gui/test/cppchecklibrarydata/files/memory_resource_valid.cfg b/gui/test/cppchecklibrarydata/files/memory_resource_valid.cfg index fe0e9329096..3f59249c9c1 100644 --- a/gui/test/cppchecklibrarydata/files/memory_resource_valid.cfg +++ b/gui/test/cppchecklibrarydata/files/memory_resource_valid.cfg @@ -6,6 +6,7 @@ realloc UuidToString HeapFree + g_malloc diff --git a/gui/test/cppchecklibrarydata/testcppchecklibrarydata.cpp b/gui/test/cppchecklibrarydata/testcppchecklibrarydata.cpp index d822c7ce98e..203bd044882 100644 --- a/gui/test/cppchecklibrarydata/testcppchecklibrarydata.cpp +++ b/gui/test/cppchecklibrarydata/testcppchecklibrarydata.cpp @@ -284,7 +284,7 @@ void TestCppcheckLibraryData::memoryResourceValid() // Do size and content checks against swapped data. QCOMPARE(libraryData.memoryresource.size(), 2); QCOMPARE(libraryData.memoryresource[0].type, QString("memory")); - QCOMPARE(libraryData.memoryresource[0].alloc.size(), 4); + QCOMPARE(libraryData.memoryresource[0].alloc.size(), 5); QCOMPARE(libraryData.memoryresource[0].dealloc.size(), 1); QCOMPARE(libraryData.memoryresource[0].use.size(), 0); @@ -292,6 +292,7 @@ void TestCppcheckLibraryData::memoryResourceValid() QCOMPARE(libraryData.memoryresource[0].alloc[0].bufferSize, QString("malloc")); QCOMPARE(libraryData.memoryresource[0].alloc[0].isRealloc, false); QCOMPARE(libraryData.memoryresource[0].alloc[0].init, false); + QCOMPARE(libraryData.memoryresource[0].alloc[0].noFail, false); QCOMPARE(libraryData.memoryresource[0].alloc[0].arg, -1); QCOMPARE(libraryData.memoryresource[0].alloc[0].reallocArg, -1); @@ -299,6 +300,7 @@ void TestCppcheckLibraryData::memoryResourceValid() QCOMPARE(libraryData.memoryresource[0].alloc[1].bufferSize, QString("calloc")); QCOMPARE(libraryData.memoryresource[0].alloc[1].isRealloc, false); QCOMPARE(libraryData.memoryresource[0].alloc[1].init, true); + QCOMPARE(libraryData.memoryresource[0].alloc[1].noFail, false); QCOMPARE(libraryData.memoryresource[0].alloc[1].arg, -1); QCOMPARE(libraryData.memoryresource[0].alloc[1].reallocArg, -1); @@ -306,6 +308,7 @@ void TestCppcheckLibraryData::memoryResourceValid() QCOMPARE(libraryData.memoryresource[0].alloc[2].bufferSize, QString("malloc:2")); QCOMPARE(libraryData.memoryresource[0].alloc[2].isRealloc, true); QCOMPARE(libraryData.memoryresource[0].alloc[2].init, false); + QCOMPARE(libraryData.memoryresource[0].alloc[2].noFail, false); QCOMPARE(libraryData.memoryresource[0].alloc[2].arg, -1); QCOMPARE(libraryData.memoryresource[0].alloc[2].reallocArg, -1); @@ -313,9 +316,18 @@ void TestCppcheckLibraryData::memoryResourceValid() QCOMPARE(libraryData.memoryresource[0].alloc[3].bufferSize.isEmpty(), true); QCOMPARE(libraryData.memoryresource[0].alloc[3].isRealloc, false); QCOMPARE(libraryData.memoryresource[0].alloc[3].init, false); + QCOMPARE(libraryData.memoryresource[0].alloc[3].noFail, false); QCOMPARE(libraryData.memoryresource[0].alloc[3].arg, 2); QCOMPARE(libraryData.memoryresource[0].alloc[3].reallocArg, -1); + QCOMPARE(libraryData.memoryresource[0].alloc[4].name, QString("g_malloc")); + QCOMPARE(libraryData.memoryresource[0].alloc[4].bufferSize, QString("malloc")); + QCOMPARE(libraryData.memoryresource[0].alloc[4].isRealloc, false); + QCOMPARE(libraryData.memoryresource[0].alloc[4].init, false); + QCOMPARE(libraryData.memoryresource[0].alloc[4].noFail, true); + QCOMPARE(libraryData.memoryresource[0].alloc[4].arg, -1); + QCOMPARE(libraryData.memoryresource[0].alloc[4].reallocArg, -1); + QCOMPARE(libraryData.memoryresource[0].dealloc[0].name, QString("HeapFree")); QCOMPARE(libraryData.memoryresource[0].dealloc[0].arg, 3); @@ -328,6 +340,7 @@ void TestCppcheckLibraryData::memoryResourceValid() QCOMPARE(libraryData.memoryresource[1].alloc[0].bufferSize.isEmpty(), true); QCOMPARE(libraryData.memoryresource[1].alloc[0].isRealloc, false); QCOMPARE(libraryData.memoryresource[1].alloc[0].init, true); + QCOMPARE(libraryData.memoryresource[1].alloc[0].noFail, false); QCOMPARE(libraryData.memoryresource[1].alloc[0].arg, 1); QCOMPARE(libraryData.memoryresource[1].alloc[0].reallocArg, -1); @@ -362,6 +375,7 @@ void TestCppcheckLibraryData::memoryResourceValid() QCOMPARE(lhs.alloc[num].bufferSize, rhs.alloc[num].bufferSize); QCOMPARE(lhs.alloc[num].isRealloc, rhs.alloc[num].isRealloc); QCOMPARE(lhs.alloc[num].init, rhs.alloc[num].init); + QCOMPARE(lhs.alloc[num].noFail, rhs.alloc[num].noFail); QCOMPARE(lhs.alloc[num].arg, rhs.alloc[num].arg); QCOMPARE(lhs.alloc[num].reallocArg, rhs.alloc[num].reallocArg); }