Skip to content

Commit 81ca0ec

Browse files
committed
8334028: HttpClient: NPE thrown from assert statement
Reviewed-by: jpai
1 parent bd750b6 commit 81ca0ec

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/java.net.http/share/classes/jdk/internal/net/http/ResponseSubscribers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2024, 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
@@ -356,6 +356,7 @@ public void onNext(List<ByteBuffer> items) {
356356
// incoming buffers are allocated by http client internally,
357357
// and won't be used anywhere except this place.
358358
// So it's free simply to store them for further processing.
359+
Objects.requireNonNull(items); // ensure NPE is thrown before assert
359360
assert Utils.hasRemaining(items);
360361
received.addAll(items);
361362
}

src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ public static String stringOf(Collection<?> source) {
746746
}
747747

748748
public static long remaining(ByteBuffer[] bufs) {
749+
if (bufs == null) return 0;
749750
long remain = 0;
750751
for (ByteBuffer buf : bufs) {
751752
remain += buf.remaining();
@@ -754,6 +755,7 @@ public static long remaining(ByteBuffer[] bufs) {
754755
}
755756

756757
public static boolean hasRemaining(List<ByteBuffer> bufs) {
758+
if (bufs == null) return false;
757759
for (ByteBuffer buf : bufs) {
758760
if (buf.hasRemaining())
759761
return true;
@@ -762,6 +764,7 @@ public static boolean hasRemaining(List<ByteBuffer> bufs) {
762764
}
763765

764766
public static boolean hasRemaining(ByteBuffer[] bufs) {
767+
if (bufs == null) return false;
765768
for (ByteBuffer buf : bufs) {
766769
if (buf.hasRemaining())
767770
return true;
@@ -770,6 +773,7 @@ public static boolean hasRemaining(ByteBuffer[] bufs) {
770773
}
771774

772775
public static long remaining(List<ByteBuffer> bufs) {
776+
if (bufs == null) return 0L;
773777
long remain = 0;
774778
for (ByteBuffer buf : bufs) {
775779
remain += buf.remaining();
@@ -778,12 +782,14 @@ public static long remaining(List<ByteBuffer> bufs) {
778782
}
779783

780784
public static long synchronizedRemaining(List<ByteBuffer> bufs) {
785+
if (bufs == null) return 0L;
781786
synchronized (bufs) {
782787
return remaining(bufs);
783788
}
784789
}
785790

786-
public static int remaining(List<ByteBuffer> bufs, int max) {
791+
public static long remaining(List<ByteBuffer> bufs, long max) {
792+
if (bufs == null) return 0;
787793
long remain = 0;
788794
for (ByteBuffer buf : bufs) {
789795
remain += buf.remaining();
@@ -794,7 +800,13 @@ public static int remaining(List<ByteBuffer> bufs, int max) {
794800
return (int) remain;
795801
}
796802

797-
public static int remaining(ByteBuffer[] refs, int max) {
803+
public static int remaining(List<ByteBuffer> bufs, int max) {
804+
// safe cast since max is an int
805+
return (int) remaining(bufs, (long) max);
806+
}
807+
808+
public static long remaining(ByteBuffer[] refs, long max) {
809+
if (refs == null) return 0;
798810
long remain = 0;
799811
for (ByteBuffer b : refs) {
800812
remain += b.remaining();
@@ -805,6 +817,11 @@ public static int remaining(ByteBuffer[] refs, int max) {
805817
return (int) remain;
806818
}
807819

820+
public static int remaining(ByteBuffer[] refs, int max) {
821+
// safe cast since max is an int
822+
return (int) remaining(refs, (long) max);
823+
}
824+
808825
public static void close(Closeable... closeables) {
809826
for (Closeable c : closeables) {
810827
try {

test/jdk/java/net/httpclient/BodySubscribersTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/*
2525
* @test
2626
* @summary Basic test for the standard BodySubscribers default behavior
27-
* @bug 8225583
27+
* @bug 8225583 8334028
2828
* @run testng BodySubscribersTest
2929
*/
3030

0 commit comments

Comments
 (0)