Skip to content

Commit 30ce90c

Browse files
committed
5041655: Force zero size of Long.MAX_VALUE - position; revert Windows change
1 parent f97a412 commit 30ce90c

File tree

2 files changed

+15
-35
lines changed

2 files changed

+15
-35
lines changed

src/java.base/share/classes/java/nio/channels/FileLock.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ protected FileLock(FileChannel channel,
160160
throw new IllegalArgumentException("Negative position + size");
161161
this.channel = channel;
162162
this.position = position;
163-
this.size = size;
163+
this.size = size == 0 ? Long.MAX_VALUE - position : size;
164164
this.shared = shared;
165165
}
166166

@@ -202,7 +202,7 @@ protected FileLock(AsynchronousFileChannel channel,
202202
throw new IllegalArgumentException("Negative position + size");
203203
this.channel = channel;
204204
this.position = position;
205-
this.size = size;
205+
this.size = size == 0 ? Long.MAX_VALUE - position : size;
206206
this.shared = shared;
207207
}
208208

@@ -284,11 +284,17 @@ public final boolean isShared() {
284284
* least one byte
285285
*/
286286
public final boolean overlaps(long position, long size) {
287-
System.out.printf("%d %d %d %d%n", this.position, this.size,
288-
position, size);
289287
if (size < 0)
290288
return false;
291289

290+
try {
291+
if (Math.addExact(this.position, this.size) <= position)
292+
return false; // This is below that
293+
} catch (ArithmeticException ignored) {
294+
// the sum of this.position and this.size overflows the range of
295+
// long hence their mathematical sum is greater than position
296+
}
297+
292298
// if size == 0 then the specified lock range is unbounded and
293299
// cannot be below the range of this lock
294300
if (size > 0) {
@@ -301,18 +307,6 @@ public final boolean overlaps(long position, long size) {
301307
}
302308
}
303309

304-
// if this.size == 0 then the range of this lock is unbounded and
305-
// cannot be below the specified lock range
306-
if (this.size > 0) {
307-
try {
308-
if (Math.addExact(this.position, this.size) <= position)
309-
return false; // This is below that
310-
} catch (ArithmeticException ignored) {
311-
// the sum of this.position and this.size overflows the range of
312-
// long hence their mathematical sum is greater than position
313-
}
314-
}
315-
316310
return true;
317311
}
318312

src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -389,15 +389,8 @@ Java_sun_nio_ch_FileDispatcherImpl_lock0(JNIEnv *env, jobject this, jobject fdo,
389389
HANDLE h = (HANDLE)(handleval(env, fdo));
390390
DWORD lowPos = (DWORD)pos;
391391
long highPos = (long)(pos >> 32);
392-
DWORD lowNumBytes;
393-
DWORD highNumBytes;
394-
if (size == 0) {
395-
lowNumBytes = MAXDWORD;
396-
highNumBytes = MAXDWORD;
397-
} else {
398-
lowNumBytes = (DWORD)size;
399-
highNumBytes = (DWORD)(size >> 32);
400-
}
392+
DWORD lowNumBytes = (DWORD)size;
393+
DWORD highNumBytes = (DWORD)(size >> 32);
401394
BOOL result;
402395
DWORD flags = 0;
403396
OVERLAPPED o;
@@ -441,15 +434,8 @@ Java_sun_nio_ch_FileDispatcherImpl_release0(JNIEnv *env, jobject this,
441434
HANDLE h = (HANDLE)(handleval(env, fdo));
442435
DWORD lowPos = (DWORD)pos;
443436
long highPos = (long)(pos >> 32);
444-
DWORD lowNumBytes;
445-
DWORD highNumBytes;
446-
if (size == 0) {
447-
lowNumBytes = MAXDWORD;
448-
highNumBytes = MAXDWORD;
449-
} else {
450-
lowNumBytes = (DWORD)size;
451-
highNumBytes = (DWORD)(size >> 32);
452-
}
437+
DWORD lowNumBytes = (DWORD)size;
438+
DWORD highNumBytes = (DWORD)(size >> 32);
453439
BOOL result = 0;
454440
OVERLAPPED o;
455441
o.hEvent = 0;

0 commit comments

Comments
 (0)