Browse Source

[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>
tags/v1.20.1
Michael Brown 6 years ago
parent
commit
ae93064496
1 changed files with 4 additions and 2 deletions
  1. 4
    2
      src/core/profile.c

+ 4
- 2
src/core/profile.c View File

@@ -26,6 +26,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
26 26
 #include <stdint.h>
27 27
 #include <stdio.h>
28 28
 #include <strings.h>
29
+#include <limits.h>
29 30
 #include <assert.h>
30 31
 #include <ipxe/isqrt.h>
31 32
 #include <ipxe/profile.h>
@@ -122,8 +123,9 @@ void profile_update ( struct profiler *profiler, unsigned long sample ) {
122 123
 	 */
123 124
 	assert ( ( ( signed ) sample ) >= 0 );
124 125
 
125
-	/* Update sample count */
126
-	profiler->count++;
126
+	/* Update sample count, limiting to avoid signed overflow */
127
+	if ( profiler->count < INT_MAX )
128
+		profiler->count++;
127 129
 
128 130
 	/* Adjust mean sample value scale if necessary.  Skip if
129 131
 	 * sample is zero (in which case flsl(sample)-1 would

Loading…
Cancel
Save