From 490769e75fe58c034e6f0dada5f82e3f0b37b857 Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Wed, 1 Sep 2021 12:48:20 -0500 Subject: [PATCH] Possible fix for https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=37905 --- .../java/com/google/zxing/aztec/encoder/State.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/google/zxing/aztec/encoder/State.java b/core/src/main/java/com/google/zxing/aztec/encoder/State.java index d31b166d6..acd1ac6d8 100644 --- a/core/src/main/java/com/google/zxing/aztec/encoder/State.java +++ b/core/src/main/java/com/google/zxing/aztec/encoder/State.java @@ -42,12 +42,14 @@ final class State { private final int binaryShiftByteCount; // The total number of bits generated (including Binary Shift). private final int bitCount; + private final int binaryShiftCost; private State(Token token, int mode, int binaryBytes, int bitCount) { this.token = token; this.mode = mode; this.binaryShiftByteCount = binaryBytes; this.bitCount = bitCount; + this.binaryShiftCost = calculateBinaryShiftCost(binaryBytes); } int getMode() { @@ -155,7 +157,7 @@ final class State { int newModeBitCount = this.bitCount + (HighLevelEncoder.LATCH_TABLE[this.mode][other.mode] >> 16); if (this.binaryShiftByteCount < other.binaryShiftByteCount) { // add additional B/S encoding cost of other, if any - newModeBitCount += calculateBinaryShiftCost(other) - calculateBinaryShiftCost(this); + newModeBitCount += other.binaryShiftCost - this.binaryShiftCost; } else if (this.binaryShiftByteCount > other.binaryShiftByteCount && other.binaryShiftByteCount > 0) { // maximum possible additional cost (we end up exceeding the 31 byte boundary and other state can stay beneath it) newModeBitCount += 10; @@ -184,14 +186,14 @@ final class State { return String.format("%s bits=%d bytes=%d", HighLevelEncoder.MODE_NAMES[mode], bitCount, binaryShiftByteCount); } - private static int calculateBinaryShiftCost(State state) { - if (state.binaryShiftByteCount > 62) { + private static int calculateBinaryShiftCost(int binaryShiftByteCount) { + if (binaryShiftByteCount > 62) { return 21; // B/S with extended length } - if (state.binaryShiftByteCount > 31) { + if (binaryShiftByteCount > 31) { return 20; // two B/S } - if (state.binaryShiftByteCount > 0) { + if (binaryShiftByteCount > 0) { return 10; // one B/S } return 0;