From 7570fe03829e1896bdc4c6c75a24de93990245da Mon Sep 17 00:00:00 2001 From: "JesusFreke@JesusFreke.com" Date: Wed, 23 Dec 2009 05:27:23 +0000 Subject: [PATCH] Fix the way offsets are handled in Instruction21t and Instruction22t Use a short internally for the offset, and validate the value in updateOffset() git-svn-id: https://smali.googlecode.com/svn/trunk@505 55b6fa8a-2a1e-11de-a435-ffa8d773f76a --- .../org/jf/dexlib/Code/Format/Instruction21t.java | 14 ++++++++------ .../org/jf/dexlib/Code/Format/Instruction22t.java | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21t.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21t.java index f0dd11aa..a8f9f725 100644 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21t.java +++ b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21t.java @@ -38,7 +38,7 @@ import org.jf.dexlib.Util.AnnotatedOutput; public class Instruction21t extends Instruction implements OffsetInstruction { public static final Instruction.InstructionFactory Factory = new Factory(); private byte regA; - private int offset; + private short offset; public Instruction21t(Opcode opcode, short regA, short offB) { super(opcode); @@ -66,17 +66,19 @@ public class Instruction21t extends Instruction implements OffsetInstruction { } protected void writeInstruction(AnnotatedOutput out, int currentCodeOffset) { - if (offset < -32768 || offset > 32767) { - throw new RuntimeException("The offset " + offset + " is out of range. It must be in [-32768, 32767]"); - } - out.writeByte(opcode.value); out.writeByte(regA); out.writeShort(offset); } public void updateOffset(int offset) { - this.offset = offset; + if (offset < Short.MIN_VALUE || offset > Short.MAX_VALUE) { + throw new RuntimeException("The offset " + offset + " is out of range. It must be in [-32768, 32767]"); + } + if (offset == 0) { + throw new RuntimeException("The offset cannot be 0"); + } + this.offset = (short)offset; } public Format getFormat() { diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22t.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22t.java index 7fa8372e..4a755787 100644 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22t.java +++ b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22t.java @@ -40,7 +40,7 @@ public class Instruction22t extends Instruction implements OffsetInstruction, Tw public static final Instruction.InstructionFactory Factory = new Factory(); private byte regA; private byte regB; - private int offset; + private short offset; public Instruction22t(Opcode opcode, byte regA, byte regB, short offC) { super(opcode); @@ -72,17 +72,19 @@ public class Instruction22t extends Instruction implements OffsetInstruction, Tw } protected void writeInstruction(AnnotatedOutput out, int currentCodeOffset) { - if (offset < -32768 || offset > 32767) { - throw new RuntimeException("The offset " + offset + " is out of range. It must be in [-32768, 32767]"); - } - out.writeByte(opcode.value); out.writeByte((regB << 4) | regA); out.writeShort(offset); } public void updateOffset(int offset) { - this.offset = offset; + if (offset < -32768 || offset > 32767) { + throw new RuntimeException("The offset " + offset + " is out of range. It must be in [-32768, 32767]"); + } + if (offset == 0) { + throw new RuntimeException("The offset cannot be 0"); + } + this.offset = (short)offset; } public Format getFormat() {