Merge "Remove spurious long to int type casting in ApfFilter.java" into main
diff --git a/src/android/net/apf/ApfFilter.java b/src/android/net/apf/ApfFilter.java
index ccb2b55..5fe19fd 100644
--- a/src/android/net/apf/ApfFilter.java
+++ b/src/android/net/apf/ApfFilter.java
@@ -598,6 +598,10 @@
if (type == Type.MATCH && (lifetime != 0 || min != 0)) {
throw new IllegalArgumentException("lifetime, min must be 0 for MATCH sections");
}
+ // Clamp the lifetime to INT_MAX to prevent it from ever going negative.
+ if (lifetime > Integer.MAX_VALUE) {
+ lifetime = Integer.MAX_VALUE;
+ }
this.lifetime = lifetime;
// It has already been asserted that min is 0 for MATCH sections.
diff --git a/src/android/net/apf/ApfV4Generator.java b/src/android/net/apf/ApfV4Generator.java
index e8d3e6d..54a95a1 100644
--- a/src/android/net/apf/ApfV4Generator.java
+++ b/src/android/net/apf/ApfV4Generator.java
@@ -362,6 +362,15 @@
return this;
}
+ // in practice, 'int' always enough for packet offset
+ Instruction addPacketOffset(int imm) {
+ return addUnsigned(imm);
+ }
+
+ // in practice, 'int' always enough for data offset
+ Instruction addDataOffset(int imm) {
+ return addUnsigned(imm);
+ }
Instruction addTwosCompSigned(int imm) {
mIntImms.add(IntImmediate.newTwosComplementSigned(imm));
@@ -714,7 +723,7 @@
* bytes from the beginning of the packet into {@code register}.
*/
public ApfV4Generator addLoad8(Register r, int ofs) {
- return append(new Instruction(Opcodes.LDB, r).addUnsigned(ofs));
+ return append(new Instruction(Opcodes.LDB, r).addPacketOffset(ofs));
}
/**
@@ -722,7 +731,7 @@
* bytes from the beginning of the packet into {@code register}.
*/
public ApfV4Generator addLoad16(Register r, int ofs) {
- return append(new Instruction(Opcodes.LDH, r).addUnsigned(ofs));
+ return append(new Instruction(Opcodes.LDH, r).addPacketOffset(ofs));
}
/**
@@ -730,7 +739,7 @@
* bytes from the beginning of the packet into {@code register}.
*/
public ApfV4Generator addLoad32(Register r, int ofs) {
- return append(new Instruction(Opcodes.LDW, r).addUnsigned(ofs));
+ return append(new Instruction(Opcodes.LDW, r).addPacketOffset(ofs));
}
/**
@@ -739,7 +748,7 @@
* the sum of {@code offset} and the value in register R1.
*/
public ApfV4Generator addLoad8Indexed(Register r, int ofs) {
- return append(new Instruction(Opcodes.LDBX, r).addUnsigned(ofs));
+ return append(new Instruction(Opcodes.LDBX, r).addPacketOffset(ofs));
}
/**
@@ -748,7 +757,7 @@
* the sum of {@code offset} and the value in register R1.
*/
public ApfV4Generator addLoad16Indexed(Register r, int ofs) {
- return append(new Instruction(Opcodes.LDHX, r).addUnsigned(ofs));
+ return append(new Instruction(Opcodes.LDHX, r).addPacketOffset(ofs));
}
/**
@@ -757,7 +766,7 @@
* the sum of {@code offset} and the value in register R1.
*/
public ApfV4Generator addLoad32Indexed(Register r, int ofs) {
- return append(new Instruction(Opcodes.LDWX, r).addUnsigned(ofs));
+ return append(new Instruction(Opcodes.LDWX, r).addPacketOffset(ofs));
}
/**
diff --git a/src/android/net/apf/ApfV6Generator.java b/src/android/net/apf/ApfV6Generator.java
index 48d4806..5ca41f7 100644
--- a/src/android/net/apf/ApfV6Generator.java
+++ b/src/android/net/apf/ApfV6Generator.java
@@ -170,7 +170,7 @@
* @return the ApfGenerator object
*/
public ApfV4Generator addDataCopy(int src, int len) {
- return append(new Instruction(Opcodes.PKTDATACOPY, Rbit1).addUnsigned(src).addU8(len));
+ return append(new Instruction(Opcodes.PKTDATACOPY, Rbit1).addDataOffset(src).addU8(len));
}
/**
@@ -183,7 +183,7 @@
* @return the ApfGenerator object
*/
public ApfV4Generator addPacketCopy(int src, int len) {
- return append(new Instruction(Opcodes.PKTDATACOPY, Rbit0).addUnsigned(src).addU8(len));
+ return append(new Instruction(Opcodes.PKTDATACOPY, Rbit0).addPacketOffset(src).addU8(len));
}
/**