From 81ca0ece2e69477b3d22a40c51e044d56e13b6e4 Mon Sep 17 00:00:00 2001 From: Daniel Fuchs Date: Wed, 12 Jun 2024 10:53:08 +0000 Subject: [PATCH] 8334028: HttpClient: NPE thrown from assert statement Reviewed-by: jpai --- .../net/http/ResponseSubscribers.java | 3 ++- .../jdk/internal/net/http/common/Utils.java | 21 +++++++++++++++++-- .../net/httpclient/BodySubscribersTest.java | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/java.net.http/share/classes/jdk/internal/net/http/ResponseSubscribers.java b/src/java.net.http/share/classes/jdk/internal/net/http/ResponseSubscribers.java index ed95f815913..09ad87f9205 100644 --- a/src/java.net.http/share/classes/jdk/internal/net/http/ResponseSubscribers.java +++ b/src/java.net.http/share/classes/jdk/internal/net/http/ResponseSubscribers.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2024, 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 @@ -356,6 +356,7 @@ public class ResponseSubscribers { // incoming buffers are allocated by http client internally, // and won't be used anywhere except this place. // So it's free simply to store them for further processing. + Objects.requireNonNull(items); // ensure NPE is thrown before assert assert Utils.hasRemaining(items); received.addAll(items); } diff --git a/src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java b/src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java index 9bd7f350286..b2eb570db0c 100644 --- a/src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java +++ b/src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java @@ -746,6 +746,7 @@ public final class Utils { } public static long remaining(ByteBuffer[] bufs) { + if (bufs == null) return 0; long remain = 0; for (ByteBuffer buf : bufs) { remain += buf.remaining(); @@ -754,6 +755,7 @@ public final class Utils { } public static boolean hasRemaining(List bufs) { + if (bufs == null) return false; for (ByteBuffer buf : bufs) { if (buf.hasRemaining()) return true; @@ -762,6 +764,7 @@ public final class Utils { } public static boolean hasRemaining(ByteBuffer[] bufs) { + if (bufs == null) return false; for (ByteBuffer buf : bufs) { if (buf.hasRemaining()) return true; @@ -770,6 +773,7 @@ public final class Utils { } public static long remaining(List bufs) { + if (bufs == null) return 0L; long remain = 0; for (ByteBuffer buf : bufs) { remain += buf.remaining(); @@ -778,12 +782,14 @@ public final class Utils { } public static long synchronizedRemaining(List bufs) { + if (bufs == null) return 0L; synchronized (bufs) { return remaining(bufs); } } - public static int remaining(List bufs, int max) { + public static long remaining(List bufs, long max) { + if (bufs == null) return 0; long remain = 0; for (ByteBuffer buf : bufs) { remain += buf.remaining(); @@ -794,7 +800,13 @@ public final class Utils { return (int) remain; } - public static int remaining(ByteBuffer[] refs, int max) { + public static int remaining(List bufs, int max) { + // safe cast since max is an int + return (int) remaining(bufs, (long) max); + } + + public static long remaining(ByteBuffer[] refs, long max) { + if (refs == null) return 0; long remain = 0; for (ByteBuffer b : refs) { remain += b.remaining(); @@ -805,6 +817,11 @@ public final class Utils { return (int) remain; } + public static int remaining(ByteBuffer[] refs, int max) { + // safe cast since max is an int + return (int) remaining(refs, (long) max); + } + public static void close(Closeable... closeables) { for (Closeable c : closeables) { try { diff --git a/test/jdk/java/net/httpclient/BodySubscribersTest.java b/test/jdk/java/net/httpclient/BodySubscribersTest.java index 508cc95abb0..5ee7ed4ef62 100644 --- a/test/jdk/java/net/httpclient/BodySubscribersTest.java +++ b/test/jdk/java/net/httpclient/BodySubscribersTest.java @@ -24,7 +24,7 @@ /* * @test * @summary Basic test for the standard BodySubscribers default behavior - * @bug 8225583 + * @bug 8225583 8334028 * @run testng BodySubscribersTest */