Fix up line number handling to be unsigned int safe

This commit is contained in:
Ben Gruver
2013-05-03 19:35:47 -07:00
parent 6fc32629c2
commit d38f3a7983
6 changed files with 129 additions and 3 deletions

View File

@ -128,6 +128,46 @@ public class BaseDexReader<T extends BaseDexBuffer> {
return result;
}
/**
* Reads a "big" uleb128 that can legitimately be > 2^31. The value is returned as a signed integer, with the
* expected semantics of re-interpreting an unsigned value as a signed value.
*
* @return The unsigned value, reinterpreted as a signed int
*/
public int readBigUleb128() {
int end = offset;
int currentByteValue;
int result;
byte[] buf = dexBuf.buf;
result = buf[end++] & 0xff;
if (result > 0x7f) {
currentByteValue = buf[end++] & 0xff;
result = (result & 0x7f) | ((currentByteValue & 0x7f) << 7);
if (currentByteValue > 0x7f) {
currentByteValue = buf[end++] & 0xff;
result |= (currentByteValue & 0x7f) << 14;
if (currentByteValue > 0x7f) {
currentByteValue = buf[end++] & 0xff;
result |= (currentByteValue & 0x7f) << 21;
if (currentByteValue > 0x7f) {
currentByteValue = buf[end++];
// MSB shouldn't be set on last byte
if (currentByteValue < 0) {
throw new ExceptionWithContext(
"Invalid uleb128 integer encountered at offset 0x%x", offset);
}
result |= currentByteValue << 28;
}
}
}
}
offset = end;
return result;
}
public void skipUleb128() {
int end = offset;
byte currentByteValue;

View File

@ -99,8 +99,7 @@ public abstract class DebugInfo implements Iterable<DebugItem> {
@Override
public Iterator<DebugItem> iterator() {
DexReader reader = dexFile.readerAt(debugInfoOffset);
// TODO: this unsigned value could legitimally be > MAX_INT
final int lineNumberStart = reader.readSmallUleb128();
final int lineNumberStart = reader.readBigUleb128();
int registerCount = methodImpl.getRegisterCount();
//TODO: does dalvik allow references to invalid registers?

View File

@ -32,5 +32,11 @@
package org.jf.dexlib2.iface.debug;
public interface LineNumber extends DebugItem {
/**
* The line number associated with this code address. This value should be treated as an unsigned integer, with
* negative values indicating a value > 2^31, using the usual two's complement semantics.
*
* @return The line number associated with this code address.
*/
int getLineNumber();
}