This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit d4ca8e15494e59bfc22f8d6a4f52ee225281d865 Author: Martin Desruisseaux <[email protected]> AuthorDate: Fri Jan 11 16:15:55 2019 +0100 When adding a margin to a grid extent, need to check for overflow. --- .../src/main/java/org/apache/sis/coverage/grid/GridExtent.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java index d1f396b..7c0230f 100644 --- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java +++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java @@ -379,11 +379,15 @@ public class GridExtent implements Serializable { * If the user specified a margin, add it now. The margin dimension indices follow the envelope * dimension indices. Note that the resulting extent will be intersected with enclosing extent * at the next step, which may cancel the margin effect. + * + * Note about overflow checks: if m>0, then x < x+m unless the result overflows the 'long' capacity. + * We detect overflows for the m>0 case with compare(x, x+m) > 0. If m<0 the logic is inverted; this + * is the purpose of ^m. */ if (margin != null && i < margin.length) { final int m = margin[i]; - lower = Math.subtractExact(lower, m); - upper = Math.addExact(upper, m); + if ((Long.compare(lower, lower -= m) ^ m) < 0) lower = Long.MIN_VALUE; // Clamp to MIN/MAX if overflow. + if ((Long.compare(upper, upper += m) ^ m) > 0) upper = Long.MAX_VALUE; } if (lower > upper) { upper += (lower - upper) >>> 1; // (upper - lower) as unsigned integer: overflow-safe.
