Jiangwei Liu created IO-779:
-------------------------------

             Summary: IOUtils.byteArray(size) should add the verification to 
assure that the size is legal(size > 0), the illegal(size <=0) should throw 
IllegalArgumentException.
                 Key: IO-779
                 URL: https://issues.apache.org/jira/browse/IO-779
             Project: Commons IO
          Issue Type: Bug
          Components: Utilities
    Affects Versions: 2.11.0
         Environment: windows 10
            Reporter: Jiangwei Liu


IOUtils.byteArray(size) should add the verification to assure that the size is 
legal(size > 0), the illegal(size <=0) should throw IllegalArgumentException.
for an example:
IOUtils.byteArray(-1);
should throw java.lang.NegativeArraySizeException。

 

fixed code:

/**
* Returns a new byte array of the given size.
*
* TODO Consider guarding or warning against large allocations...
*
* @param size array size.
* @return a new byte array of the given size.
*
* @exception IllegalArgumentException If \{@code size <= 0}
*
* @since 2.9.0
*/
public static byte[] byteArray(final int size) {
if (size <= 0) {
throw new IllegalArgumentException("byte size <= 0");
}
return new byte[size];
}

 

test code:IOUtilsTest.java

@Test
public void testByteArrayWithIllegalSize() {
try {
int size = -1;
byte[] bytes = IOUtils.byteArray(size);

size = 0;
bytes = IOUtils.byteArray(size);
}catch (Exception e) {
assertEquals(e.getClass().getName(), IllegalArgumentException.class.getName());
}

}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to