Replace CFX_MemoryStream::GetBuffer() with GetSpan().
Use spans instead of GetBuffer() + GetSize(), especially considering
callers all take spans.
Change-Id: I06979e2c66dc28dddd6da77e008f3f0d434e8f2f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98430
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/cfx_memorystream.cpp b/core/fxcrt/cfx_memorystream.cpp
index 4361fdb..066582e 100644
--- a/core/fxcrt/cfx_memorystream.cpp
+++ b/core/fxcrt/cfx_memorystream.cpp
@@ -32,21 +32,27 @@
return true;
}
+pdfium::span<const uint8_t> CFX_MemoryStream::GetSpan() const {
+ return pdfium::make_span(m_data).first(m_nCurSize);
+}
+
bool CFX_MemoryStream::ReadBlockAtOffset(void* buffer,
FX_FILESIZE offset,
size_t size) {
if (!buffer || offset < 0 || !size)
return false;
- FX_SAFE_SIZE_T newPos = size;
- newPos += offset;
- if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 ||
- newPos.ValueOrDie() > m_nCurSize) {
+ FX_SAFE_SIZE_T new_pos = size;
+ new_pos += offset;
+ if (!new_pos.IsValid() || new_pos.ValueOrDefault(0) == 0 ||
+ new_pos.ValueOrDie() > m_nCurSize) {
return false;
}
- m_nCurPos = newPos.ValueOrDie();
- memcpy(buffer, &GetBuffer()[offset], size);
+ m_nCurPos = new_pos.ValueOrDie();
+ // Safe to cast `offset` because it was used to calculate `new_pos` above, and
+ // `new_pos` is valid.
+ memcpy(buffer, &GetSpan()[static_cast<size_t>(offset)], size);
return true;
}
diff --git a/core/fxcrt/cfx_memorystream.h b/core/fxcrt/cfx_memorystream.h
index a573a60..f487183 100644
--- a/core/fxcrt/cfx_memorystream.h
+++ b/core/fxcrt/cfx_memorystream.h
@@ -10,6 +10,7 @@
#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/fx_stream.h"
#include "core/fxcrt/retain_ptr.h"
+#include "third_party/base/span.h"
class CFX_MemoryStream final : public IFX_SeekableStream {
public:
@@ -28,7 +29,7 @@
size_t size) override;
bool Flush() override;
- const uint8_t* GetBuffer() const { return m_data.data(); }
+ pdfium::span<const uint8_t> GetSpan() const;
private:
CFX_MemoryStream();
diff --git a/core/fxcrt/cfx_memorystream_unittest.cpp b/core/fxcrt/cfx_memorystream_unittest.cpp
index 716e47b..31a4e0c 100644
--- a/core/fxcrt/cfx_memorystream_unittest.cpp
+++ b/core/fxcrt/cfx_memorystream_unittest.cpp
@@ -35,13 +35,10 @@
auto stream = pdfium::MakeRetain<CFX_MemoryStream>();
const uint8_t kData1[] = {'a', 'b', 'c'};
ASSERT_TRUE(stream->WriteBlock(kData1, sizeof(kData1)));
- auto stream_contents =
- pdfium::make_span(stream->GetBuffer(), stream->GetSize());
- ASSERT_THAT(stream_contents, testing::ElementsAre('a', 'b', 'c'));
+ ASSERT_THAT(stream->GetSpan(), testing::ElementsAre('a', 'b', 'c'));
ASSERT_TRUE(stream->WriteBlockAtOffset(kData1, 5, sizeof(kData1)));
- stream_contents = pdfium::make_span(stream->GetBuffer(), stream->GetSize());
- ASSERT_THAT(stream_contents,
+ ASSERT_THAT(stream->GetSpan(),
testing::ElementsAre('a', 'b', 'c', '\0', '\0', 'a', 'b', 'c'));
uint8_t buffer[4];
@@ -53,15 +50,11 @@
auto stream = pdfium::MakeRetain<CFX_MemoryStream>();
const uint8_t kData1[] = {'a', 'b', 'c'};
ASSERT_TRUE(stream->WriteBlock(kData1, sizeof(kData1)));
- auto stream_contents =
- pdfium::make_span(stream->GetBuffer(), stream->GetSize());
- ASSERT_THAT(stream_contents, testing::ElementsAre('a', 'b', 'c'));
+ ASSERT_THAT(stream->GetSpan(), testing::ElementsAre('a', 'b', 'c'));
ASSERT_TRUE(stream->WriteBlock(kData1, 0));
- stream_contents = pdfium::make_span(stream->GetBuffer(), stream->GetSize());
- ASSERT_THAT(stream_contents, testing::ElementsAre('a', 'b', 'c'));
+ ASSERT_THAT(stream->GetSpan(), testing::ElementsAre('a', 'b', 'c'));
ASSERT_TRUE(stream->WriteBlock(nullptr, 0));
- stream_contents = pdfium::make_span(stream->GetBuffer(), stream->GetSize());
- ASSERT_THAT(stream_contents, testing::ElementsAre('a', 'b', 'c'));
+ ASSERT_THAT(stream->GetSpan(), testing::ElementsAre('a', 'b', 'c'));
}
diff --git a/fxjs/xfa/cjx_node.cpp b/fxjs/xfa/cjx_node.cpp
index 942c411..6f287be 100644
--- a/fxjs/xfa/cjx_node.cpp
+++ b/fxjs/xfa/cjx_node.cpp
@@ -370,8 +370,8 @@
pElement->Save(pMemoryStream);
}
- return CJS_Result::Success(runtime->NewString(
- ByteStringView(pMemoryStream->GetBuffer(), pMemoryStream->GetSize())));
+ return CJS_Result::Success(
+ runtime->NewString(ByteStringView(pMemoryStream->GetSpan())));
}
CJS_Result CJX_Node::setAttribute(
diff --git a/xfa/fxfa/parser/xfa_utils.cpp b/xfa/fxfa/parser/xfa_utils.cpp
index fe2c7eb..7aef2ef 100644
--- a/xfa/fxfa/parser/xfa_utils.cpp
+++ b/xfa/fxfa/parser/xfa_utils.cpp
@@ -196,9 +196,8 @@
auto pMemStream = pdfium::MakeRetain<CFX_MemoryStream>();
pRichTextXML->Save(pMemStream);
- wsChildren += WideString::FromUTF8(
- ByteStringView(pMemStream->GetBuffer(),
- static_cast<size_t>(pMemStream->GetSize())));
+ wsChildren +=
+ WideString::FromUTF8(ByteStringView(pMemStream->GetSpan()));
} else if (pRawValueNode->GetElementType() == XFA_Element::Sharpxml &&
contentType.has_value() &&
contentType.value().EqualsASCII("text/xml")) {