GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Auto-port 5.0: Don't loop forever in AbstractDiskHttpData when the fi… · netty/netty@074b1c6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http/multipart
      • AbstractDiskHttpData.java
    • test/java/io/netty/handler/codec/http/multipart
      • AbstractDiskHttpDataTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.netty.util.internal.logging.InternalLogger;
2424
import io.netty.util.internal.logging.InternalLoggerFactory;
2525

26+
import java.io.EOFException;
2627
import java.io.File;
2728
import java.io.IOException;
2829
import java.io.InputStream;
@@ -459,7 +460,11 @@ private static byte[] readFrom(File src) throws IOException {
459460
ByteBuffer byteBuffer = ByteBuffer.wrap(array);
460461
int read = 0;
461462
while (read < srcsize) {
462-
read += fileChannel.read(byteBuffer);
463+
int readnow = fileChannel.read(byteBuffer);
464+
if (readnow == -1) {
465+
throw new EOFException("Unexpected end of file after " + read + " of " + srcsize + " bytes");
466+
}
467+
read += readnow;
463468
}
464469
} finally {
465470
accessFile.close();
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpDataTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpDataTest.java
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@
1919
import io.netty.buffer.ByteBufUtil;
2020
import io.netty.util.internal.PlatformDependent;
2121
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.Timeout;
2223

24+
import java.io.EOFException;
2325
import java.io.File;
2426
import java.io.FileOutputStream;
2527
import java.nio.charset.Charset;
2628
import java.util.Arrays;
2729
import java.util.UUID;
2830
import java.util.concurrent.ThreadLocalRandom;
31+
import java.util.concurrent.TimeUnit;
2932

3033
import static io.netty.util.CharsetUtil.UTF_8;
3134
import static org.junit.jupiter.api.Assertions.assertEquals;
3235
import static org.junit.jupiter.api.Assertions.assertFalse;
36+
import static org.junit.jupiter.api.Assertions.assertThrows;
37+
import static org.junit.jupiter.api.Assertions.assertTrue;
3338

3439
/**
3540
* {@link AbstractDiskHttpData} test cases
@@ -65,6 +70,35 @@ public void testGetChunk() throws Exception {
6570
}
6671
}
6772

73+
@Test
74+
@Timeout(value = 10, unit = TimeUnit.SECONDS)
75+
public void testGetWhenFileIsShorterThanItsLength() throws Exception {
76+
TestHttpData test = new TestHttpData("test", UTF_8, 0);
77+
File tmpFile = PlatformDependent.createTempFile(UUID.randomUUID().toString(), ".tmp", null);
78+
try {
79+
FileOutputStream fos = new FileOutputStream(tmpFile);
80+
try {
81+
fos.write(new byte[16]);
82+
} finally {
83+
fos.close();
84+
}
85+
// Simulate a file that is truncated after its length was obtained.
86+
File shortFile = new File(tmpFile.getPath()) {
87+
@Override
88+
public long length() {
89+
return 32;
90+
}
91+
};
92+
test.setContent(shortFile);
93+
assertThrows(EOFException.class, test::get);
94+
assertThrows(EOFException.class, test::getByteBuf);
95+
assertThrows(EOFException.class, test::getString);
96+
} finally {
97+
test.delete();
98+
assertTrue(tmpFile.delete());
99+
}
100+
}
101+
68102
private static final class TestHttpData extends AbstractDiskHttpData {
69103

70104
private TestHttpData(String name, Charset charset, long size) {

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.