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
 #include <stdint.h>
26
 #include <stdint.h>
27
 #include <stdio.h>
27
 #include <stdio.h>
28
 #include <strings.h>
28
 #include <strings.h>
29
+#include <limits.h>
29
 #include <assert.h>
30
 #include <assert.h>
30
 #include <ipxe/isqrt.h>
31
 #include <ipxe/isqrt.h>
31
 #include <ipxe/profile.h>
32
 #include <ipxe/profile.h>
122
 	 */
123
 	 */
123
 	assert ( ( ( signed ) sample ) >= 0 );
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
 	/* Adjust mean sample value scale if necessary.  Skip if
130
 	/* Adjust mean sample value scale if necessary.  Skip if
129
 	 * sample is zero (in which case flsl(sample)-1 would
131
 	 * sample is zero (in which case flsl(sample)-1 would

Loading…
Cancel
Save