Change CBC_QRCoderMatrixUtil::BuildMatrix() to return a bool.

Fix caller code and remove some impossible to reach code inside
CBC_QRCoderMatrixUtil.

Change-Id: I3b0cc0750784e806ba4050fb2487675dee4ee8c3
Reviewed-on: https://pdfium-review.googlesource.com/42455
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/fxbarcode/qrcode/BC_QRCoderEncoder.cpp b/fxbarcode/qrcode/BC_QRCoderEncoder.cpp
index 052ad49..4d15ca6 100644
--- a/fxbarcode/qrcode/BC_QRCoderEncoder.cpp
+++ b/fxbarcode/qrcode/BC_QRCoderEncoder.cpp
@@ -38,6 +38,7 @@
 #include "fxbarcode/qrcode/BC_QRCoderMatrixUtil.h"
 #include "fxbarcode/qrcode/BC_QRCoderMode.h"
 #include "fxbarcode/qrcode/BC_QRCoderVersion.h"
+#include "third_party/base/optional.h"
 #include "third_party/base/ptr_util.h"
 
 using ModeStringPair = std::pair<CBC_QRCoderMode*, ByteString>;
@@ -249,19 +250,19 @@
          CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule4(matrix);
 }
 
-int32_t ChooseMaskPattern(CBC_QRCoderBitVector* bits,
-                          const CBC_QRCoderErrorCorrectionLevel* ecLevel,
-                          int32_t version,
-                          CBC_CommonByteMatrix* matrix,
-                          int32_t& e) {
+Optional<int32_t> ChooseMaskPattern(
+    CBC_QRCoderBitVector* bits,
+    const CBC_QRCoderErrorCorrectionLevel* ecLevel,
+    int32_t version,
+    CBC_CommonByteMatrix* matrix) {
   int32_t minPenalty = 65535;
   int32_t bestMaskPattern = -1;
   for (int32_t maskPattern = 0; maskPattern < CBC_QRCoder::kNumMaskPatterns;
        maskPattern++) {
-    CBC_QRCoderMatrixUtil::BuildMatrix(bits, ecLevel, version, maskPattern,
-                                       matrix, e);
-    if (e != BCExceptionNO)
-      return 0;
+    if (!CBC_QRCoderMatrixUtil::BuildMatrix(bits, ecLevel, version, maskPattern,
+                                            matrix)) {
+      return {};
+    }
     int32_t penalty = CalculateMaskPenalty(matrix);
     if (penalty < minPenalty) {
       minPenalty = penalty;
@@ -494,20 +495,19 @@
     return false;
   }
 
-  int32_t e = BCExceptionNO;
   auto matrix = pdfium::MakeUnique<CBC_CommonByteMatrix>(
       qrCode->GetMatrixWidth(), qrCode->GetMatrixWidth());
-  int32_t maskPattern = ChooseMaskPattern(
-      &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(), matrix.get(), e);
-  if (e != BCExceptionNO)
+  Optional<int32_t> maskPattern = ChooseMaskPattern(
+      &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(), matrix.get());
+  if (!maskPattern)
     return false;
 
-  qrCode->SetMaskPattern(maskPattern);
-  CBC_QRCoderMatrixUtil::BuildMatrix(&finalBits, qrCode->GetECLevel(),
-                                     qrCode->GetVersion(),
-                                     qrCode->GetMaskPattern(), matrix.get(), e);
-  if (e != BCExceptionNO)
+  qrCode->SetMaskPattern(*maskPattern);
+  if (!CBC_QRCoderMatrixUtil::BuildMatrix(
+          &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(),
+          qrCode->GetMaskPattern(), matrix.get())) {
     return false;
+  }
 
   qrCode->SetMatrix(std::move(matrix));
   return qrCode->IsValid();
diff --git a/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp b/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp
index 0ef04cb..d7e35c1 100644
--- a/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp
+++ b/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp
@@ -93,19 +93,11 @@
   return numDigits;
 }
 
-void ClearMatrix(CBC_CommonByteMatrix* matrix, int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
-  matrix->clear((uint8_t)-1);
-}
-
 void EmbedDataBits(CBC_QRCoderBitVector* dataBits,
                    int32_t maskPattern,
                    CBC_CommonByteMatrix* matrix,
                    int32_t& e) {
-  if (!matrix || !dataBits) {
+  if (!dataBits) {
     e = BCExceptionNullPointer;
     return;
   }
@@ -209,10 +201,6 @@
                    int32_t maskPattern,
                    CBC_CommonByteMatrix* matrix,
                    int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
   CBC_QRCoderBitVector typeInfoBits;
   MakeTypeInfoBits(ecLevel, maskPattern, &typeInfoBits, e);
   if (e != BCExceptionNO)
@@ -240,10 +228,6 @@
 void MaybeEmbedVersionInfo(int32_t version,
                            CBC_CommonByteMatrix* matrix,
                            int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
   if (version < 7) {
     return;
   }
@@ -265,10 +249,6 @@
 }
 
 void EmbedTimingPatterns(CBC_CommonByteMatrix* matrix, int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
   for (int32_t i = 8; i < matrix->GetWidth() - 8; i++) {
     int32_t bit = (i + 1) % 2;
     if (!IsValidValue(matrix->Get(i, 6))) {
@@ -289,10 +269,6 @@
 }
 
 void EmbedDarkDotAtLeftBottomCorner(CBC_CommonByteMatrix* matrix, int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
   if (matrix->Get(8, matrix->GetHeight() - 8) == 0) {
     e = BCExceptionHeight_8BeZero;
     return;
@@ -304,10 +280,6 @@
                                       int32_t yStart,
                                       CBC_CommonByteMatrix* matrix,
                                       int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
   for (int32_t x = 0; x < 8; x++) {
     if (!IsEmpty(matrix->Get(xStart + x, yStart))) {
       e = BCExceptionInvalidateData;
@@ -321,10 +293,6 @@
                                     int32_t yStart,
                                     CBC_CommonByteMatrix* matrix,
                                     int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
   for (int32_t y = 0; y < 7; y++) {
     if (!IsEmpty(matrix->Get(xStart, yStart + y))) {
       e = BCExceptionInvalidateData;
@@ -338,11 +306,6 @@
                                     int32_t yStart,
                                     CBC_CommonByteMatrix* matrix,
                                     int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    if (e != BCExceptionNO)
-      return;
-  }
   for (int32_t y = 0; y < 5; y++) {
     for (int32_t x = 0; x < 5; x++) {
       if (!IsEmpty(matrix->Get(xStart + x, y + yStart))) {
@@ -358,10 +321,6 @@
                                    int32_t yStart,
                                    CBC_CommonByteMatrix* matrix,
                                    int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
   for (int32_t y = 0; y < 7; y++) {
     for (int32_t x = 0; x < 7; x++) {
       if (!IsEmpty(matrix->Get(xStart + x, yStart + y))) {
@@ -375,10 +334,6 @@
 
 void EmbedPositionDetectionPatternsAndSeparators(CBC_CommonByteMatrix* matrix,
                                                  int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
   int32_t pdpWidth = 7;
   EmbedPositionDetectionPattern(0, 0, matrix, e);
   if (e != BCExceptionNO)
@@ -417,10 +372,6 @@
 void MaybeEmbedPositionAdjustmentPatterns(int32_t version,
                                           CBC_CommonByteMatrix* matrix,
                                           int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
   if (version < 2) {
     return;
   }
@@ -447,10 +398,6 @@
 void EmbedBasicPatterns(int32_t version,
                         CBC_CommonByteMatrix* matrix,
                         int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
   EmbedPositionDetectionPatternsAndSeparators(matrix, e);
   if (e != BCExceptionNO)
     return;
@@ -467,30 +414,29 @@
 
 }  // namespace
 
-void CBC_QRCoderMatrixUtil::BuildMatrix(
+bool CBC_QRCoderMatrixUtil::BuildMatrix(
     CBC_QRCoderBitVector* dataBits,
     const CBC_QRCoderErrorCorrectionLevel* ecLevel,
     int32_t version,
     int32_t maskPattern,
-    CBC_CommonByteMatrix* matrix,
-    int32_t& e) {
-  if (!matrix) {
-    e = BCExceptionNullPointer;
-    return;
-  }
-  ClearMatrix(matrix, e);
-  if (e != BCExceptionNO)
-    return;
+    CBC_CommonByteMatrix* matrix) {
+  if (!matrix)
+    return false;
+
+  matrix->clear(0xff);
+
+  int32_t e = BCExceptionNO;
   EmbedBasicPatterns(version, matrix, e);
   if (e != BCExceptionNO)
-    return;
+    return false;
   EmbedTypeInfo(ecLevel, maskPattern, matrix, e);
   if (e != BCExceptionNO)
-    return;
+    return false;
   MaybeEmbedVersionInfo(version, matrix, e);
   if (e != BCExceptionNO)
-    return;
+    return false;
   EmbedDataBits(dataBits, maskPattern, matrix, e);
   if (e != BCExceptionNO)
-    return;
+    return false;
+  return true;
 }
diff --git a/fxbarcode/qrcode/BC_QRCoderMatrixUtil.h b/fxbarcode/qrcode/BC_QRCoderMatrixUtil.h
index 3a286ef..2952511 100644
--- a/fxbarcode/qrcode/BC_QRCoderMatrixUtil.h
+++ b/fxbarcode/qrcode/BC_QRCoderMatrixUtil.h
@@ -16,12 +16,11 @@
   CBC_QRCoderMatrixUtil() = delete;
   ~CBC_QRCoderMatrixUtil() = delete;
 
-  static void BuildMatrix(CBC_QRCoderBitVector* dataBits,
+  static bool BuildMatrix(CBC_QRCoderBitVector* dataBits,
                           const CBC_QRCoderErrorCorrectionLevel* ecLevel,
                           int32_t version,
                           int32_t maskPattern,
-                          CBC_CommonByteMatrix* matrix,
-                          int32_t& e);
+                          CBC_CommonByteMatrix* matrix);
 };
 
 #endif  // FXBARCODE_QRCODE_BC_QRCODERMATRIXUTIL_H_