# HG changeset patch # User dfuchs # Date 1565286251 -3600 # Node ID e4cc5231ce2d98c84261f81d80a6c8dbc632800b # Parent 1cf884e437eac67dbae8114ca540d050c5497114 8228970: AssertionError in ResponseSubscribers$HttpResponseInputStream Summary: HttpResponseInputStream::read(byte[],int,int) now implements the same check than the InputStream::read(byte[],int,int). Reviewed-by: prappo, chegar, dfuchs Contributed-by: Patrick Concannon diff -r 1cf884e437ea -r e4cc5231ce2d src/java.net.http/share/classes/jdk/internal/net/http/ResponseSubscribers.java --- a/src/java.net.http/share/classes/jdk/internal/net/http/ResponseSubscribers.java Thu Aug 08 04:29:56 2019 -0700 +++ b/src/java.net.http/share/classes/jdk/internal/net/http/ResponseSubscribers.java Thu Aug 08 18:44:11 2019 +0100 @@ -438,6 +438,10 @@ @Override public int read(byte[] bytes, int off, int len) throws IOException { + Objects.checkFromIndexSize(off, len, bytes.length); + if (len == 0) { + return 0; + } // get the buffer to read from, possibly blocking if // none is available ByteBuffer buffer; diff -r 1cf884e437ea -r e4cc5231ce2d test/jdk/java/net/httpclient/HttpResponseInputStreamTest.java --- a/test/jdk/java/net/httpclient/HttpResponseInputStreamTest.java Thu Aug 08 04:29:56 2019 -0700 +++ b/test/jdk/java/net/httpclient/HttpResponseInputStreamTest.java Thu Aug 08 18:44:11 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -40,13 +40,18 @@ import org.testng.annotations.Test; +import static org.testng.Assert.*; + /* * @test + * @bug 8197564 8228970 * @summary Simple smoke test for BodySubscriber.asInputStream(); * @run testng/othervm HttpResponseInputStreamTest * @author daniel fuchs */ public class HttpResponseInputStreamTest { + static final Class NPE = NullPointerException.class; + static final Class OOB = IndexOutOfBoundsException.class; static class TestException extends IOException {} @@ -184,6 +189,33 @@ } @Test + public static void testReadParameters() throws InterruptedException, ExecutionException, IOException { + BodySubscriber isb = BodySubscribers.ofInputStream(); + InputStream is = isb.getBody().toCompletableFuture().get(); + + Throwable ex; + + // len == 0 + assertEquals(is.read(new byte[16], 0, 0), 0); + assertEquals(is.read(new byte[16], 16, 0), 0); + + // index == -1 + ex = expectThrows(OOB, () -> is.read(new byte[16], -1, 10)); + System.out.println("OutOfBoundsException thrown as expected: " + ex); + + // large offset + ex = expectThrows(OOB, () -> is.read(new byte[16], 17, 10)); + System.out.println("OutOfBoundsException thrown as expected: " + ex); + + ex = expectThrows(OOB, () -> is.read(new byte[16], 10, 10)); + System.out.println("OutOfBoundsException thrown as expected: " + ex); + + // null value + ex = expectThrows(NPE, () -> is.read(null, 0, 10)); + System.out.println("NullPointerException thrown as expected: " + ex); + } + + @Test public static void testSubscribeAndClose() throws InterruptedException, ExecutionException {