This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 2ec7d90ebad sched_setparam.c: coverity HIS_metric_violation: RETURN
2ec7d90ebad is described below

commit 2ec7d90ebade98521889c599f9f9a2fe8d7091e0
Author: hujun5 <[email protected]>
AuthorDate: Wed Jan 28 11:06:53 2026 +0800

    sched_setparam.c: coverity HIS_metric_violation: RETURN
    
    This change refactors nxsched_set_param() by extracting complex conditional 
logic
    into dedicated helper functions and consolidating multiple return 
statements into
    single exit points to reduce cyclomatic complexity and comply with MISRA 
HIS coding
    standards for safety-critical embedded systems.
    
    Signed-off-by: hujun5 <[email protected]>
---
 sched/sched/sched_setparam.c | 234 +++++++++++++++++++++++--------------------
 1 file changed, 128 insertions(+), 106 deletions(-)

diff --git a/sched/sched/sched_setparam.c b/sched/sched/sched_setparam.c
index 97c76696d43..4e8a5b7626d 100644
--- a/sched/sched/sched_setparam.c
+++ b/sched/sched/sched_setparam.c
@@ -38,6 +38,107 @@
 #include "clock/clock.h"
 #include "sched/sched.h"
 
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_SPORADIC
+static inline_function
+int set_sporadic_param(FAR const struct sched_param *param,
+                       FAR struct tcb_s *rtcb, FAR struct tcb_s *tcb)
+{
+  irqstate_t flags;
+  int ret = OK;
+
+  /* Update parameters associated with SCHED_SPORADIC */
+
+  if ((rtcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC)
+    {
+      FAR struct sporadic_s *sporadic;
+      sclock_t repl_ticks;
+      sclock_t budget_ticks;
+
+      if (param->sched_ss_max_repl >= 1 &&
+          param->sched_ss_max_repl <= CONFIG_SCHED_SPORADIC_MAXREPL)
+        {
+          /* Convert timespec values to system clock ticks */
+
+          repl_ticks = clock_time2ticks(&param->sched_ss_repl_period);
+          budget_ticks = clock_time2ticks(&param->sched_ss_init_budget);
+
+          /* Avoid zero/negative times */
+
+          if (repl_ticks < 1)
+            {
+              repl_ticks = 1;
+            }
+
+          if (budget_ticks < 1)
+            {
+              budget_ticks = 1;
+            }
+
+          /* The replenishment period must be greater than or equal to the
+           * budget period.
+           */
+
+#if 1
+          /* REVISIT: In the current implementation, the budget cannot exceed
+           * half the duty.
+           */
+
+          if (repl_ticks < (2 * budget_ticks))
+#else
+          if (repl_ticks < budget_ticks)
+#endif
+            {
+              /* Stop/reset current sporadic scheduling */
+
+              flags = enter_critical_section();
+              ret = nxsched_reset_sporadic(tcb);
+              if (ret >= 0)
+                {
+                  /* Save the sporadic scheduling parameters and reset to the
+                   * beginning to the replenishment interval.
+                   */
+
+                  tcb->timeslice         = budget_ticks;
+
+                  sporadic = rtcb->sporadic;
+                  DEBUGASSERT(sporadic != NULL);
+
+                  sporadic->hi_priority  = param->sched_priority;
+                  sporadic->low_priority = param->sched_ss_low_priority;
+                  sporadic->max_repl     = param->sched_ss_max_repl;
+                  sporadic->repl_period  = repl_ticks;
+                  sporadic->budget       = budget_ticks;
+
+                  /* And restart at the next replenishment interval */
+
+                  ret = nxsched_start_sporadic(tcb);
+                }
+
+              /* Restore interrupts and handle any pending work */
+
+              leave_critical_section(flags);
+            }
+          else
+            {
+              ret = -EINVAL;
+            }
+        }
+      else
+        {
+          ret = -EINVAL;
+        }
+    }
+
+  return ret;
+}
+#else
+#  define set_sporadic_param(p, r, t) OK
+#endif
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -80,137 +181,58 @@ int nxsched_set_param(pid_t pid, FAR const struct 
sched_param *param)
 {
   FAR struct tcb_s *rtcb;
   FAR struct tcb_s *tcb;
-  int ret;
+  int ret = OK;
 
   /* Verify that the requested priority is in the valid range */
 
-  if (param == NULL)
-    {
-      return -EINVAL;
-    }
-
-  /* Prohibit modifications to the head of the ready-to-run task
-   * list while adjusting the priority
-   */
-
-  sched_lock();
-
-  /* Check if the task to reprioritize is the calling task */
-
-  rtcb = this_task();
-  if (pid == 0 || pid == rtcb->pid)
+  if (param != NULL)
     {
-      tcb = rtcb;
-    }
+      /* Prohibit modifications to the head of the ready-to-run task
+       * list while adjusting the priority
+       */
 
-  /* The PID is not the calling task, we will have to search for it */
+      sched_lock();
 
-  else
-    {
-      tcb = nxsched_get_tcb(pid);
-      if (!tcb)
-        {
-          /* No task with this PID was found */
-
-          ret = -ESRCH;
-          goto errout_with_lock;
-        }
-    }
+      /* Check if the task to reprioritize is the calling task */
 
-#ifdef CONFIG_SCHED_SPORADIC
-  /* Update parameters associated with SCHED_SPORADIC */
-
-  if ((rtcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC)
-    {
-      FAR struct sporadic_s *sporadic;
-      irqstate_t flags;
-      sclock_t repl_ticks;
-      sclock_t budget_ticks;
-
-      if (param->sched_ss_max_repl < 1 ||
-          param->sched_ss_max_repl > CONFIG_SCHED_SPORADIC_MAXREPL)
+      rtcb = this_task();
+      if (pid == 0 || pid == rtcb->pid)
         {
-          ret = -EINVAL;
-          goto errout_with_lock;
+          tcb = rtcb;
         }
 
-      /* Convert timespec values to system clock ticks */
-
-      repl_ticks = clock_time2ticks(&param->sched_ss_repl_period);
-      budget_ticks = clock_time2ticks(&param->sched_ss_init_budget);
+      /* The PID is not the calling task, we will have to search for it */
 
-      /* Avoid zero/negative times */
-
-      if (repl_ticks < 1)
+      else
         {
-          repl_ticks = 1;
-        }
+          tcb = nxsched_get_tcb(pid);
+          if (!tcb)
+            {
+              /* No task with this PID was found */
 
-      if (budget_ticks < 1)
-        {
-          budget_ticks = 1;
+              ret = -ESRCH;
+            }
         }
 
-      /* The replenishment period must be greater than or equal to the
-       * budget period.
-       */
-
-#if 1
-      /* REVISIT: In the current implementation, the budget cannot exceed
-       * half the duty.
-       */
-
-      if (repl_ticks < (2 * budget_ticks))
-#else
-      if (repl_ticks < budget_ticks)
-#endif
+      if (ret >= 0)
         {
-          ret = -EINVAL;
-          goto errout_with_lock;
+          ret = set_sporadic_param(param, rtcb, tcb);
         }
 
-      /* Stop/reset current sporadic scheduling */
+      /* Then perform the reprioritization */
 
-      flags = enter_critical_section();
-      ret = nxsched_reset_sporadic(tcb);
       if (ret >= 0)
         {
-          /* Save the sporadic scheduling parameters and reset to the
-           * beginning to the replenishment interval.
-           */
-
-          tcb->timeslice         = budget_ticks;
-
-          sporadic = rtcb->sporadic;
-          DEBUGASSERT(sporadic != NULL);
-
-          sporadic->hi_priority  = param->sched_priority;
-          sporadic->low_priority = param->sched_ss_low_priority;
-          sporadic->max_repl     = param->sched_ss_max_repl;
-          sporadic->repl_period  = repl_ticks;
-          sporadic->budget       = budget_ticks;
-
-          /* And restart at the next replenishment interval */
-
-          ret = nxsched_start_sporadic(tcb);
+          ret = nxsched_reprioritize(tcb, param->sched_priority);
         }
 
-      /* Restore interrupts and handler errors */
-
-      leave_critical_section(flags);
-      if (ret < 0)
-        {
-          goto errout_with_lock;
-        }
+      sched_unlock();
+    }
+  else
+    {
+      ret = -EINVAL;
     }
-#endif
-
-  /* Then perform the reprioritization */
-
-  ret = nxsched_reprioritize(tcb, param->sched_priority);
 
-errout_with_lock:
-  sched_unlock();
   return ret;
 }
 

Reply via email to