summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorEtienne Bergeron2013-11-22 00:11:19 (EST)
committer Matthew Khouzam2013-11-26 11:06:17 (EST)
commitbd8571ca871dec4a7f48e0ad14adabab974b95cc (patch)
treef988839b3c11837b50703dc7fbc09af8fbfd9280
parent577699814c215e6e51a80884f310909a6a5019eb (diff)
downloadorg.eclipse.linuxtools-bd8571ca871dec4a7f48e0ad14adabab974b95cc.zip
org.eclipse.linuxtools-bd8571ca871dec4a7f48e0ad14adabab974b95cc.tar.gz
org.eclipse.linuxtools-bd8571ca871dec4a7f48e0ad14adabab974b95cc.tar.bz2
[ctf] Fix coding style issues in BitBuffer.refs/changes/07/18707/5
Fix potential issue with an undefined behavior in CTF. - Reading a integer of zero bit at the end of the buffer may be valid. - Change the behavior to accept it. Change-Id: I22fccc4e2ffee3bbf796e52b516b00f7fc5e31ac Signed-off-by: Etienne Bergeron <etienne.bergeron@gmail.com> Reviewed-on: https://git.eclipse.org/r/18707 Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Tested-by: Hudson CI Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im> IP-Clean: Alexandre Montplaisir <alexmonthy@voxpopuli.im> Tested-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
-rw-r--r--lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/BitBufferIntTest.java2
-rw-r--r--lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/io/BitBuffer.java53
2 files changed, 31 insertions, 24 deletions
diff --git a/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/BitBufferIntTest.java b/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/BitBufferIntTest.java
index 2bf4399..81811cb 100644
--- a/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/BitBufferIntTest.java
+++ b/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/BitBufferIntTest.java
@@ -21,7 +21,7 @@ import org.junit.Before;
import org.junit.Test;
/**
- * Part of the BitBuffet tests with test the methods to read/write integers.
+ * Part of the BitBuffer tests which test the methods to read/write integers.
* These are separated from the main file because the fixture is different.
*
* @author alexmont
diff --git a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/io/BitBuffer.java b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/io/BitBuffer.java
index 1b75d93..d97eb5f 100644
--- a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/io/BitBuffer.java
+++ b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/io/BitBuffer.java
@@ -9,6 +9,7 @@
* Contributors: Matthew Khouzam - Initial Design and implementation
* Contributors: Francis Giraldeau - Initial API and implementation
* Contributors: Philippe Proulx - Some refinement and optimization
+ * Contributors: Etienne Bergeron <Etienne.Bergeron@gmail.com> - fix zero size read + cleanup
*******************************************************************************/
package org.eclipse.linuxtools.ctf.core.event.io;
@@ -53,14 +54,14 @@ public final class BitBuffer {
// Constructors
// ------------------------------------------------------------------------
/**
- * Default constructor, makes a bigendian buffer
+ * Default constructor, makes a big-endian buffer
*/
public BitBuffer() {
this(null, ByteOrder.BIG_ENDIAN);
}
/**
- * Constructor, makes a bigendian buffer
+ * Constructor, makes a big-endian buffer
*
* @param buf
* the bytebuffer to read
@@ -70,12 +71,12 @@ public final class BitBuffer {
}
/**
- * Constructor that is fully parametrisable
+ * Constructor that is fully parameterizable
*
* @param buf
* the buffer to read
* @param order
- * the byte order (big endian, little endian, network?)
+ * the byte order (big-endian, little-endian, network?)
*/
public BitBuffer(ByteBuffer buf, ByteOrder order) {
setByteBuffer(buf);
@@ -113,35 +114,39 @@ public final class BitBuffer {
* @return The int value read from the buffer
*/
public int getInt(int length, boolean signed) {
- int val = 0;
- if (!canRead(length)) {
- throw new BufferOverflowException();
- }
+
+ /* Nothing to read. */
if (length == 0) {
return 0;
}
+
+ /* Validate that the buffer has enough bits. */
+ if (!canRead(length)) {
+ throw new BufferOverflowException();
+ }
+
+ /* Get the value from the byte buffer. */
+ int val = 0;
boolean gotIt = false;
- // Fall back to fast ByteBuffer reader if we want to read byte-aligned bytes
+ /* Try a fast read when the position is byte-aligned by using the */
+ /* native methods of ByteBuffer. */
if (this.pos % BitBuffer.BIT_CHAR == 0) {
switch (length) {
case BitBuffer.BIT_CHAR:
// Byte
- if (signed) {
- val = this.buf.get((int) (this.pos / 8));
- } else {
- val = (this.buf.get((int) (this.pos / 8))) & 0xff;
+ val = this.buf.get((int) (this.pos / 8));
+ if (!signed) {
+ val = val & 0xff;
}
gotIt = true;
break;
case BitBuffer.BIT_SHORT:
// Word
- if (signed) {
- val = this.buf.getShort((int) (this.pos / 8));
- } else {
- short a = this.buf.getShort((int) (this.pos / 8));
- val = a & 0xffff;
+ val = this.buf.getShort((int) (this.pos / 8));
+ if (!signed) {
+ val = val & 0xffff;
}
gotIt = true;
break;
@@ -156,6 +161,8 @@ public final class BitBuffer {
break;
}
}
+
+ /* When not byte-aligned, fall-back to a general decoder. */
if (!gotIt) {
// Nothing read yet: use longer methods
if (this.byteOrder == ByteOrder.LITTLE_ENDIAN) {
@@ -349,8 +356,8 @@ public final class BitBuffer {
}
cmask = correctedValue << lshift;
/*
- * low bits are cleared because of lshift and high bits are already
- * cleared
+ * low bits are cleared because of left-shift and high bits are
+ * already cleared
*/
cmask &= ~mask;
int b = this.buf.get(startByte) & 0xFF;
@@ -414,8 +421,8 @@ public final class BitBuffer {
}
cmask = correctedValue << lshift;
/*
- * low bits are cleared because of lshift and high bits are already
- * cleared
+ * low bits are cleared because of left-shift and high bits are
+ * already cleared
*/
cmask &= ~mask;
int b = this.buf.get(startByte) & 0xFF;
@@ -540,7 +547,7 @@ public final class BitBuffer {
}
/**
- * resets the bitbuffer.
+ * Resets the bitbuffer.
*/
public void clear() {
position(0);