From c844ecf3938bed688e7ba407f56689b3bf73cd57 Mon Sep 17 00:00:00 2001 From: hujun5 Date: Wed, 13 Aug 2025 13:21:17 +0800 Subject: [PATCH] sched_profil.c: coverity HIS_metric_violation: RETURN This change consolidates multiple return statements in the profil() function into a single exit point by inverting the parameter validation condition and restructuring error handling to reduce cyclomatic complexity and comply with MISRA HIS. Signed-off-by: hujun5 --- sched/sched/sched_profil.c | 47 +++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/sched/sched/sched_profil.c b/sched/sched/sched_profil.c index cd1b6dea551..585c5b24275 100644 --- a/sched/sched/sched_profil.c +++ b/sched/sched/sched_profil.c @@ -142,30 +142,35 @@ int profil(FAR unsigned short *buf, size_t bufsiz, FAR struct profinfo_s *prof = &g_prof; irqstate_t flags; uintptr_t highpc; + int ret = OK; - if (scale > 65536) + if (scale <= 65536) + { + if (buf != NULL && scale != 0) + { + memset(buf, 0, bufsiz); + highpc = (uintmax_t)bufsiz * 65536 / scale; + + flags = spin_lock_irqsave(&prof->lock); + prof->counter = buf; + prof->lowpc = offset; + prof->highpc = offset + highpc; + prof->scale = scale; + spin_unlock_irqrestore(&prof->lock, flags); + + wd_start(&prof->timer, PROFTICK, profil_timer_handler, + (wdparm_t)(uintptr_t)prof); + } + else + { + wd_cancel(&prof->timer); + } + } + else { set_errno(EINVAL); - return ERROR; + ret = ERROR; } - if (buf == NULL || scale == 0) - { - wd_cancel(&prof->timer); - return OK; - } - - memset(buf, 0, bufsiz); - highpc = (uintmax_t)bufsiz * 65536 / scale; - - flags = spin_lock_irqsave(&prof->lock); - prof->counter = buf; - prof->lowpc = offset; - prof->highpc = offset + highpc; - prof->scale = scale; - spin_unlock_irqrestore(&prof->lock, flags); - - wd_start(&prof->timer, PROFTICK, profil_timer_handler, - (wdparm_t)(uintptr_t)prof); - return OK; + return ret; }