From 068b9d5948e4e4d2762095873d16328cea1953e8 Mon Sep 17 00:00:00 2001 From: Roger Riggs Date: Mon, 22 Jan 2024 14:32:38 -0500 Subject: [PATCH] When handling an OutOfMemoryError due to off-heap allocation failure use String.concat() instead of string format concatenation that does code generation. --- .../classes/java/io/ObjectInputStream.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/java.base/share/classes/java/io/ObjectInputStream.java b/src/java.base/share/classes/java/io/ObjectInputStream.java index a803969df5acd..9b31944d831be 100644 --- a/src/java.base/share/classes/java/io/ObjectInputStream.java +++ b/src/java.base/share/classes/java/io/ObjectInputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 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 @@ -1987,9 +1987,8 @@ private ObjectStreamClass readProxyDesc(boolean unshared) resolveEx = ex; } catch (IllegalAccessError aie) { throw new InvalidClassException(aie.getMessage(), aie); - } catch (OutOfMemoryError memerr) { - throw new InvalidObjectException("Proxy interface limit exceeded: " + - Arrays.toString(ifaces), memerr); + } catch (OutOfMemoryError oome) { + throw genInvalidObjectException(oome, ifaces); } // Call filterCheck on the class before reading anything else @@ -2001,9 +2000,8 @@ private ObjectStreamClass readProxyDesc(boolean unshared) totalObjectRefs++; depth++; desc.initProxy(cl, resolveEx, readClassDesc(false)); - } catch (OutOfMemoryError memerr) { - throw new InvalidObjectException("Proxy interface limit exceeded: " + - Arrays.toString(ifaces), memerr); + } catch (OutOfMemoryError oome) { + throw genInvalidObjectException(oome, ifaces); } finally { depth--; } @@ -2013,6 +2011,13 @@ private ObjectStreamClass readProxyDesc(boolean unshared) return desc; } + // Generate an InvalidObjectException for an OutOfMemoryError + // Use String.concat() to avoid string formatting code generation + private static InvalidObjectException genInvalidObjectException(OutOfMemoryError oome, String[] ifaces) { + return new InvalidObjectException("Proxy interface limit exceeded: " + .concat(Arrays.toString(ifaces)), oome); + } + /** * Reads in and returns class descriptor for a class that is not a dynamic * proxy class. Sets passHandle to class descriptor's assigned handle. If