Page MenuHomePhabricator

Fix potential timedata overflow
ClosedPublic

Authored by jasonbcox on May 11 2020, 03:04.

Details

Summary

The add before division can potentially cause an overflow.
If we divide before adding, no overflow can occur.

Test Plan

ninja check

Diff Detail

Repository
rABC Bitcoin ABC
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Fabien requested changes to this revision.May 11 2020, 08:06
Fabien added a subscriber: Fabien.
Fabien added inline comments.
src/timedata.h
54 ↗(On Diff #19879)

Let's say that vSorted[vSortedSize / 2 - 1] == vSorted[vSortedSize / 2 ] == 3.
Before the result was (3 + 3) / 2 = 6/2 = 3 and now it is 3/2 + 3/2 = 1 + 1 = 2 because of integer division.

This revision now requires changes to proceed.May 11 2020, 08:06
jasonbcox added inline comments.
src/timedata.h
54 ↗(On Diff #19879)

Although there is a slight change in behavior when both elements are odd (median is 1 less), this result is inconsequential since the time offsets are not super accurate anyway.

deadalnix requested changes to this revision.May 11 2020, 18:01
deadalnix added a subscriber: deadalnix.
deadalnix added inline comments.
src/timedata.h
54 ↗(On Diff #19879)

int a, b;
int avg = a / 2 + b / 2 + (a & b & 1);

voila.

This revision now requires changes to proceed.May 11 2020, 18:01

Fix median calculation when both elements are odd

This revision is now accepted and ready to land.May 11 2020, 22:42
This revision was automatically updated to reflect the committed changes.