[ 
https://issues.apache.org/jira/browse/IO-429?focusedWorklogId=533682&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-533682
 ]

ASF GitHub Bot logged work on IO-429:
-------------------------------------

                Author: ASF GitHub Bot
            Created on: 10/Jan/21 04:08
            Start Date: 10/Jan/21 04:08
    Worklog Time Spent: 10m 
      Work Description: garydgregory commented on a change in pull request #175:
URL: https://github.com/apache/commons-io/pull/175#discussion_r544754329



##########
File path: pom.xml
##########
@@ -384,7 +384,7 @@ file comparators, endian transformation classes, and much 
more.
           <forkCount>1</forkCount>
           <reuseForks>false</reuseForks>
           <!-- limit memory size see IO-161 -->
-          <argLine>${argLine} -Xmx25M</argLine>
+          <argLine>${argLine} -Xmx4223M</argLine>

Review comment:
       Not acceptable, leave as is for now.

##########
File path: src/main/java/org/apache/commons/io/IOUtils.java
##########
@@ -2243,10 +2243,13 @@ public static BufferedReader toBufferedReader(final 
Reader reader, final int siz
      * @param input the <code>InputStream</code> to read from
      * @return the requested byte array
      * @throws IOException          if an I/O error occurs
+     * @throws IllegalArgumentException if input is longer than the maximum 
Java array length
      */
     public static byte[] toByteArray(final InputStream input) throws 
IOException {
         try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) 
{
-            copy(input, output);
+            if (copy(input, output) == -1) {
+                throw new IllegalArgumentException("Stream cannot be longer 
than Integer max value bytes");

Review comment:
       @leskin-in 
   This condition will not happen for a `ByteArrayInputStream`, instead of 
you'll get a `IndexOutOfBoundsException` or am I missing something?

##########
File path: src/test/java/org/apache/commons/io/IOUtilsTestCase.java
##########
@@ -1331,6 +1332,15 @@ public void testCopyLarge_SkipWithInvalidOffset() throws 
IOException {
         }
     }
 
+    @Test public void testToByteArray_InputStreamTooLong() throws Exception {
+        try (CircularInputStream cin = new CircularInputStream(new byte[]{65, 
65, 65}, ((long)Integer.MAX_VALUE) + 1L)) {
+            IOUtils.toByteArray(cin);
+            fail("IllegalArgumentException expected");
+        } catch (final IllegalArgumentException exc) {

Review comment:
       Use assertThrows()




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 533682)
    Time Spent: 1h  (was: 50m)

> ByteArrayOutputStream can overflow
> ----------------------------------
>
>                 Key: IO-429
>                 URL: https://issues.apache.org/jira/browse/IO-429
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Utilities
>            Reporter: Fabian Lange
>            Priority: Major
>          Time Spent: 1h
>  Remaining Estimate: 0h
>
> There are many places involved in the problem, and a good fix might be 
> problematic performance wise.
> For example:
> IOUtils.toByteArray(InputStream input) invoked with a Stream which feeds more 
> than Integer.MAX_VALUE bytes will either crash with 
> NegativeArraySizeException or maybe worse overflow in such a way that it 
> returns fine (but only with partial data)
> The ByteArrayOutputStream will happily consume the full stream but "int 
> count" will overflow. At some point then toByteArray is invoked which will do 
> like new byte[count].
> maybe "needNewBuffer" can throw the IllegalArgumentException, as it gets  the 
> count and could check for the overflow.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to