Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/arm/rockchip Fix signed integer overflow found by k...



details:   https://anonhg.NetBSD.org/src/rev/857c2f8bc134
branches:  trunk
changeset: 379222:857c2f8bc134
user:      msaitoh <msaitoh%NetBSD.org@localhost>
date:      Thu May 20 01:07:24 2021 +0000

description:
Fix signed integer overflow found by kUBSan. OK'd by jmcneill.

 The output was:
   UBSan: Undefined Behavior in ../../../../arch/arm/rockchip/rk3399_cru.c:
   284:13, signed integer overflow: 594000000 - -2086967296 cannot be
   represented in type 'int'

diffstat:

 sys/arch/arm/rockchip/rk3399_cru.c |  15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)

diffs (37 lines):

diff -r cb58544f0a7b -r 857c2f8bc134 sys/arch/arm/rockchip/rk3399_cru.c
--- a/sys/arch/arm/rockchip/rk3399_cru.c        Thu May 20 01:02:42 2021 +0000
+++ b/sys/arch/arm/rockchip/rk3399_cru.c        Thu May 20 01:07:24 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: rk3399_cru.c,v 1.21 2021/01/27 03:10:19 thorpej Exp $ */
+/* $NetBSD: rk3399_cru.c,v 1.22 2021/05/20 01:07:24 msaitoh Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <jmcneill%invisible.ca@localhost>
@@ -28,7 +28,7 @@
 
 #include <sys/cdefs.h>
 
-__KERNEL_RCSID(1, "$NetBSD: rk3399_cru.c,v 1.21 2021/01/27 03:10:19 thorpej Exp $");
+__KERNEL_RCSID(1, "$NetBSD: rk3399_cru.c,v 1.22 2021/05/20 01:07:24 msaitoh Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -281,10 +281,15 @@ rk3399_cru_pll_set_rate(struct rk_cru_so
 
        best_diff = INT_MAX;
        for (int i = 0; i < pll->nrates; i++) {
-               const int diff = (int)rate - (int)pll->rates[i].rate;
-               if (abs(diff) < best_diff) {
+               int diff;
+
+               if (rate > pll->rates[i].rate)
+                       diff = rate - pll->rates[i].rate;
+               else
+                       diff = pll->rates[i].rate - rate;
+               if (diff < best_diff) {
                        pll_rate = &pll->rates[i];
-                       best_diff = abs(diff);
+                       best_diff = diff;
                }
        }
 



Home | Main Index | Thread Index | Old Index