diff --git a/robocode.host/src/main/java/net/sf/robocode/host/io/RobotFileOutputStream.java b/robocode.host/src/main/java/net/sf/robocode/host/io/RobotFileOutputStream.java index 9dc31c147..3b80ee055 100644 --- a/robocode.host/src/main/java/net/sf/robocode/host/io/RobotFileOutputStream.java +++ b/robocode.host/src/main/java/net/sf/robocode/host/io/RobotFileOutputStream.java @@ -46,16 +46,24 @@ public final void write(byte[] b) throws IOException { @Override public final void write(byte[] b, int off, int len) throws IOException { - if (len < 0) { - throw new IndexOutOfBoundsException(); + // Sanity check + if (b == null) { + throw new NullPointerException(); } + + // Comprehensive bounds checking to prevent integer overflow + if (off < 0 || len < 0 || len > b.length || off > b.length - len) { + throw new ArrayIndexOutOfBoundsException(); + } + try { fileSystemManager.checkQuota(len); super.write(b, off, len); } catch (IOException e) { try { close(); - } catch (IOException ignored) {} + } catch (IOException ignore) { + } throw e; } }