Remove CPDF_Stream's span ctor.

Instead of having CPDF_Stream copy the span internally, change callers
to allocate DataVectors.

Change-Id: I5c42b14dc8b9e46d7abba83263924f1637db00c5
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/99692
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp
index 8d9b197..c47258c 100644
--- a/core/fpdfapi/page/cpdf_image.cpp
+++ b/core/fpdfapi/page/cpdf_image.cpp
@@ -294,11 +294,8 @@
 
   uint8_t* src_buf = pBitmap->GetBuffer();
   int32_t src_pitch = pBitmap->GetPitch();
-  std::unique_ptr<uint8_t, FxFreeDeleter> dest_buf(
-      FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight));
-  // Safe as checked alloc returned.
-  size_t dest_size = dest_pitch * BitmapHeight;
-  auto dest_span = pdfium::make_span(dest_buf.get(), dest_size);
+  DataVector<uint8_t> dest_buf(Fx2DSizeOrDie(dest_pitch, BitmapHeight));
+  auto dest_span = pdfium::make_span(dest_buf);
   size_t dest_span_offset = 0;
   if (bCopyWithoutAlpha) {
     for (int32_t i = 0; i < BitmapHeight; i++) {
@@ -328,7 +325,8 @@
     }
   }
 
-  m_pStream = pdfium::MakeRetain<CPDF_Stream>(dest_span, std::move(pDict));
+  m_pStream =
+      pdfium::MakeRetain<CPDF_Stream>(std::move(dest_buf), std::move(pDict));
   m_bIsMask = pBitmap->IsMaskFormat();
   m_Width = BitmapWidth;
   m_Height = BitmapHeight;
diff --git a/core/fpdfapi/parser/cpdf_object_stream_unittest.cpp b/core/fpdfapi/parser/cpdf_object_stream_unittest.cpp
index 8b0c9ab..2b31fc5 100644
--- a/core/fpdfapi/parser/cpdf_object_stream_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_object_stream_unittest.cpp
@@ -12,6 +12,7 @@
 #include "core/fpdfapi/parser/cpdf_number.h"
 #include "core/fpdfapi/parser/cpdf_stream.h"
 #include "core/fpdfapi/parser/cpdf_string.h"
+#include "core/fxcrt/data_vector.h"
 #include "testing/gmock/include/gmock/gmock.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
@@ -35,8 +36,9 @@
   dict->SetNewFor<CPDF_Number>("N", 3);
   dict->SetNewFor<CPDF_Number>("First", kNormalStreamContentOffset);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
@@ -78,8 +80,10 @@
 }
 
 TEST(ObjectStreamTest, StreamNoDict) {
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), /*pDict=*/nullptr);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()),
+      /*pDict=*/nullptr);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -88,8 +92,9 @@
   dict->SetNewFor<CPDF_Number>("N", 3);
   dict->SetNewFor<CPDF_Number>("First", 5);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -99,8 +104,9 @@
   dict->SetNewFor<CPDF_Number>("N", 3);
   dict->SetNewFor<CPDF_Number>("First", 5);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -110,8 +116,9 @@
   dict->SetNewFor<CPDF_Number>("N", 3);
   dict->SetNewFor<CPDF_Number>("First", 5);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -120,8 +127,9 @@
   dict->SetNewFor<CPDF_Name>("Type", "ObjStm");
   dict->SetNewFor<CPDF_Number>("First", 5);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -131,8 +139,9 @@
   dict->SetNewFor<CPDF_Number>("N", 2.2f);
   dict->SetNewFor<CPDF_Number>("First", 5);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -142,8 +151,9 @@
   dict->SetNewFor<CPDF_Number>("N", -1);
   dict->SetNewFor<CPDF_Number>("First", 5);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -153,8 +163,9 @@
   dict->SetNewFor<CPDF_Number>("N", 999999999);
   dict->SetNewFor<CPDF_Number>("First", 5);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -163,8 +174,9 @@
   dict->SetNewFor<CPDF_Name>("Type", "ObjStm");
   dict->SetNewFor<CPDF_Number>("N", 3);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -174,8 +186,9 @@
   dict->SetNewFor<CPDF_Number>("N", 3);
   dict->SetNewFor<CPDF_Number>("First", 5.5f);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -185,8 +198,9 @@
   dict->SetNewFor<CPDF_Number>("N", 3);
   dict->SetNewFor<CPDF_Number>("First", -5);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   EXPECT_FALSE(CPDF_ObjectStream::Create(stream.Get()));
 }
 
@@ -197,8 +211,9 @@
   constexpr int kTooBigOffset = std::size(kNormalStreamContent);
   dict->SetNewFor<CPDF_Number>("First", kTooBigOffset);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
@@ -219,8 +234,9 @@
   dict->SetNewFor<CPDF_Number>("N", 2);
   dict->SetNewFor<CPDF_Number>("First", kNormalStreamContentOffset);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
@@ -250,8 +266,9 @@
   dict->SetNewFor<CPDF_Number>("N", 9);
   dict->SetNewFor<CPDF_Number>("First", kNormalStreamContentOffset);
 
+  ByteStringView contents_view(kNormalStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kNormalStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
@@ -277,8 +294,9 @@
   dict->SetNewFor<CPDF_Number>("First", 19);
 
   const char kStreamContent[] = "10 0 hi 14 12 21<</Name /Foo>>[1 2 3]4";
+  ByteStringView contents_view(kStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
@@ -294,8 +312,9 @@
   dict->SetNewFor<CPDF_Number>("First", 16);
 
   const char kStreamContent[] = "10 0 11 hi 12 21<</Name /Foo>>[1 2 3]4";
+  ByteStringView contents_view(kStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
@@ -326,8 +345,9 @@
   dict->SetNewFor<CPDF_Number>("First", 16);
 
   const char kStreamContent[] = "10 0 11 -1 12 21<</Name /Foo>>[1 2 3]4";
+  ByteStringView contents_view(kStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
@@ -348,8 +368,9 @@
   dict->SetNewFor<CPDF_Number>("First", 17);
 
   const char kStreamContent[] = "10 0 11 999 12 21<</Name /Foo>>[1 2 3]4";
+  ByteStringView contents_view(kStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
@@ -370,8 +391,9 @@
   dict->SetNewFor<CPDF_Number>("First", 16);
 
   const char kStreamContent[] = "10 0 10 14 12 21<</Name /Foo>>[1 2 3]4";
+  ByteStringView contents_view(kStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
@@ -412,8 +434,9 @@
   dict->SetNewFor<CPDF_Number>("First", 16);
 
   const char kStreamContent[] = "11 0 12 14 10 21<</Name /Foo>>[1 2 3]4";
+  ByteStringView contents_view(kStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
@@ -453,8 +476,9 @@
   dict->SetNewFor<CPDF_Number>("First", 16);
 
   const char kStreamContent[] = "10 21 11 0 12 14<</Name /Foo>>[1 2 3]4";
+  ByteStringView contents_view(kStreamContent);
   auto stream = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView(kStreamContent).raw_span(), dict);
+      DataVector<uint8_t>(contents_view.begin(), contents_view.end()), dict);
   auto obj_stream = CPDF_ObjectStream::Create(stream.Get());
   ASSERT_TRUE(obj_stream);
 
diff --git a/core/fpdfapi/parser/cpdf_seekablemultistream_unittest.cpp b/core/fpdfapi/parser/cpdf_seekablemultistream_unittest.cpp
index 6eb6762..56bd4ef 100644
--- a/core/fpdfapi/parser/cpdf_seekablemultistream_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_seekablemultistream_unittest.cpp
@@ -9,6 +9,7 @@
 
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_stream.h"
+#include "core/fxcrt/data_vector.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 TEST(CPDFSeekableMultiStreamTest, NoStreams) {
@@ -36,14 +37,22 @@
 
 TEST(CXFAFileReadTest, NormalStreams) {
   // 16 chars total.
-  auto stream1 =
-      pdfium::MakeRetain<CPDF_Stream>(ByteStringView("one t").raw_span(),
-                                      pdfium::MakeRetain<CPDF_Dictionary>());
+  static const char kOne[] = "one t";
+  static const char kTwo[] = "wo ";
+  static const char kThree[] = "three!!!";
+
+  ByteStringView one_view(kOne);
+  ByteStringView two_view(kTwo);
+  ByteStringView three_view(kThree);
+  auto stream1 = pdfium::MakeRetain<CPDF_Stream>(
+      DataVector<uint8_t>(one_view.begin(), one_view.end()),
+      pdfium::MakeRetain<CPDF_Dictionary>());
   auto stream2 = pdfium::MakeRetain<CPDF_Stream>(
-      ByteStringView("wo ").raw_span(), pdfium::MakeRetain<CPDF_Dictionary>());
-  auto stream3 =
-      pdfium::MakeRetain<CPDF_Stream>(ByteStringView("three!!!").raw_span(),
-                                      pdfium::MakeRetain<CPDF_Dictionary>());
+      DataVector<uint8_t>(two_view.begin(), two_view.end()),
+      pdfium::MakeRetain<CPDF_Dictionary>());
+  auto stream3 = pdfium::MakeRetain<CPDF_Stream>(
+      DataVector<uint8_t>(three_view.begin(), three_view.end()),
+      pdfium::MakeRetain<CPDF_Dictionary>());
 
   std::vector<RetainPtr<const CPDF_Stream>> streams;
   streams.push_back(std::move(stream1));
diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp
index 231450d..a2d8c43 100644
--- a/core/fpdfapi/parser/cpdf_stream.cpp
+++ b/core/fpdfapi/parser/cpdf_stream.cpp
@@ -41,12 +41,6 @@
 CPDF_Stream::CPDF_Stream(RetainPtr<CPDF_Dictionary> pDict)
     : CPDF_Stream(DataVector<uint8_t>(), std::move(pDict)) {}
 
-CPDF_Stream::CPDF_Stream(pdfium::span<const uint8_t> pData,
-                         RetainPtr<CPDF_Dictionary> pDict)
-    : dict_(std::move(pDict)) {
-  SetData(pData);
-}
-
 CPDF_Stream::CPDF_Stream(DataVector<uint8_t> pData,
                          RetainPtr<CPDF_Dictionary> pDict)
     : data_(std::move(pData)), dict_(std::move(pDict)) {
@@ -131,8 +125,6 @@
 void CPDF_Stream::TakeData(DataVector<uint8_t> data) {
   const size_t size = data.size();
   data_ = std::move(data);
-  if (!dict_)
-    dict_ = pdfium::MakeRetain<CPDF_Dictionary>();
   SetLengthInDict(pdfium::base::checked_cast<int>(size));
 }
 
@@ -205,5 +197,7 @@
 }
 
 void CPDF_Stream::SetLengthInDict(int length) {
+  if (!dict_)
+    dict_ = pdfium::MakeRetain<CPDF_Dictionary>();
   dict_->SetNewFor<CPDF_Number>("Length", length);
 }
diff --git a/core/fpdfapi/parser/cpdf_stream.h b/core/fpdfapi/parser/cpdf_stream.h
index 1795a6e..4c57844 100644
--- a/core/fpdfapi/parser/cpdf_stream.h
+++ b/core/fpdfapi/parser/cpdf_stream.h
@@ -71,10 +71,6 @@
   // Initializes with empty data.
   explicit CPDF_Stream(RetainPtr<CPDF_Dictionary> pDict);
 
-  // Copies `pData`.
-  CPDF_Stream(pdfium::span<const uint8_t> pData,
-              RetainPtr<CPDF_Dictionary> pDict);
-
   CPDF_Stream(DataVector<uint8_t> pData, RetainPtr<CPDF_Dictionary> pDict);
   ~CPDF_Stream() override;
 
diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.cpp b/core/fpdfapi/parser/cpdf_syntax_parser.cpp
index e5f1d44..cbf9b27 100644
--- a/core/fpdfapi/parser/cpdf_syntax_parser.cpp
+++ b/core/fpdfapi/parser/cpdf_syntax_parser.cpp
@@ -803,8 +803,7 @@
     pStream->InitStreamFromFile(std::move(data_as_stream), std::move(pDict));
   } else {
     DCHECK(!len);
-    pStream = pdfium::MakeRetain<CPDF_Stream>(pdfium::span<const uint8_t>(),
-                                              std::move(pDict));
+    pStream = pdfium::MakeRetain<CPDF_Stream>(std::move(pDict));
   }
   const FX_FILESIZE end_stream_offset = GetPos();
   memset(m_WordBuffer, 0, kEndObjStr.GetLength() + 1);