FULL PRODUCT VERSION :
os.name : Windows 7
os.arch : amd64
os.version : 6.1
java.version : 1.8.0_11
java.vm.version : 25.11-b03
ADDITIONAL OS VERSION INFORMATION :
Windows7 64bit
EXTRA RELEVANT SYSTEM CONFIGURATION :
java.nio.channels.FileLock
A DESCRIPTION OF THE PROBLEM :
The channel of length 0 can overwrite with a locked channel.
emptyFileChannel.transferTo(0, 0, exclusiveLockedChannel);
is success.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
ex. test code
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
will exception
ACTUAL -
not exception.
override locked channel content.
os.name : Windows 7
os.arch : amd64
os.version : 6.1
java.version : 1.8.0_11
java.vm.version : 25.11-b03
======================================== test with current thread
---- src, empty
name: src.txt
exists: true
size: 0
---- dst, notempty, lock
name: dst.txt
exists: true
size: 5
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
overwrite with a locked channel
Is the channel locked?
java.nio.channels.OverlappingFileLockException
at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:1015)
at java.nio.channels.FileChannel.lock(FileChannel.java:1053)
at Main.copy(Main.java:23)
at Main.run(Main.java:70)
at Main.test(Main.java:100)
at Main.main(Main.java:137)
succeeded by overwrite
Is the channel locked?
java.nio.channels.OverlappingFileLockException
at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:1015)
at java.nio.channels.FileChannel.lock(FileChannel.java:1053)
at Main.copy(Main.java:31)
at Main.run(Main.java:70)
at Main.test(Main.java:100)
at Main.main(Main.java:137)
---- dst, notempty?, lock
name: dst.txt
exists: true
size: 0
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
======================================== reset
---- dst, notempty, lock
name: dst.txt
exists: true
size: 10
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
======================================== test with other thread
---- src, empty
name: src.txt
exists: true
size: 0
---- dst, notempty, lock
name: dst.txt
exists: true
size: 10
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
overwrite with a locked channel
Is the channel locked?
java.nio.channels.OverlappingFileLockException
at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:1015)
at java.nio.channels.FileChannel.lock(FileChannel.java:1053)
at Main.copy(Main.java:23)
at Main.run(Main.java:70)
at java.lang.Thread.run(Thread.java:745)
succeeded by overwrite
Is the channel locked?
java.nio.channels.OverlappingFileLockException
at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:1015)
at java.nio.channels.FileChannel.lock(FileChannel.java:1053)
at Main.copy(Main.java:31)
at Main.run(Main.java:70)
at java.lang.Thread.run(Thread.java:745)
---- dst, notempty?, lock
name: dst.txt
exists: true
size: 0
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
======================================== final
---- dst, notempty?, lock
name: dst.txt
exists: true
size: 0
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
---- dst
name: dst.txt
exists: true
size: 0
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
/**
* @author keiichirou
*/
public class Main implements Runnable {
static File buildFile(File path, String name) {
return new File(new File(path, name).getAbsolutePath());
}
static void copy(File src, File dst) throws Exception {
FileChannel srcChannel = null;
FileChannel destChannel = null;
try {
srcChannel = new FileInputStream(src).getChannel();
destChannel = new FileOutputStream(dst, false).getChannel();
try {
System.out.println("Is the channel locked?");
destChannel.lock();
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
srcChannel.transferTo(0, srcChannel.size(), destChannel);
System.out.println("succeeded by overwrite");
try {
System.out.println("Is the channel locked?");
destChannel.lock();
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
} finally {
if (srcChannel!=null) {
srcChannel.close();
}
if (destChannel!=null) {
destChannel.close();
}
}
}
static void info(File file, String title) {
System.out.println(title);
System.out.println("\tname: " + file.getName());
System.out.println("\texists: " + file.exists());
System.out.println("\tsize: " + file.length());
}
static void delete(File file) throws InterruptedException {
synchronized (file) {
while (file.exists()) {
file.delete();
file.wait(100);
}
}
}
File src;
File dst;
FileLock lock;
@Override
public void run() {
info(src, "---- src, empty");
info(dst, "---- dst, notempty, lock");
System.out.println("lock : " + lock.toString());
// empty file copy to not empty file
System.out.println("overwrite with a locked channel");
try {
copy(src, dst);
} catch (Exception e) {
e.printStackTrace(System.out);
}
info(dst, "---- dst, notempty?, lock");
System.out.println("lock : " + lock.toString());
}
public void test() throws IOException, InterruptedException {
File workdir = buildFile(new File(System.getProperty("java.io.tmpdir")), "FileLock");
workdir.delete();
workdir.mkdirs();
src = buildFile(workdir, "src.txt");
dst = buildFile(workdir, "dst.txt");
FileOutputStream dstFile = null;
delete(src);
delete(dst);
try {
// src
new FileOutputStream(src).close();
// dst
dstFile = new FileOutputStream(dst, false);
dstFile.write("test5".getBytes());
dstFile.flush();
// lock dst
lock = dstFile.getChannel().lock();
System.out.println("======================================== test with current thread");
run();
System.out.println("======================================== reset");
// reset
dstFile.write("test5".getBytes());
dstFile.flush();
info(dst, "---- dst, notempty, lock");
System.out.println("lock : " + lock.toString());
System.out.println("======================================== test with other thread");
Thread thread = new Thread(this);
thread.start();
try {
thread.join();
} catch(Exception e) {
e.printStackTrace(System.out);
}
System.out.println("======================================== final");
info(dst, "---- dst, notempty?, lock");
System.out.println("lock : " + lock.toString());
} catch (Exception ex) {
ex.printStackTrace(System.out);;
} finally {
dstFile.close();
dstFile = null;
}
info(dst, "---- dst");
}
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("os.name : " + System.getProperty("os.name"));
System.out.println("os.arch : " + System.getProperty("os.arch"));
System.out.println("os.version : " + System.getProperty("os.version"));
System.out.println("java.version : " + System.getProperty("java.version"));
System.out.println("java.vm.version : " + System.getProperty("java.vm.version"));
Main inst = new Main();
inst.test();
}
}
---------- END SOURCE ----------
os.name : Windows 7
os.arch : amd64
os.version : 6.1
java.version : 1.8.0_11
java.vm.version : 25.11-b03
ADDITIONAL OS VERSION INFORMATION :
Windows7 64bit
EXTRA RELEVANT SYSTEM CONFIGURATION :
java.nio.channels.FileLock
A DESCRIPTION OF THE PROBLEM :
The channel of length 0 can overwrite with a locked channel.
emptyFileChannel.transferTo(0, 0, exclusiveLockedChannel);
is success.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
ex. test code
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
will exception
ACTUAL -
not exception.
override locked channel content.
os.name : Windows 7
os.arch : amd64
os.version : 6.1
java.version : 1.8.0_11
java.vm.version : 25.11-b03
======================================== test with current thread
---- src, empty
name: src.txt
exists: true
size: 0
---- dst, notempty, lock
name: dst.txt
exists: true
size: 5
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
overwrite with a locked channel
Is the channel locked?
java.nio.channels.OverlappingFileLockException
at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:1015)
at java.nio.channels.FileChannel.lock(FileChannel.java:1053)
at Main.copy(Main.java:23)
at Main.run(Main.java:70)
at Main.test(Main.java:100)
at Main.main(Main.java:137)
succeeded by overwrite
Is the channel locked?
java.nio.channels.OverlappingFileLockException
at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:1015)
at java.nio.channels.FileChannel.lock(FileChannel.java:1053)
at Main.copy(Main.java:31)
at Main.run(Main.java:70)
at Main.test(Main.java:100)
at Main.main(Main.java:137)
---- dst, notempty?, lock
name: dst.txt
exists: true
size: 0
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
======================================== reset
---- dst, notempty, lock
name: dst.txt
exists: true
size: 10
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
======================================== test with other thread
---- src, empty
name: src.txt
exists: true
size: 0
---- dst, notempty, lock
name: dst.txt
exists: true
size: 10
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
overwrite with a locked channel
Is the channel locked?
java.nio.channels.OverlappingFileLockException
at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:1015)
at java.nio.channels.FileChannel.lock(FileChannel.java:1053)
at Main.copy(Main.java:23)
at Main.run(Main.java:70)
at java.lang.Thread.run(Thread.java:745)
succeeded by overwrite
Is the channel locked?
java.nio.channels.OverlappingFileLockException
at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:1015)
at java.nio.channels.FileChannel.lock(FileChannel.java:1053)
at Main.copy(Main.java:31)
at Main.run(Main.java:70)
at java.lang.Thread.run(Thread.java:745)
---- dst, notempty?, lock
name: dst.txt
exists: true
size: 0
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
======================================== final
---- dst, notempty?, lock
name: dst.txt
exists: true
size: 0
lock : sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
---- dst
name: dst.txt
exists: true
size: 0
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
/**
* @author keiichirou
*/
public class Main implements Runnable {
static File buildFile(File path, String name) {
return new File(new File(path, name).getAbsolutePath());
}
static void copy(File src, File dst) throws Exception {
FileChannel srcChannel = null;
FileChannel destChannel = null;
try {
srcChannel = new FileInputStream(src).getChannel();
destChannel = new FileOutputStream(dst, false).getChannel();
try {
System.out.println("Is the channel locked?");
destChannel.lock();
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
srcChannel.transferTo(0, srcChannel.size(), destChannel);
System.out.println("succeeded by overwrite");
try {
System.out.println("Is the channel locked?");
destChannel.lock();
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
} finally {
if (srcChannel!=null) {
srcChannel.close();
}
if (destChannel!=null) {
destChannel.close();
}
}
}
static void info(File file, String title) {
System.out.println(title);
System.out.println("\tname: " + file.getName());
System.out.println("\texists: " + file.exists());
System.out.println("\tsize: " + file.length());
}
static void delete(File file) throws InterruptedException {
synchronized (file) {
while (file.exists()) {
file.delete();
file.wait(100);
}
}
}
File src;
File dst;
FileLock lock;
@Override
public void run() {
info(src, "---- src, empty");
info(dst, "---- dst, notempty, lock");
System.out.println("lock : " + lock.toString());
// empty file copy to not empty file
System.out.println("overwrite with a locked channel");
try {
copy(src, dst);
} catch (Exception e) {
e.printStackTrace(System.out);
}
info(dst, "---- dst, notempty?, lock");
System.out.println("lock : " + lock.toString());
}
public void test() throws IOException, InterruptedException {
File workdir = buildFile(new File(System.getProperty("java.io.tmpdir")), "FileLock");
workdir.delete();
workdir.mkdirs();
src = buildFile(workdir, "src.txt");
dst = buildFile(workdir, "dst.txt");
FileOutputStream dstFile = null;
delete(src);
delete(dst);
try {
// src
new FileOutputStream(src).close();
// dst
dstFile = new FileOutputStream(dst, false);
dstFile.write("test5".getBytes());
dstFile.flush();
// lock dst
lock = dstFile.getChannel().lock();
System.out.println("======================================== test with current thread");
run();
System.out.println("======================================== reset");
// reset
dstFile.write("test5".getBytes());
dstFile.flush();
info(dst, "---- dst, notempty, lock");
System.out.println("lock : " + lock.toString());
System.out.println("======================================== test with other thread");
Thread thread = new Thread(this);
thread.start();
try {
thread.join();
} catch(Exception e) {
e.printStackTrace(System.out);
}
System.out.println("======================================== final");
info(dst, "---- dst, notempty?, lock");
System.out.println("lock : " + lock.toString());
} catch (Exception ex) {
ex.printStackTrace(System.out);;
} finally {
dstFile.close();
dstFile = null;
}
info(dst, "---- dst");
}
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("os.name : " + System.getProperty("os.name"));
System.out.println("os.arch : " + System.getProperty("os.arch"));
System.out.println("os.version : " + System.getProperty("os.version"));
System.out.println("java.version : " + System.getProperty("java.version"));
System.out.println("java.vm.version : " + System.getProperty("java.vm.version"));
Main inst = new Main();
inst.test();
}
}
---------- END SOURCE ----------
- relates to
-
JDK-8073715 The channel of length 0 can overwrite with a locked channel
-
- Closed
-