Skip to content

Commit

Permalink
Fix: medium Coverity issues (#1016)
Browse files Browse the repository at this point in the history
Check if pointer is NULL before dereferencing it in st_rx_video_session.c
Fix Improper use of negative value in mt_shared_queue.c
Handle return val from send_response() in mtl_instance.hpp
Handle return val from ioctl() in v4l2_to_ip.c

Signed-off-by: Kasiewicz, Marek <[email protected]>
  • Loading branch information
Sakoram authored Dec 12, 2024
1 parent 0de0be7 commit 08139b4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 29 deletions.
6 changes: 5 additions & 1 deletion app/v4l2_to_ip/v4l2_to_ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,11 @@ static void video_close(struct device* dev) {
}

static void video_log_status(struct device* dev) {
ioctl(dev->fd, VIDIOC_LOG_STATUS);
int ret;
ret = ioctl(dev->fd, VIDIOC_LOG_STATUS);
if (ret < 0) {
printf("Failed to log status: %s (%d).\n", strerror(errno), errno);
}
}

static int video_get_format(struct device* dev) {
Expand Down
8 changes: 4 additions & 4 deletions lib/src/datapath/mt_shared_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ static int rsq_entry_free(struct mt_rsq_entry* entry) {
mt_ring_dequeue_clean(entry->ring);
rte_ring_free(entry->ring);
}
if (entry->mcast_fd) {
close(entry->mcast_fd);
entry->mcast_fd = -1;
}
if (entry->mcast_fd > 0) close(entry->mcast_fd);

entry->mcast_fd = -1;

info("%s(%d), succ on q %u idx %d\n", __func__, rsqm->port, entry->queue_id,
entry->idx);
mt_rte_free(entry);
Expand Down
15 changes: 7 additions & 8 deletions lib/src/st2110/st_rx_video_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -1325,14 +1325,9 @@ static int rv_start_pcap(struct st_rx_video_session_impl* s, enum mtl_session_po
err("%s(%d,%d), pcap dump already started\n", __func__, idx, s_port);
return -EIO;
}
snprintf(pcap->file_name, sizeof(pcap->file_name), "st22rx_s%dp%d_%u_XXXXXX.pcapng",
idx, s_port, max_dump_packets);

if (s->st22_info) {
snprintf(pcap->file_name, sizeof(pcap->file_name), "st22rx_s%dp%d_%u_XXXXXX.pcapng",
idx, s_port, max_dump_packets);
} else {
snprintf(pcap->file_name, sizeof(pcap->file_name), "st22rx_s%dp%d_%u_XXXXXX.pcapng",
idx, s_port, max_dump_packets);
}
int fd = mt_mkstemps(pcap->file_name, strlen(".pcapng"));
if (fd < 0) {
err("%s(%d,%d), failed to create pcap file %s\n", __func__, idx, s_port,
Expand Down Expand Up @@ -3337,7 +3332,8 @@ static int rv_migrate_dma(struct mtl_main_impl* impl,

static void rv_stat(struct st_rx_video_sessions_mgr* mgr,
struct st_rx_video_session_impl* s) {
int m_idx = mgr ? mgr->idx : 0, idx = s->idx;
int m_idx = mgr->idx;
int idx = s->idx;
uint64_t cur_time_ns = mt_get_monotonic_time();
double time_sec = (double)(cur_time_ns - s->stat_last_time) / NS_PER_S;
int frames_received = rte_atomic32_read(&s->stat_frames_received);
Expand Down Expand Up @@ -3560,6 +3556,7 @@ static int rvs_pkt_rx_tasklet_start(void* priv) {

static int rv_detach(struct mtl_main_impl* impl, struct st_rx_video_sessions_mgr* mgr,
struct st_rx_video_session_impl* s) {
if (!mgr || !s) return -EINVAL;
s->attached = false;
rv_stat(mgr, s);
rv_uinit(impl, s);
Expand Down Expand Up @@ -3776,6 +3773,8 @@ static int rv_sessions_stat(void* priv) {
struct st_rx_video_sessions_mgr* mgr = priv;
struct st_rx_video_session_impl* s;

if (!mgr) return -EINVAL;

for (int j = 0; j < mgr->max_idx; j++) {
s = rx_video_session_get_timeout(mgr, j, ST_SESSION_STAT_TIMEOUT_US);
if (!s) continue;
Expand Down
64 changes: 48 additions & 16 deletions manager/mtl_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ void mtl_instance::handle_message_get_lcore(mtl_lcore_message_t* lcore_msg) {
uint16_t lcore_id = ntohs(lcore_msg->lcore);
int ret = mtl_lcore::get_instance().get_lcore(lcore_id);
if (ret < 0) {
send_response(ret);
if (send_response(ret) < 0) {
log(log_level::ERROR, "Failed to send response for get_lcore");
}
return;
}
lcore_ids.insert(lcore_id);
log(log_level::INFO, "Added lcore " + std::to_string(lcore_id));
send_response(0);
if (send_response(0) < 0) {
log(log_level::ERROR, "Failed to send response for get_lcore");
}
}

void mtl_instance::handle_message_put_lcore(mtl_lcore_message_t* lcore_msg) {
Expand All @@ -172,12 +176,16 @@ void mtl_instance::handle_message_put_lcore(mtl_lcore_message_t* lcore_msg) {
uint16_t lcore_id = ntohs(lcore_msg->lcore);
int ret = mtl_lcore::get_instance().put_lcore(lcore_id);
if (ret < 0) {
send_response(ret);
if (send_response(ret) < 0) {
log(log_level::ERROR, "Failed to send response for put_lcore");
}
return;
}
lcore_ids.erase(lcore_id);
log(log_level::INFO, "Removed lcore " + std::to_string(lcore_id));
send_response(0);
if (send_response(0) < 0) {
log(log_level::ERROR, "Failed to send response for put_lcore");
}
}

void mtl_instance::handle_message_register(mtl_register_message_t* register_msg) {
Expand All @@ -190,15 +198,19 @@ void mtl_instance::handle_message_register(mtl_register_message_t* register_msg)
auto interface = get_interface(ifindex);
if (interface == nullptr) {
log(log_level::ERROR, "Could not get interface " + std::to_string(ifindex));
send_response(-1);
if (send_response(-1) < 0) {
log(log_level::ERROR, "Failed to send response for register");
}
return;
}
}

log(log_level::INFO, "Registered.");

is_registered = true;
send_response(0);
if (send_response(0) < 0) {
log(log_level::ERROR, "Failed to send response for register");
}
}

std::shared_ptr<mtl_interface> mtl_instance::get_interface(const unsigned int ifindex) {
Expand Down Expand Up @@ -265,67 +277,87 @@ void mtl_instance::handle_message_udp_dp_filter(
auto interface = get_interface(ifindex);
if (interface == nullptr) {
log(log_level::ERROR, "Failed to get interface " + std::to_string(ifindex));
send_response(-1);
if (send_response(-1) < 0) {
log(log_level::ERROR, "Failed to send response for udp_dp_filter");
}
return;
}
int ret = interface->update_udp_dp_filter(port, add);
send_response(ret);
if (send_response(ret) < 0) {
log(log_level::ERROR, "Failed to send response for udp_dp_filter");
}
}

void mtl_instance::handle_message_if_get_queue(mtl_if_message_t* if_msg) {
unsigned int ifindex = ntohl(if_msg->ifindex);
auto interface = get_interface(ifindex);
if (interface == nullptr) {
log(log_level::ERROR, "Failed to get interface " + std::to_string(ifindex));
send_response(-1, MTL_MSG_TYPE_IF_QUEUE_ID);
if (send_response(-1, MTL_MSG_TYPE_IF_QUEUE_ID) < 0) {
log(log_level::ERROR, "Failed to send response for if_get_queue");
}
return;
}
int ret = interface->get_queue();
if (ret > 0) if_queue_ids[ifindex].insert(ret);
send_response(ret, MTL_MSG_TYPE_IF_QUEUE_ID);
if (send_response(ret, MTL_MSG_TYPE_IF_QUEUE_ID) < 0) {
log(log_level::ERROR, "Failed to send response for if_get_queue");
}
}

void mtl_instance::handle_message_if_put_queue(mtl_if_message_t* if_msg) {
unsigned int ifindex = ntohl(if_msg->ifindex);
auto interface = get_interface(ifindex);
if (interface == nullptr) {
log(log_level::ERROR, "Failed to get interface " + std::to_string(ifindex));
send_response(-1);
if (send_response(-1) < 0) {
log(log_level::ERROR, "Failed to send response for if_put_queue");
}
return;
}
uint16_t queue_id = ntohs(if_msg->queue_id);
int ret = interface->put_queue(queue_id);
if (ret == 0) if_queue_ids[ifindex].erase(queue_id);
send_response(ret);
if (send_response(ret) < 0) {
log(log_level::ERROR, "Failed to send response for if_put_queue");
}
}

void mtl_instance::handle_message_if_add_flow(mtl_if_message_t* if_msg) {
unsigned int ifindex = ntohl(if_msg->ifindex);
auto interface = get_interface(ifindex);
if (interface == nullptr) {
log(log_level::ERROR, "Failed to get interface " + std::to_string(ifindex));
send_response(-1);
if (send_response(-1) < 0) {
log(log_level::ERROR, "Failed to send response for if_add_flow");
}
return;
}
int ret = interface->add_flow(ntohs(if_msg->queue_id), ntohl(if_msg->flow_type),
ntohl(if_msg->src_ip), ntohl(if_msg->dst_ip),
ntohs(if_msg->src_port), ntohs(if_msg->dst_port));
if (ret > 0) if_flow_ids[ifindex].insert(ret);
send_response(ret, MTL_MSG_TYPE_IF_FLOW_ID);
if (send_response(ret, MTL_MSG_TYPE_IF_FLOW_ID) < 0) {
log(log_level::ERROR, "Failed to send response for if_add_flow");
}
}

void mtl_instance::handle_message_if_del_flow(mtl_if_message_t* if_msg) {
unsigned int ifindex = ntohl(if_msg->ifindex);
auto interface = get_interface(ifindex);
if (interface == nullptr) {
log(log_level::ERROR, "Failed to get interface " + std::to_string(ifindex));
send_response(-1);
if (send_response(-1) < 0) {
log(log_level::ERROR, "Failed to send response for if_del_flow");
}
return;
}
unsigned int flow_id = ntohl(if_msg->flow_id);
int ret = interface->del_flow(flow_id);
if (ret == 0) if_flow_ids[ifindex].erase(flow_id);
send_response(ret);
if (send_response(ret) < 0) {
log(log_level::ERROR, "Failed to send response for if_del_flow");
}
}

#endif

0 comments on commit 08139b4

Please sign in to comment.