mirror of https://github.com/ipxe/ipxe.git
[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>pull/71/head
parent
b11ae1d91b
commit
ae93064496
|
@ -26,6 +26,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <ipxe/isqrt.h>
|
#include <ipxe/isqrt.h>
|
||||||
#include <ipxe/profile.h>
|
#include <ipxe/profile.h>
|
||||||
|
@ -122,7 +123,8 @@ void profile_update ( struct profiler *profiler, unsigned long sample ) {
|
||||||
*/
|
*/
|
||||||
assert ( ( ( signed ) sample ) >= 0 );
|
assert ( ( ( signed ) sample ) >= 0 );
|
||||||
|
|
||||||
/* Update sample count */
|
/* Update sample count, limiting to avoid signed overflow */
|
||||||
|
if ( profiler->count < INT_MAX )
|
||||||
profiler->count++;
|
profiler->count++;
|
||||||
|
|
||||||
/* Adjust mean sample value scale if necessary. Skip if
|
/* Adjust mean sample value scale if necessary. Skip if
|
||||||
|
|
Loading…
Reference in New Issue