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

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

commit ade88c9da2ff8f1ef86cfabd2296ed6790b9d750
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Mon Feb 9 19:27:45 2026 +0800

    lib/math32: Fix coverity of divide 0.
    
    This commit fixed the coverity warning: divide 0.
    
    Add DEBUGASSERT(d > 1) to invdiv_init_param32() and
    invdiv_init_param64(): d == 0 would divide by zero and d == 1 is
    excluded by the documented contract ("d != 0 and d != 1 is required").
    
    Assisted-By: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/nuttx/lib/math32.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h
index 1832013141c..d76c0dd1f24 100644
--- a/include/nuttx/lib/math32.h
+++ b/include/nuttx/lib/math32.h
@@ -30,6 +30,7 @@
 #include <nuttx/config.h>
 #include <nuttx/compiler.h>
 
+#include <assert.h>
 #include <limits.h>
 #include <inttypes.h>
 #include <stdint.h>
@@ -376,6 +377,8 @@ void invdiv_init_param32(uint32_t d, FAR invdiv_param32_t 
*ip)
   uint64_t t1 = (uint64_t)1 << 32;
   uint64_t t2 = (uint64_t)1 << l;
 
+  DEBUGASSERT(d > 1);
+
   ip->mult  = (t1 * (t2 - d)) / d + 1;
 #if ULONG_MAX == 4294967295UL
   ip->shift = l - 1;
@@ -514,6 +517,8 @@ void invdiv_init_param64(uint64_t d, FAR invdiv_param64_t 
*ip)
   int      l = log2ceil(d);
   uint64_t t = ((uint64_t)1 << l) - d;
 
+  DEBUGASSERT(d > 1);
+
 #ifndef __SIZEOF_INT128__
   /* If the compiler do not support UINT128 */
 

Reply via email to