diff options
author | Michael Brown | 2018-03-18 17:36:58 +0100 |
---|---|---|
committer | Michael Brown | 2018-03-18 17:39:39 +0100 |
commit | ae930644962a886d3845e5d8836c5cf136c82df0 (patch) | |
tree | 71bf03441f6080217b394bcd889ae8df6501bcf0 | |
parent | [tftp] Prevent potential division by zero (diff) | |
download | ipxe-ae930644962a886d3845e5d8836c5cf136c82df0.tar.gz ipxe-ae930644962a886d3845e5d8836c5cf136c82df0.tar.xz ipxe-ae930644962a886d3845e5d8836c5cf136c82df0.zip |
[profile] Prevent potential division by zero
Limit the profile sample count to INT_MAX to avoid both signed
overflow and a potential division by zero when updating the stored
mean value.
Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r-- | src/core/profile.c | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/core/profile.c b/src/core/profile.c index 1075047b..3655108e 100644 --- a/src/core/profile.c +++ b/src/core/profile.c @@ -26,6 +26,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include <stdint.h> #include <stdio.h> #include <strings.h> +#include <limits.h> #include <assert.h> #include <ipxe/isqrt.h> #include <ipxe/profile.h> @@ -122,8 +123,9 @@ void profile_update ( struct profiler *profiler, unsigned long sample ) { */ assert ( ( ( signed ) sample ) >= 0 ); - /* Update sample count */ - profiler->count++; + /* Update sample count, limiting to avoid signed overflow */ + if ( profiler->count < INT_MAX ) + profiler->count++; /* Adjust mean sample value scale if necessary. Skip if * sample is zero (in which case flsl(sample)-1 would |