Skip to content

Commit

Permalink
Adds hex dump length safety check
Browse files Browse the repository at this point in the history
jon-valliere committed Apr 15, 2021

Verified

This commit was signed with the committer’s verified signature.
schlosna David Schlosnagle
1 parent 04d121f commit 01e0497
Showing 3 changed files with 5 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1545,8 +1545,8 @@ public String getHexDump(int length) {
* @return hexidecimal representation of this buffer
*/
public String getHexDump(int length, boolean pretty) {
return (pretty) ? IoBufferHexDumper.getPrettyHexDumpSlice(this, this.position(), length)
: IoBufferHexDumper.getHexDumpSlice(this, this.position(), length);
return (pretty) ? IoBufferHexDumper.getPrettyHexDumpSlice(this, this.position(), Math.min(this.remaining(), length))
: IoBufferHexDumper.getHexDumpSlice(this, this.position(), Math.min(this.remaining(), length));
}

// //////////////////////////////
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@
package org.apache.mina.core.buffer;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
* Provides utility methods to dump an {@link IoBuffer} into a hex formatted
Original file line number Diff line number Diff line change
@@ -16,10 +16,6 @@ public void checkHexDumpLength() {

buf.flip();

// System.out.println(buf.getHexDump());
// System.out.println(buf.getHexDump(20));
// System.out.println(buf.getHexDump(50));

/* special case */
assertEquals(0, buf.getHexDump(0).length());

@@ -28,9 +24,9 @@ public void checkHexDumpLength() {
assertEquals((Math.min(300, buf.limit()) * 3) - 1, buf.getHexDump(300).length());

/* must truncate */
assertEquals((7 * 3) + 2, buf.getHexDump(7).length());
assertEquals((10 * 3) + 2, buf.getHexDump(10).length());
assertEquals((30 * 3) + 2, buf.getHexDump(30).length());
assertEquals((7 * 3) - 1, buf.getHexDump(7).length());
assertEquals((10 * 3) - 1, buf.getHexDump(10).length());
assertEquals((30 * 3) - 1, buf.getHexDump(30).length());

}

@@ -44,15 +40,7 @@ public void checkPrettyHexDumpLength() {

buf.flip();

// System.out.println(buf.getHexDump(0, true));
// System.out.println(buf.getHexDump(20, true));
// System.out.println(buf.getHexDump(50, true));

String[] dump = buf.getHexDump(50, true).split("\\n");

for (String x : dump) {
System.out.println(x);
}

assertEquals(4, dump.length);
}

0 comments on commit 01e0497

Please sign in to comment.