From 1ab8b921f63c14c80591def51a0a95cae93ef01e Mon Sep 17 00:00:00 2001 From: Junbo Zheng Date: Wed, 12 Oct 2022 20:33:58 +0800 Subject: [PATCH] nuttx/sched/sched: fix runtime error UBSan(division by zero) NuttShell (NSH) NuttX-10.3.0 ap> ap> ap> uptime sched/sched_sysinfo.c:69:37: runtime error: division by zero [1] 28220 floating point exception sudo ./nuttx Signed-off-by: Junbo Zheng --- sched/sched/sched_sysinfo.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/sched/sched/sched_sysinfo.c b/sched/sched/sched_sysinfo.c index 5562ebd0d3..024fd605a7 100644 --- a/sched/sched/sched_sysinfo.c +++ b/sched/sched/sched_sysinfo.c @@ -65,10 +65,17 @@ int sysinfo(FAR struct sysinfo *info) #ifdef CONFIG_SCHED_CPULOAD clock_cpuload(0, &cpuload); - info->loads[0] = ((cpuload.total - cpuload.active) << - SI_LOAD_SHIFT) / cpuload.total; - info->loads[1] = info->loads[0]; - info->loads[2] = info->loads[0]; + /* On the simulator, you may hit cpuload.total == 0, but probably never + * on real hardware. + */ + + if (cpuload.total) + { + info->loads[0] = ((cpuload.total - cpuload.active) << + SI_LOAD_SHIFT) / cpuload.total; + info->loads[1] = info->loads[0]; + info->loads[2] = info->loads[0]; + } #endif #ifdef MM_KERNEL_USRHEAP_INIT