Avoid uninit value in CBC_PDF417I::Encode()

Change CBC_PDF417Writer::Encode() to return a struct instead of using
out-parameters. Thus helping to make sure all the return paths return
width / height values.

Also mark Encode() as const, and move CBC_PDF417Writer::RotateArray()
into an anonymous namespace.

Bug: 342428942
Change-Id: If166a1679ebac5719460d506c59c043199f0e201
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/119593
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/fxbarcode/cbc_pdf417i.cpp b/fxbarcode/cbc_pdf417i.cpp
index cb237a9..54a7f42 100644
--- a/fxbarcode/cbc_pdf417i.cpp
+++ b/fxbarcode/cbc_pdf417i.cpp
@@ -45,11 +45,9 @@
   if (contents.GetLength() > kMaxPDF417InputLengthBytes)
     return false;
 
-  int32_t width;
-  int32_t height;
   auto* pWriter = GetPDF417Writer();
-  DataVector<uint8_t> data = pWriter->Encode(contents, &width, &height);
-  return pWriter->RenderResult(data, width, height);
+  CBC_PDF417Writer::EncodeResult result = pWriter->Encode(contents);
+  return pWriter->RenderResult(result.data, result.width, result.height);
 }
 
 bool CBC_PDF417I::RenderDevice(CFX_RenderDevice* device,
diff --git a/fxbarcode/pdf417/BC_PDF417Writer.cpp b/fxbarcode/pdf417/BC_PDF417Writer.cpp
index a7ed452..2999552 100644
--- a/fxbarcode/pdf417/BC_PDF417Writer.cpp
+++ b/fxbarcode/pdf417/BC_PDF417Writer.cpp
@@ -34,6 +34,20 @@
 #include "fxbarcode/pdf417/BC_PDF417.h"
 #include "fxbarcode/pdf417/BC_PDF417BarcodeMatrix.h"
 
+namespace {
+
+void RotateArray(DataVector<uint8_t>& bitarray, int32_t width, int32_t height) {
+  DataVector<uint8_t> temp = bitarray;
+  for (int32_t i = 0; i < height; i++) {
+    int32_t inverse_i = height - i - 1;
+    for (int32_t j = 0; j < width; j++) {
+      bitarray[j * height + inverse_i] = temp[i * width + j];
+    }
+  }
+}
+
+}  // namespace
+
 CBC_PDF417Writer::CBC_PDF417Writer() : CBC_TwoDimWriter(false) {}
 
 CBC_PDF417Writer::~CBC_PDF417Writer() = default;
@@ -46,9 +60,8 @@
   return true;
 }
 
-DataVector<uint8_t> CBC_PDF417Writer::Encode(WideStringView contents,
-                                             int32_t* pOutWidth,
-                                             int32_t* pOutHeight) {
+CBC_PDF417Writer::EncodeResult CBC_PDF417Writer::Encode(
+    WideStringView contents) const {
   CBC_PDF417 encoder;
   int32_t col = (m_Width / m_ModuleWidth - 69) / 17;
   int32_t row = m_Height / (m_ModuleWidth * 20);
@@ -58,31 +71,25 @@
     encoder.setDimensions(col, col, 90, 3);
   else if (row >= 3 && row <= 90)
     encoder.setDimensions(30, 1, row, row);
-  if (!encoder.GenerateBarcodeLogic(contents, error_correction_level()))
-    return DataVector<uint8_t>();
+  if (!encoder.GenerateBarcodeLogic(contents, error_correction_level())) {
+    return {DataVector<uint8_t>(), 0, 0};
+  }
 
   CBC_BarcodeMatrix* barcodeMatrix = encoder.getBarcodeMatrix();
-  DataVector<uint8_t> matrixData = barcodeMatrix->toBitArray();
-  int32_t matrixWidth = barcodeMatrix->getWidth();
-  int32_t matrixHeight = barcodeMatrix->getHeight();
+  DataVector<uint8_t> matrix_data = barcodeMatrix->toBitArray();
+  int32_t matrix_width = barcodeMatrix->getWidth();
+  int32_t matrix_height = barcodeMatrix->getHeight();
 
-  if (matrixWidth < matrixHeight) {
-    RotateArray(&matrixData, matrixHeight, matrixWidth);
-    std::swap(matrixWidth, matrixHeight);
+  if (matrix_width < matrix_height) {
+    RotateArray(matrix_data, matrix_width, matrix_height);
+    std::swap(matrix_width, matrix_height);
   }
-  *pOutWidth = matrixWidth;
-  *pOutHeight = matrixHeight;
-  return matrixData;
+  return {std::move(matrix_data), matrix_width, matrix_height};
 }
 
-void CBC_PDF417Writer::RotateArray(DataVector<uint8_t>* bitarray,
-                                   int32_t height,
-                                   int32_t width) {
-  DataVector<uint8_t> temp = *bitarray;
-  for (int32_t ii = 0; ii < height; ii++) {
-    int32_t inverseii = height - ii - 1;
-    for (int32_t jj = 0; jj < width; jj++) {
-      (*bitarray)[jj * height + inverseii] = temp[ii * width + jj];
-    }
-  }
-}
+CBC_PDF417Writer::EncodeResult::EncodeResult(DataVector<uint8_t> data,
+                                             int32_t width,
+                                             int32_t height)
+    : data(std::move(data)), width(width), height(height) {}
+
+CBC_PDF417Writer::EncodeResult::~EncodeResult() = default;
diff --git a/fxbarcode/pdf417/BC_PDF417Writer.h b/fxbarcode/pdf417/BC_PDF417Writer.h
index dc1f1f4..cb9a1c1 100644
--- a/fxbarcode/pdf417/BC_PDF417Writer.h
+++ b/fxbarcode/pdf417/BC_PDF417Writer.h
@@ -16,20 +16,22 @@
 
 class CBC_PDF417Writer final : public CBC_TwoDimWriter {
  public:
+  struct EncodeResult {
+    EncodeResult(DataVector<uint8_t> data, int32_t width, int32_t height);
+    ~EncodeResult();
+
+    DataVector<uint8_t> data;
+    int32_t width;
+    int32_t height;
+  };
+
   CBC_PDF417Writer();
   ~CBC_PDF417Writer() override;
 
-  DataVector<uint8_t> Encode(WideStringView contents,
-                             int32_t* pOutWidth,
-                             int32_t* pOutHeight);
+  EncodeResult Encode(WideStringView contents) const;
 
   // CBC_TwoDimWriter
   bool SetErrorCorrectionLevel(int32_t level) override;
-
- private:
-  void RotateArray(DataVector<uint8_t>* bitarray,
-                   int32_t width,
-                   int32_t height);
 };
 
 #endif  // FXBARCODE_PDF417_BC_PDF417WRITER_H_
diff --git a/fxbarcode/pdf417/BC_PDF417Writer_unittest.cpp b/fxbarcode/pdf417/BC_PDF417Writer_unittest.cpp
index 2bcb1c3..55ed23c 100644
--- a/fxbarcode/pdf417/BC_PDF417Writer_unittest.cpp
+++ b/fxbarcode/pdf417/BC_PDF417Writer_unittest.cpp
@@ -26,8 +26,6 @@
 
 TEST_F(CBC_PDF417WriterTest, Encode) {
   CBC_PDF417Writer writer;
-  int32_t width;
-  int32_t height;
 
   {
     static constexpr int kExpectedWidth = 579;
@@ -419,12 +417,12 @@
         1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1,
         0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1,
         1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1};
-    DataVector<uint8_t> data = writer.Encode(L"", &width, &height);
-    ASSERT_EQ(std::size(kExpectedData), data.size());
-    ASSERT_EQ(kExpectedWidth, width);
-    ASSERT_EQ(kExpectedHeight, height);
+    CBC_PDF417Writer::EncodeResult result = writer.Encode(L"");
+    ASSERT_EQ(std::size(kExpectedData), result.data.size());
+    ASSERT_EQ(kExpectedWidth, result.width);
+    ASSERT_EQ(kExpectedHeight, result.height);
     for (size_t i = 0; i < std::size(kExpectedData); ++i)
-      EXPECT_EQ(kExpectedData[i], data[i]) << i;
+      EXPECT_EQ(kExpectedData[i], result.data[i]) << i;
   }
   {
     static constexpr int kExpectedWidth = 579;
@@ -816,11 +814,11 @@
         1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0,
         0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1,
         1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1};
-    DataVector<uint8_t> data = writer.Encode(L"hello world", &width, &height);
-    ASSERT_EQ(std::size(kExpectedData), data.size());
-    ASSERT_EQ(kExpectedWidth, width);
-    ASSERT_EQ(kExpectedHeight, height);
+    CBC_PDF417Writer::EncodeResult result = writer.Encode(L"hello world");
+    ASSERT_EQ(std::size(kExpectedData), result.data.size());
+    ASSERT_EQ(kExpectedWidth, result.width);
+    ASSERT_EQ(kExpectedHeight, result.height);
     for (size_t i = 0; i < std::size(kExpectedData); ++i)
-      EXPECT_EQ(kExpectedData[i], data[i]) << i;
+      EXPECT_EQ(kExpectedData[i], result.data[i]) << i;
   }
 }