diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index 52281f7..1a24bf4 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -115,16 +115,14 @@ QrCode QrCode::encodeSegments(const vector &segs, Ecc ecl, QrCode::QrCode(int ver, Ecc ecl, const vector &dataCodewords, int mask) : - // Initialize fields + // Initialize fields and check arguments version(ver), - size(MIN_VERSION <= ver && ver <= MAX_VERSION ? ver * 4 + 17 : -1), // Avoid signed overflow undefined behavior errorCorrectionLevel(ecl), modules (size, vector(size)), // Initially all white isFunction(size, vector(size)) { - - // Check arguments if (ver < MIN_VERSION || ver > MAX_VERSION || mask < -1 || mask > 7) throw std::domain_error("Value out of range"); + size = ver * 4 + 17; // Compute ECC, draw modules, do masking drawFunctionPatterns(); diff --git a/java/io/nayuki/qrcodegen/QrCode.java b/java/io/nayuki/qrcodegen/QrCode.java index d89781c..42373d6 100644 --- a/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/io/nayuki/qrcodegen/QrCode.java @@ -213,15 +213,13 @@ public final class QrCode { * or if the data is the wrong length for the specified version and error correction level */ public QrCode(int ver, Ecc ecl, byte[] dataCodewords, int mask) { - // Check arguments - errorCorrectionLevel = Objects.requireNonNull(ecl); - Objects.requireNonNull(dataCodewords); + // Check arguments and initialize fields if (ver < MIN_VERSION || ver > MAX_VERSION || mask < -1 || mask > 7) throw new IllegalArgumentException("Value out of range"); - - // Initialize fields version = ver; size = ver * 4 + 17; + errorCorrectionLevel = Objects.requireNonNull(ecl); + Objects.requireNonNull(dataCodewords); modules = new boolean[size][size]; // Initially all white isFunction = new boolean[size][size]; diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index 25d2d09..d9567bf 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -72,11 +72,11 @@ var qrcodegen = new function() { /*---- Constructor (low level) ----*/ - // Check arguments and handle simple scalar fields - if (mask < -1 || mask > 7) - throw "Mask value out of range"; + // Check scalar arguments if (version < MIN_VERSION || version > MAX_VERSION) throw "Version value out of range"; + if (mask < -1 || mask > 7) + throw "Mask value out of range"; var size = version * 4 + 17; // Initialize both grids to be size*size arrays of Boolean false diff --git a/python/qrcodegen.py b/python/qrcodegen.py index d07a6cf..2159850 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -151,21 +151,21 @@ class QrCode(object): and mask number. mask = -1 is for automatic choice, or 0 to 7 for fixed choice. This is a cumbersome low-level constructor that should not be invoked directly by the user. To go one level up, see the QrCode.encode_segments() function.""" - # Check arguments and handle simple scalar fields - if not (-1 <= mask <= 7): - raise ValueError("Mask value out of range") + # Check scalar arguments and set fields if not (QrCode.MIN_VERSION <= version <= QrCode.MAX_VERSION): raise ValueError("Version value out of range") + if not (-1 <= mask <= 7): + raise ValueError("Mask value out of range") if not isinstance(errcorlvl, QrCode.Ecc): raise TypeError("QrCode.Ecc expected") self._version = version - self._errcorlvl = errcorlvl self._size = version * 4 + 17 + self._errcorlvl = errcorlvl if len(datacodewords) != QrCode._get_num_data_codewords(version, errcorlvl): raise ValueError("Invalid array length") - # Initialize grids of modules + # Initialize both grids to be size*size arrays of Boolean false # The modules of this QR Code symbol (False = white, True = black). Immutable after constructor finishes self._modules = [[False] * self._size for _ in range(self._size)] # Initially all white # Indicates function modules that are not subjected to masking. Discarded when constructor finishes diff --git a/typescript/qrcodegen.ts b/typescript/qrcodegen.ts index dde5e87..82a42e6 100644 --- a/typescript/qrcodegen.ts +++ b/typescript/qrcodegen.ts @@ -160,11 +160,11 @@ namespace qrcodegen { // (mask = -1), the resulting object will still have a mask value between 0 and 7. public readonly mask: int) { - // Check arguments and handle simple scalar fields - if (mask < -1 || mask > 7) - throw "Mask value out of range"; + // Check scalar arguments if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION) throw "Version value out of range"; + if (mask < -1 || mask > 7) + throw "Mask value out of range"; this.size = version * 4 + 17; // Initialize both grids to be size*size arrays of Boolean false