Allow StructElement_Attr_GetName to return buflen with null buffer
The documentation of FPDF_StructElement_Attr_GetName currently
indicates:
```
buffer - A buffer for output. May be NULL. This is only
modified if |buflen| is longer than the length
of the key. Optional, pass null to just
retrieve the size of the buffer needed.
```
However, the current implementation doesn't allow the caller to pass
buffer=NULL to obtain the required buffer length.
Removing the check for `!buffer` is enough to simply populate the
buffer size and return true.
Change-Id: I8249c2fed1c07a133c66e7d38dc9ff56f7de3970
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/110711
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/fpdf_structtree.cpp b/fpdfsdk/fpdf_structtree.cpp
index 76a30be..dc68239 100644
--- a/fpdfsdk/fpdf_structtree.cpp
+++ b/fpdfsdk/fpdf_structtree.cpp
@@ -288,8 +288,9 @@
void* buffer,
unsigned long buflen,
unsigned long* out_buflen) {
- if (!out_buflen || !buffer)
+ if (!out_buflen) {
return false;
+ }
const CPDF_Dictionary* dict =
CPDFDictionaryFromFPDFStructElementAttr(struct_attribute);
diff --git a/fpdfsdk/fpdf_structtree_embeddertest.cpp b/fpdfsdk/fpdf_structtree_embeddertest.cpp
index 0b9ca77..1ee1612 100644
--- a/fpdfsdk/fpdf_structtree_embeddertest.cpp
+++ b/fpdfsdk/fpdf_structtree_embeddertest.cpp
@@ -595,6 +595,11 @@
ASSERT_EQ(2, FPDF_StructElement_Attr_GetCount(attr));
ASSERT_FALSE(
FPDF_StructElement_Attr_GetName(attr, 1, nullptr, 0U, nullptr));
+ unsigned long buffer_len_needed = ULONG_MAX;
+ // Pass buffer = nullptr to obtain the size of the buffer needed,
+ ASSERT_TRUE(FPDF_StructElement_Attr_GetName(attr, 1, nullptr, 0,
+ &buffer_len_needed));
+ EXPECT_EQ(2U, buffer_len_needed);
char buffer[8] = {};
unsigned long out_len = ULONG_MAX;
// Deliberately pass in a small buffer size to make sure `buffer` remains