Avoid overflow when calculating midpoint in mergesort
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/27707a3f Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/27707a3f Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/27707a3f Branch: refs/heads/master Commit: 27707a3fc478ec4bc423bbe87c2b29a93850c131 Parents: 67fb2ea Author: Nick Wellnhofer <[email protected]> Authored: Sun Apr 26 18:07:08 2015 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Sun Apr 26 19:45:01 2015 +0200 ---------------------------------------------------------------------- runtime/core/Clownfish/Util/SortUtils.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/27707a3f/runtime/core/Clownfish/Util/SortUtils.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Util/SortUtils.c b/runtime/core/Clownfish/Util/SortUtils.c index e1fd72e..3137531 100644 --- a/runtime/core/Clownfish/Util/SortUtils.c +++ b/runtime/core/Clownfish/Util/SortUtils.c @@ -106,7 +106,7 @@ S_msort4(void *velems, void *vscratch, uint32_t left, uint32_t right, uint8_t *elems = (uint8_t*)velems; uint8_t *scratch = (uint8_t*)vscratch; if (right > left) { - const uint32_t mid = ((right + left) / 2) + 1; + const uint32_t mid = left + (right - left) / 2 + 1; S_msort4(elems, scratch, left, mid - 1, compare, context); S_msort4(elems, scratch, mid, right, compare, context); SI_merge((elems + left * WIDTH), (mid - left), @@ -124,7 +124,7 @@ S_msort8(void *velems, void *vscratch, uint32_t left, uint32_t right, uint8_t *elems = (uint8_t*)velems; uint8_t *scratch = (uint8_t*)vscratch; if (right > left) { - const uint32_t mid = ((right + left) / 2) + 1; + const uint32_t mid = left + (right - left) / 2 + 1; S_msort8(elems, scratch, left, mid - 1, compare, context); S_msort8(elems, scratch, mid, right, compare, context); SI_merge((elems + left * WIDTH), (mid - left), @@ -141,7 +141,7 @@ S_msort_any(void *velems, void *vscratch, uint32_t left, uint32_t right, uint8_t *elems = (uint8_t*)velems; uint8_t *scratch = (uint8_t*)vscratch; if (right > left) { - const uint32_t mid = ((right + left) / 2) + 1; + const uint32_t mid = left + (right - left) / 2 + 1; S_msort_any(elems, scratch, left, mid - 1, compare, context, width); S_msort_any(elems, scratch, mid, right, compare, context, width); SI_merge((elems + left * width), (mid - left),
