From ef17ee4dea38c3bb953927bfdaabf1fe1b7e54ea Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Thu, 23 Jun 2022 15:40:23 +0000 Subject: [PATCH] 8288515: (ch) Unnecessary use of Math.addExact() in java.nio.channels.FileLock.overlaps() Reviewed-by: alanb --- .../share/classes/java/nio/channels/FileLock.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/java.base/share/classes/java/nio/channels/FileLock.java b/src/java.base/share/classes/java/nio/channels/FileLock.java index 3bd0b9c7b2ca9..b5c0ba4a81f36 100644 --- a/src/java.base/share/classes/java/nio/channels/FileLock.java +++ b/src/java.base/share/classes/java/nio/channels/FileLock.java @@ -277,13 +277,10 @@ public final boolean overlaps(long position, long size) { if (size < 0) return false; - // Test whether this is below that - try { - if (Math.addExact(this.position, this.size) <= position) - return false; - } catch (ArithmeticException ignored) { - // the sum of this.position and this.size overflows the range of - // long hence their mathematical sum is greater than position + // Test whether this is below that. The sum cannot overflow as the + // size and position are immutable and were checked at construction. + if (this.position + this.size <= position) { + return false; } // if size == 0 then the specified lock range is unbounded and