Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
Buffer classes defined in the {{java.nio}} package, such as {{IntBuffer}}, {{CharBuffer}}, and {{ByteBuffer}},  define a variety of {{wrap()}} methods that wrap an array of somethe corresponding primitive data type into a buffer and return the buffer as a {{Buffer}} object. Although these methods create a new {{Buffer}} object, the new {{Buffer}} is backed by the same given input array. According to the Java API for these methods \[[API 2006|AA. Bibliography#API 06]\],

...

Exposing these buffers to untrusted code exposes the backing array to malicious modification. Likewise, the duplicate() methods create additional buffers that are backed by the original buffer's backing array; exposing such additional buffers to untrusted code affords the same opportunity for malicious modification of the contents of the original buffer's backing store.

Noncompliant Code Example (wrap())

...

Code Block
bgColor#FFCCCC
final class Wrap {
  private char[] dataArray;

  public Wrap () {
    dataArray = new char[10];
    // Initialize
  }

  public CharBuffer getBufferCopy() {
    return CharBuffer.wrap(dataArray);
  }
}

...

Code Block
bgColor#ccccff
final class Wrap {
  private char[] dataArray;

  public Wrap () {
    dataArray = new char[10];
    // Initialize
  }

  public CharBuffer getBufferCopy() {
    return CharBuffer.wrap(dataArray).asReadOnlyBuffer();
  }
}

...

Code Block
bgColor#ccccff
final class Wrap {
  private char[] dataArray;

  public Wrap () {
    dataArray = new char[10];
    // Initialize
  }

  public CharBuffer getBufferCopy() {
    CharBuffer cb = CharBuffer.allocate(dataArray.length);
    cb.put(dataArray);
    return cb;
  }
}

...

This noncompliant code example invokes the duplicate() method to create and return a copy of the CharBuffer. As stated in the contract for the duplicate() method, the returned buffer is backed by the same array as is the original buffer. Consequently, if a caller can were to modify the elements of the backing array; , these modifications would also affect the original buffer.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="26338bb367d50f95-aec7fedb-4e224014-8859b593-1624bd530bc1a547efabdd1f"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

class CharBuffer

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4b93bf5c17fbf257-e5eee393-42124227-9f358c01-cdcd3304c013a3d4183645de"><ac:plain-text-body><![CDATA[

[[Hitchens 2002

AA. Bibliography#Hitchens 02]]

2.3 Duplicating Buffers

]]></ac:plain-text-body></ac:structured-macro>

...