From 97e0388cb7f1331536911fbc7b60634afecbf00b Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Fri, 18 Aug 2017 03:33:03 +0000 Subject: [PATCH] Simplified the argument check in BitBuffer.appendBits() by tightening the input range, also removed some parentheses. --- cpp/BitBuffer.cpp | 2 +- java/io/nayuki/qrcodegen/BitBuffer.java | 2 +- javascript/qrcodegen.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cpp/BitBuffer.cpp b/cpp/BitBuffer.cpp index 7f1441a..d6e8cb2 100644 --- a/cpp/BitBuffer.cpp +++ b/cpp/BitBuffer.cpp @@ -39,7 +39,7 @@ std::vector BitBuffer::getBytes() const { void BitBuffer::appendBits(std::uint32_t val, int len) { - if (len < 0 || len > 32 || (len < 32 && (val >> len) != 0)) + if (len < 0 || len > 31 || val >> len != 0) throw "Value out of range"; for (int i = len - 1; i >= 0; i--) // Append bit by bit this->push_back(((val >> i) & 1) != 0); diff --git a/java/io/nayuki/qrcodegen/BitBuffer.java b/java/io/nayuki/qrcodegen/BitBuffer.java index 961f804..fad2e67 100644 --- a/java/io/nayuki/qrcodegen/BitBuffer.java +++ b/java/io/nayuki/qrcodegen/BitBuffer.java @@ -77,7 +77,7 @@ public final class BitBuffer implements Cloneable { // Appends the given number of bits of the given value to this sequence. // If 0 <= len <= 31, then this requires 0 <= val < 2^len. public void appendBits(int val, int len) { - if (len < 0 || len > 32 || len < 32 && (val >>> len) != 0) + if (len < 0 || len > 31 || val >>> len != 0) throw new IllegalArgumentException("Value out of range"); for (int i = len - 1; i >= 0; i--, bitLength++) // Append bit by bit data.set(bitLength, ((val >>> i) & 1) != 0); diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index aef1649..d7fa3a5 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -1006,7 +1006,7 @@ var qrcodegen = new function() { // Appends the given number of bits of the given value to this sequence. // If 0 <= len <= 31, then this requires 0 <= val < 2^len. this.appendBits = function(val, len) { - if (len < 0 || len > 32 || len < 32 && (val >>> len) != 0) + if (len < 0 || len > 31 || val >>> len != 0) throw "Value out of range"; for (var i = len - 1; i >= 0; i--) // Append bit by bit this.push((val >>> i) & 1);