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

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

commit 7801c46a0dd49b8a4cb81ddcaa4fd850681044b6
Author: hujun5 <[email protected]>
AuthorDate: Wed Aug 13 20:28:23 2025 +0800

    sem_rw.c: down_read_trylock coverity HIS_metric_violation: RETURN
    
    This change consolidates multiple return statements in down_read_trylock() 
into
    a single exit point and replaces goto with if-else structure to reduce 
cyclomatic
    complexity and comply with MISRA HIS coding standards.
    
    Signed-off-by: hujun5 <[email protected]>
---
 sched/semaphore/sem_rw.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/sched/semaphore/sem_rw.c b/sched/semaphore/sem_rw.c
index 7aa8e05be74..a79ce0c0848 100644
--- a/sched/semaphore/sem_rw.c
+++ b/sched/semaphore/sem_rw.c
@@ -65,6 +65,8 @@ static inline void up_wait(FAR rw_semaphore_t *rwsem)
 
 int down_read_trylock(FAR rw_semaphore_t *rwsem)
 {
+  int ret = 1;
+
   nxmutex_lock(&rwsem->protected);
 
   /* if the write lock is already held by oneself and since the write lock
@@ -75,25 +77,22 @@ int down_read_trylock(FAR rw_semaphore_t *rwsem)
   if (rwsem->holder == _SCHED_GETTID())
     {
       rwsem->writer++;
-      goto out;
     }
-
-  if (rwsem->writer > 0)
+  else if (rwsem->writer > 0)
     {
-      nxmutex_unlock(&rwsem->protected);
-      return 0;
+      ret = 0;
     }
+  else
+    {
+      /* In a scenario where there is no write lock, we just need to
+       * make the read base +1.
+       */
 
-  /* In a scenario where there is no write lock, we just need to make the
-   * read base +1.
-   */
-
-  rwsem->reader++;
+      rwsem->reader++;
+    }
 
-out:
   nxmutex_unlock(&rwsem->protected);
-
-  return 1;
+  return ret;
 }
 
 /****************************************************************************

Reply via email to