Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
More string checking of long argument casted to in RrdByteArrayBacken…
…d and RrdNioBackend.
  • Loading branch information
Fabrice Bacchella committed Nov 10, 2018
1 parent 06d2623 commit da342f5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/main/java/org/rrd4j/core/RrdByteArrayBackend.java
Expand Up @@ -24,8 +24,13 @@ protected RrdByteArrayBackend(String path) {
* @param offset a long.
* @param bytes an array of byte.
* @throws java.io.IOException if any.
* @throws java.lang.IllegalArgumentException if offset is bigger that the possible length.
*/
protected synchronized void write(long offset, byte[] bytes) throws IOException {
if (offset > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Illegal offset: " + offset);
}

int pos = (int) offset;
System.arraycopy(bytes, 0, buffer, pos, bytes.length);
}
Expand All @@ -36,21 +41,25 @@ protected synchronized void write(long offset, byte[] bytes) throws IOException
* @param offset a long.
* @param bytes an array of byte.
* @throws java.io.IOException if any.
* @throws java.lang.IllegalArgumentException if offset is bigger that the possible length.
*/
protected synchronized void read(long offset, byte[] bytes) throws IOException {
int pos = (int) offset;
if (pos + bytes.length <= buffer.length) {
System.arraycopy(buffer, pos, bytes, 0, bytes.length);
if (offset < 0 || offset > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Illegal offset: " + offset);
}

if (offset + bytes.length <= buffer.length) {
System.arraycopy(buffer, (int) offset, bytes, 0, bytes.length);
}
else {
throw new IOException("Not enough bytes available in memory; RRD " + getPath());
}
}

/**
* Returns the number of RRD bytes held in memory.
* {@inheritDoc}
*
* @return Number of all RRD bytes.
* @return Number of RRD bytes held in memory.
*/
public long getLength() {
return buffer.length;
Expand All @@ -59,11 +68,13 @@ public long getLength() {
/**
* {@inheritDoc}
*
* Reserves a memory section as a RRD storage.
* <p>It will reserves a memory section as a RRD storage.</p>
*
* @throws java.lang.IllegalArgumentException if length is bigger that the possible length.
*/
protected void setLength(long length) throws IOException {
if (length > Integer.MAX_VALUE) {
throw new IOException("Illegal length: " + length);
if (length < 0 || length > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Illegal length: " + length);
}

buffer = new byte[(int) length];
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/rrd4j/core/RrdNioBackend.java
Expand Up @@ -136,8 +136,13 @@ private void unmapFile() {
*
* Sets length of the underlying RRD file. This method is called only once, immediately
* after a new RRD file gets created.
* @throws java.lang.IllegalArgumentException if the length is bigger that the possible mapping position (2GiB).
*/
protected synchronized void setLength(long newLength) throws IOException {
if (newLength < 0 || newLength > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Illegal offset: " + newLength);
}

unmapFile();
super.setLength(newLength);
mapFile();
Expand All @@ -149,8 +154,13 @@ protected synchronized void setLength(long newLength) throws IOException {
* @param offset Starting file offset
* @param b Bytes to be written.
* @throws java.io.IOException if any.
* @throws java.lang.IllegalArgumentException if offset is bigger that the possible mapping position (2GiB).
*/
protected synchronized void write(long offset, byte[] b) throws IOException {
if (offset < 0 || offset > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Illegal offset: " + offset);
}

if (byteBuffer != null) {
byteBuffer.position((int) offset);
byteBuffer.put(b);
Expand All @@ -166,8 +176,13 @@ protected synchronized void write(long offset, byte[] b) throws IOException {
* @param offset Starting file offset
* @param b Buffer which receives bytes read from the file.
* @throws java.io.IOException Thrown in case of I/O error.
* @throws java.lang.IllegalArgumentException if offset is bigger that the possible mapping position (2GiB).
*/
protected synchronized void read(long offset, byte[] b) throws IOException {
if (offset < 0 || offset > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Illegal offset: " + offset);
}

if (byteBuffer != null) {
byteBuffer.position((int) offset);
byteBuffer.get(b);
Expand Down

0 comments on commit da342f5

Please sign in to comment.