From 04be8d4fffb69e17b23fb09feb1d7f2d8f6f62de Mon Sep 17 00:00:00 2001 From: "roman.khlopkov" <roman.khlopkov@demlabs.net> Date: Wed, 24 Jul 2024 23:17:20 +0300 Subject: [PATCH] [*] UB fix + useful macros --- core/include/dap_common.h | 18 +++++++++++++++++- crypto/src/dap_cert.c | 2 +- global-db/dap_global_db_driver_mdbx.c | 2 +- io/dap_events_socket.c | 2 +- .../http_server/http_client/dap_http_client.c | 2 +- .../rpc_core/src/dap_json_rpc_notification.c | 4 ++-- .../src/dap_json_rpc_request_handler.c | 4 ++-- net/server/notify_server/src/dap_notify_srv.c | 2 +- net/stream/session/dap_stream_session.c | 4 ++-- net/stream/stream/dap_stream.c | 2 +- plugin/src/dap_plugin_manifest.c | 4 ++-- 11 files changed, 31 insertions(+), 15 deletions(-) diff --git a/core/include/dap_common.h b/core/include/dap_common.h index dff8c06c7..399c76fe0 100755 --- a/core/include/dap_common.h +++ b/core/include/dap_common.h @@ -116,6 +116,22 @@ #define UNUSED_ARG __attribute__((__unused__)) +#ifndef likely +# if (defined(__GNUC__) || __has_builtin(__builtin_expect)) && !defined(__COVERITY__) +# define likely(cond) __builtin_expect(!!(cond), 1) +# else +# define likely(x) (!!(x)) +# endif +#endif /* likely */ + +#ifndef unlikely +# if (defined(__GNUC__) || __has_builtin(__builtin_expect)) && !defined(__COVERITY__) +# define unlikely(cond) __builtin_expect(!!(cond), 0) +# else +# define unlikely(x) (!!(x)) +# endif +#endif /* unlikely */ + #ifndef ROUNDUP #define ROUNDUP(n,width) (((n) + (width) - 1) & ~(unsigned)((width) - 1)) #endif @@ -932,4 +948,4 @@ DAP_STATIC_INLINE int dap_stream_node_addr_from_str(dap_stream_node_addr_t *a_ad DAP_STATIC_INLINE bool dap_stream_node_addr_is_blank(dap_stream_node_addr_t *a_addr) { return !a_addr->uint64; } -const char *dap_stream_node_addr_to_str_static(dap_stream_node_addr_t a_address); \ No newline at end of file +const char *dap_stream_node_addr_to_str_static(dap_stream_node_addr_t a_address); diff --git a/crypto/src/dap_cert.c b/crypto/src/dap_cert.c index 99bd8f1a4..b16588a25 100755 --- a/crypto/src/dap_cert.c +++ b/crypto/src/dap_cert.c @@ -390,7 +390,7 @@ int dap_cert_add(dap_cert_t *a_cert) { if (!a_cert) return -2; - dap_cert_item_t *l_cert_item; + dap_cert_item_t *l_cert_item = NULL; HASH_FIND_STR(s_certs, a_cert->name, l_cert_item); if (l_cert_item) { log_it(L_WARNING, "Certificate with name %s already present in memory", a_cert->name); diff --git a/global-db/dap_global_db_driver_mdbx.c b/global-db/dap_global_db_driver_mdbx.c index 27e8ddbd9..ce97afc29 100644 --- a/global-db/dap_global_db_driver_mdbx.c +++ b/global-db/dap_global_db_driver_mdbx.c @@ -213,7 +213,7 @@ char l_buf[1024] = {0}; static dap_db_ctx_t *s_cre_db_ctx_for_group(const char *a_group, int a_flags, MDBX_txn *a_txn) { int rc; -dap_db_ctx_t *l_db_ctx; +dap_db_ctx_t *l_db_ctx = NULL; size_t l_name_len; MDBX_val l_key_iov, l_data_iov; diff --git a/io/dap_events_socket.c b/io/dap_events_socket.c index e66c5e3fb..68de37227 100644 --- a/io/dap_events_socket.c +++ b/io/dap_events_socket.c @@ -263,7 +263,7 @@ static inline void s_dap_evsock_free(dap_events_socket_t *a_es) { #ifdef DAP_SYS_DEBUG pthread_rwlock_wrlock(&s_evsocks_lock); - dap_events_socket_t *l_es; + dap_events_socket_t *l_es = NULL; HASH_FIND(hh2, s_esockets, &a_es->uuid, sizeof(l_es->uuid), l_es); if (l_es) HASH_DELETE(hh2, s_esockets, l_es); /* Remove record from the table */ diff --git a/net/server/http_server/http_client/dap_http_client.c b/net/server/http_server/http_client/dap_http_client.c index 0b9df07c9..0d0c89bdb 100644 --- a/net/server/http_server/http_client/dap_http_client.c +++ b/net/server/http_server/http_client/dap_http_client.c @@ -317,7 +317,7 @@ void dap_http_client_read( dap_events_socket_t *a_esocket, void *a_arg ) size_t read_bytes = 0; dap_http_client_t *l_http_client = DAP_HTTP_CLIENT( a_esocket ); - dap_http_url_proc_t *url_proc; + dap_http_url_proc_t *url_proc = NULL; dap_http_cache_t * l_http_cache; /* diff --git a/net/server/json_rpc/rpc_core/src/dap_json_rpc_notification.c b/net/server/json_rpc/rpc_core/src/dap_json_rpc_notification.c index 33ce73265..8b3cfc925 100644 --- a/net/server/json_rpc/rpc_core/src/dap_json_rpc_notification.c +++ b/net/server/json_rpc/rpc_core/src/dap_json_rpc_notification.c @@ -7,7 +7,7 @@ static dap_json_rpc_notification_handler_t *s_handler_notifications = NULL; int dap_json_rpc_notification_registration(const char *a_method, notification_handler_func_t *a_notification_func) { - dap_json_rpc_notification_handler_t *l_handler; + dap_json_rpc_notification_handler_t *l_handler = NULL; HASH_FIND_STR(s_handler_notifications, a_method, l_handler); if (l_handler == 0){ l_handler = DAP_NEW(dap_json_rpc_notification_handler_t); @@ -25,7 +25,7 @@ int dap_json_rpc_notification_registration(const char *a_method, notification_ha } void dap_json_rpc_notification_unregistration(const char *a_method) { - dap_json_rpc_notification_handler_t *l_handler; + dap_json_rpc_notification_handler_t *l_handler = NULL; HASH_FIND_STR(s_handler_notifications, a_method, l_handler); if (l_handler != NULL){ HASH_DEL(s_handler_notifications, l_handler); diff --git a/net/server/json_rpc/rpc_core/src/dap_json_rpc_request_handler.c b/net/server/json_rpc/rpc_core/src/dap_json_rpc_request_handler.c index 0a4b2c89f..6bedaea61 100644 --- a/net/server/json_rpc/rpc_core/src/dap_json_rpc_request_handler.c +++ b/net/server/json_rpc/rpc_core/src/dap_json_rpc_request_handler.c @@ -6,7 +6,7 @@ static dap_json_rpc_request_handler_t *s_handler_hash_table = NULL; int dap_json_rpc_registration_request_handler(const char *a_name, handler_func_t *a_func) { - dap_json_rpc_request_handler_t *l_handler; + dap_json_rpc_request_handler_t *l_handler = NULL; HASH_FIND_STR(s_handler_hash_table, a_name, l_handler); if (l_handler == NULL){ l_handler = DAP_NEW(dap_json_rpc_request_handler_t); @@ -24,7 +24,7 @@ int dap_json_rpc_registration_request_handler(const char *a_name, handler_func_t } int dap_json_rpc_unregistration_request_handler(const char *a_name) { - dap_json_rpc_request_handler_t *l_handler; + dap_json_rpc_request_handler_t *l_handler = NULL; HASH_FIND_STR(s_handler_hash_table, a_name, l_handler); if (l_handler == NULL){ return 1; diff --git a/net/server/notify_server/src/dap_notify_srv.c b/net/server/notify_server/src/dap_notify_srv.c index 24db56958..6f305a9ae 100644 --- a/net/server/notify_server/src/dap_notify_srv.c +++ b/net/server/notify_server/src/dap_notify_srv.c @@ -221,7 +221,7 @@ static void s_notify_server_callback_queue(dap_events_socket_t * a_es, void * a_ */ static void s_notify_server_callback_new(dap_events_socket_t * a_es, UNUSED_ARG void *a_arg) { - dap_events_socket_handler_hh_t * l_hh_new; + dap_events_socket_handler_hh_t *l_hh_new = NULL; pthread_rwlock_wrlock(&s_notify_server_clients_mutex); HASH_FIND(hh,s_notify_server_clients, &a_es->uuid, sizeof (a_es->uuid), l_hh_new); if (l_hh_new){ diff --git a/net/stream/session/dap_stream_session.c b/net/stream/session/dap_stream_session.c index 7a7aadda8..42b187d8c 100644 --- a/net/stream/session/dap_stream_session.c +++ b/net/stream/session/dap_stream_session.c @@ -97,7 +97,7 @@ void dap_stream_session_get_list_sessions_unlock(void) dap_stream_session_t * dap_stream_session_pure_new() { -dap_stream_session_t *l_stm_sess, *l_stm_tmp; +dap_stream_session_t *l_stm_sess, *l_stm_tmp = NULL; uint32_t session_id = 0; if ( !(l_stm_sess = DAP_NEW_Z(dap_stream_session_t)) ) /* Preallocate new session context */ @@ -159,7 +159,7 @@ dap_stream_session_t *dap_stream_session_id_mt(uint32_t a_id) */ dap_stream_session_t *dap_stream_session_id_unsafe(uint32_t id ) { - dap_stream_session_t *ret; + dap_stream_session_t *ret = NULL; HASH_FIND(hh, s_sessions, &id, sizeof(uint32_t), ret); return ret; } diff --git a/net/stream/stream/dap_stream.c b/net/stream/stream/dap_stream.c index d0231d843..72d03b551 100644 --- a/net/stream/stream/dap_stream.c +++ b/net/stream/stream/dap_stream.c @@ -1177,7 +1177,7 @@ dap_stream_info_t *dap_stream_get_links_info(dap_cluster_t *a_cluster, size_t *a log_it(L_CRITICAL, "! Attempt to aquire streams lock recursively !"); return NULL; } - dap_stream_t *it; + dap_stream_t *it = NULL; size_t l_streams_count = 0, i = 0; if (a_cluster) { pthread_rwlock_rdlock(&a_cluster->members_lock); diff --git a/plugin/src/dap_plugin_manifest.c b/plugin/src/dap_plugin_manifest.c index dc3002744..6625a17eb 100644 --- a/plugin/src/dap_plugin_manifest.c +++ b/plugin/src/dap_plugin_manifest.c @@ -268,7 +268,7 @@ dap_plugin_manifest_t* dap_plugin_manifest_all() */ dap_plugin_manifest_t *dap_plugin_manifest_find(const char *a_name) { - dap_plugin_manifest_t *l_ret; + dap_plugin_manifest_t *l_ret = NULL; HASH_FIND_STR(s_manifests,a_name,l_ret); return l_ret; } @@ -327,7 +327,7 @@ static void s_manifest_delete(dap_plugin_manifest_t *a_manifest) */ bool dap_plugins_manifest_remove(const char *a_name) { - dap_plugin_manifest_t *l_manifest; + dap_plugin_manifest_t *l_manifest = NULL; HASH_FIND_STR(s_manifests, a_name,l_manifest); if(l_manifest) HASH_DEL(s_manifests, l_manifest); -- GitLab