No public description

PiperOrigin-RevId: 782944922
Change-Id: I940c3df2660716c774252f3304f71916ac2d4aca
diff --git a/patches/02.input_stream_max_buffer_size.patch b/patches/02.input_stream_max_buffer_size.patch
new file mode 100644
index 0000000..55921b1
--- /dev/null
+++ b/patches/02.input_stream_max_buffer_size.patch
@@ -0,0 +1,51 @@
+See b/349666261, ResponseInputStream can produce OOM.
+===================================================================
+diff --git a/src/main/java/com/sun/mail/iap/ResponseInputStream.java b/src/main/java/com/sun/mail/iap/ResponseInputStream.java
+--- a/src/main/java/com/sun/mail/iap/ResponseInputStream.java	(revision cb7bd7e096d480731b4e28e1ba3ab42f2bc1826a)
++++ b/src/main/java/com/sun/mail/iap/ResponseInputStream.java	(date 1752488348737)
+@@ -58,6 +58,9 @@
+     private static final int maxIncrement = 256 * 1024;
+     private static final int incrementSlop = 16;
+ 
++    private final int inputStreamBufferMaxSize =
++        Integer.getInteger("com.sun.mail.iap.inputStreamBufferMaxSize", 0);
++
+     // where we read from
+     private BufferedInputStream bin;
+ 
+@@ -109,7 +112,7 @@
+ 		    int incr = buffer.length;
+ 		    if (incr > maxIncrement)
+ 			incr = maxIncrement;
+-		    ba.grow(incr);
++		    checkBeforeGrow(ba, incr);
+ 		    buffer = ba.getBytes();
+ 		}
+ 		buffer[idx++] = (byte)b;
+@@ -145,7 +148,7 @@
+ 		int avail = buffer.length - idx; // available space in buffer
+ 		if (count + incrementSlop > avail) {
+ 		    // need count-avail more bytes
+-		    ba.grow(minIncrement > count + incrementSlop - avail ? 
++		    checkBeforeGrow(ba, minIncrement > count + incrementSlop - avail ? 
+ 			    minIncrement : count + incrementSlop - avail);
+ 		    buffer = ba.getBytes();
+ 		}
+@@ -179,4 +182,15 @@
+     public int available() throws IOException {
+ 	return bin.available();
+     }
+-}
++
++		// TODO: b/349666261 - Push for a fix upstream, or deprecate sun_mail.
++		private void checkBeforeGrow(ByteArray ba, int incr) throws IOException {
++			if (inputStreamBufferMaxSize > 0 && ba.getBytes().length + incr > inputStreamBufferMaxSize) {
++				throw new IOException(
++						String.format(
++								"Buffer in ResponseInputStream grows too large. Limit: %d, actual: %d",
++								inputStreamBufferMaxSize, ba.getBytes().length + incr));
++			}
++			ba.grow(incr);
++		}
++}
+\ No newline at end of file
diff --git a/patches/series b/patches/series
index eb400ee..1f1878a 100644
--- a/patches/series
+++ b/patches/series
@@ -1 +1,2 @@
 01.unicode_check.patch
+02.input_stream_max_buffer_size.patch
\ No newline at end of file