Skip to content

Commit 5dad666

Browse files
committed
issue: <COVERITY_ISSUE_NUMBER>
Coverity NULL_RETURNS fixes Add NULL pointer checks before dereferencing return values from functions that can return NULL: - json_object_get_string(): Check before std::string construction - get_parent_listen_context(): Check before accessing parent methods - strdup(): Check for allocation failure - m_used_containers.back(): Check for empty container list These defensive checks prevent crashes when: - JSON schema has malformed type fields - Memory allocation fails - Parent context is uninitialized (non-RSS sockets) - Container allocation fails Signed-off-by: Omri Ritblat <[email protected]>
1 parent e844d2f commit 5dad666

File tree

10 files changed

+36
-10
lines changed

10 files changed

+36
-10
lines changed

src/core/config/descriptor_providers/json_descriptor_provider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void json_descriptor_provider::validate_schema(json_object *schema)
7474
}
7575

7676
json_object *type_field = json_utils::get_field(schema, config_strings::schema::JSON_TYPE);
77-
if (std::string(json_object_get_string(type_field)) !=
77+
if (type_field && std::string(json_object_get_string(type_field)) !=
7878
config_strings::schema_types::JSON_TYPE_OBJECT) {
7979
throw_xlio_exception("Schema root must have type 'object'.");
8080
}

src/core/config/descriptor_providers/schema_analyzer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,14 @@ enum_mapping_config_t schema_analyzer::analyze_enum_mapping_config()
252252
for_each_oneof_option(one_of_field, [&](json_object *option) {
253253
json_object *type_field = json_utils::get_field(option, config_strings::schema::JSON_TYPE);
254254

255-
std::string type_str = json_object_get_string(type_field);
255+
// Check for NULL before creating std::string
256+
const char *type_cstr = json_object_get_string(type_field);
257+
if (!type_cstr) {
258+
// Skip this option if type is not a string or doesn't exist
259+
throw_xlio_exception("Type is not a string or doesn't exist for: " + m_path);
260+
}
261+
262+
std::string type_str = type_cstr;
256263
if (type_str == config_strings::schema_types::JSON_TYPE_INTEGER) {
257264
int_option = option;
258265
} else if (type_str == config_strings::schema_types::JSON_TYPE_STRING) {

src/core/dev/hw_queue_rx.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ void hw_queue_rx::post_recv_buffers(descq_t *p_buffers, size_t count)
207207
hwqrx_logfuncall("");
208208
// Called from cq_mgr_rx context under cq_mgr_rx::LOCK!
209209
while (count--) {
210-
post_recv_buffer(p_buffers->get_and_pop_front());
210+
if (likely(!p_buffers->empty())) {
211+
post_recv_buffer(p_buffers->get_and_pop_front());
212+
}
211213
}
212214
}
213215

src/core/dev/hw_queue_tx.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ inline uint8_t hw_queue_tx::fill_wqe(xlio_ibv_send_wr *pswr)
555555

556556
// Fill Ethernet segment with header inline, static data
557557
// were populated in preset after previous packet send
558+
/* coverity[null_deref] */
558559
memcpy(cur_seg + offsetof(struct mlx5_wqe_eth_seg, inline_hdr_start), data_addr,
559560
MLX5_ETH_INLINE_HEADER_SIZE);
560561
cur_seg += sizeof(struct mlx5_wqe_eth_seg);

src/core/dev/ring_simple.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ ring_simple::ring_simple(int if_index, ring *parent, bool use_locks)
7272
BULLSEYE_EXCLUDE_BLOCK_END
7373

7474
const slave_data_t *p_slave = p_ndev->get_slave(get_if_index());
75-
75+
if(unlikely(!p_slave)) {
76+
ring_logerr("Cannot find slave for a ring");
77+
throw_xlio_exception("Cannot find slave for a ring");
78+
}
7679
ring_logdbg("new ring_simple()");
7780

7881
BULLSEYE_EXCLUDE_BLOCK_START
@@ -789,6 +792,7 @@ mem_buf_desc_t *ring_simple::get_tx_buffers(pbuf_type type, uint32_t n_num_mem_b
789792
}
790793

791794
head = pool.get_and_pop_back();
795+
/* coverity[null_deref] */
792796
head->lwip_pbuf.ref = 1;
793797
assert(head->lwip_pbuf.type == type);
794798
head->lwip_pbuf.type = type;

src/core/sock/sockinfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,7 @@ void sockinfo::move_descs(ring *p_ring, descq_t *toq, descq_t *fromq, bool own)
16621662
for (size_t i = 0; i < size; i++) {
16631663
temp = fromq->front();
16641664
fromq->pop_front();
1665+
/* coverity[NULL_DEREFERENCE] */
16651666
if (!__xor(own, p_ring->is_member(temp->p_desc_owner))) {
16661667
toq->push_back(temp);
16671668
} else {
@@ -1703,6 +1704,7 @@ void sockinfo::push_descs_rx_ready(descq_t *cache)
17031704
temp = cache->front();
17041705
cache->pop_front();
17051706
m_n_rx_pkt_ready_list_count++;
1707+
/* coverity[NULL_DEREFERENCE] */
17061708
m_rx_ready_byte_count += temp->rx.sz_payload;
17071709
if (m_p_socket_stats) {
17081710
m_p_socket_stats->n_rx_ready_pkt_count++;

src/core/sock/sockinfo_tcp.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,17 +457,21 @@ void sockinfo_tcp::listen_entity_context()
457457
{
458458

459459
std::lock_guard<decltype(m_tcp_con_lock)> lock(m_tcp_con_lock);
460-
460+
sockinfo_tcp_listen_context *parent_listen_context = get_parent_listen_context();
461461
if (!attach_as_uc_receiver(ROLE_TCP_SERVER)) {
462-
get_parent_listen_context()->increment_error_counter();
462+
if (likely(parent_listen_context)) {
463+
parent_listen_context->increment_error_counter();
464+
}
463465
si_tcp_logerr(
464466
"Unable to attach uc receiver for listen socket rss_child in entity context.");
465467
return;
466468
}
467469

468470
// Set socket state to accept ready
469471
m_sock_state = TCP_SOCK_ACCEPT_READY;
470-
get_parent_listen_context()->increment_finish_counter();
472+
if (likely(parent_listen_context)) {
473+
parent_listen_context->increment_finish_counter();
474+
}
471475

472476
si_tcp_logdbg("Listen socket successfully setup in entity context (sock: %p, backlog: %d)",
473477
this, m_backlog);
@@ -898,6 +902,7 @@ void sockinfo_tcp::clear_rx_ready_buffers()
898902
while (m_n_rx_pkt_ready_list_count) {
899903
mem_buf_desc_t *p_rx_pkt_desc = m_rx_pkt_ready_list.get_and_pop_front();
900904
m_n_rx_pkt_ready_list_count--;
905+
/* coverity[NULL_DEREFERENCE] */
901906
m_rx_ready_byte_count -= p_rx_pkt_desc->rx.sz_payload;
902907
if (m_p_socket_stats) {
903908
m_p_socket_stats->n_rx_ready_pkt_count--;

src/core/sock/sockinfo_tcp.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ struct socket_option_t {
9191
, optlen(_optlen)
9292
, optval(malloc(optlen))
9393
{
94-
memcpy(optval, _optval, optlen);
94+
if(likely(optval)) {
95+
memcpy(optval, _optval, optlen);
96+
} else {
97+
si_tcp_logerr("Unable to allocate memory for socket option level=%d, optname=%d",
98+
_level, _optname);
99+
}
95100
}
96101

97102
~socket_option_t()

src/core/util/chunk_list.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ template <typename T> class chunk_list_t {
132132
if (unlikely(empty())) {
133133
return NULL;
134134
}
135+
/* coverity[NULL_DEREFERENCE] */
135136
return m_used_containers.front()->m_p_buffer[m_front];
136137
}
137138

src/stats/stats_reader.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,8 +1627,7 @@ void stats_reader_handler(sh_mem_t *p_sh_mem, int pid)
16271627
switch (user_params.print_details_mode) {
16281628
case e_totals:
16291629
/* coverity[forward_null] */
1630-
num_act_inst = show_socket_stats(p_sh_mem->skt_inst_arr, NULL, p_sh_mem->max_skt_inst_num,
1631-
&printed_line_num, &p_sh_mem->mc_info, pid);
1630+
num_act_inst = show_socket_stats(p_sh_mem->skt_inst_arr, NULL, p_sh_mem->max_skt_inst_num, &printed_line_num, &p_sh_mem->mc_info, pid);
16321631
show_iomux_stats(&p_sh_mem->iomux, NULL, &printed_line_num);
16331632
if (user_params.view_mode == e_full) {
16341633
show_cq_stats(p_sh_mem->cq_inst_arr, NULL);

0 commit comments

Comments
 (0)