Skip to content

Commit

Permalink
[pdf] Fix PDFiumFormFiller::Form_GetFilePath()
Browse files Browse the repository at this point in the history
Its return value should account for the trailing null.

Change-Id: I255078c1007c967b6e648362d11eae868d23c501
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3219441
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#930867}
  • Loading branch information
Daniel Hosseinian authored and Chromium LUCI CQ committed Oct 13, 2021
1 parent 90aa90d commit f83005f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
9 changes: 6 additions & 3 deletions pdf/pdfium/pdfium_form_filler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,12 @@ int PDFiumFormFiller::Form_GetFilePath(IPDF_JSPLATFORM* param,
EngineInIsolateScope engine_scope = GetEngineInIsolateScope(param);
PDFiumEngine* engine = engine_scope.engine();
std::string rv = engine->client_->GetURL();
if (file_path && rv.size() <= static_cast<size_t>(length))
memcpy(file_path, rv.c_str(), rv.size());
return rv.size();

// Account for the trailing null.
int necessary_length = rv.size() + 1;
if (file_path && necessary_length <= length)
memcpy(file_path, rv.c_str(), necessary_length);
return necessary_length;
}

// static
Expand Down
30 changes: 11 additions & 19 deletions pdf/pdfium/pdfium_form_filler_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace {

using ::testing::Contains;
using ::testing::InSequence;
using ::testing::Not;
using ::testing::Return;

class FormFillerTestClient : public TestClient {
Expand Down Expand Up @@ -234,32 +233,27 @@ TEST_F(FormFillerJavaScriptTest, GetFilePath) {
EXPECT_CALL(client, GetURL).Times(2).WillRepeatedly(Return(kTestPath));
PDFiumEngine engine(&client, PDFiumFormFiller::ScriptOption::kJavaScript);

// TODO(dhoss): The return value should be `kTestPathSize`.
EXPECT_EQ(TriggerGetFilePath(engine, /*file_path=*/nullptr, /*length=*/0),
kTestPathSize - 1);
kTestPathSize);

std::vector<char> buffer(kTestPathSize, 'X');
EXPECT_EQ(TriggerGetFilePath(engine, buffer.data(), buffer.size()),
kTestPathSize - 1);

// TODO(dhoss): `buffer.data()` should be null terminated.
EXPECT_STRNE(buffer.data(), kTestPath);
kTestPathSize);
EXPECT_STREQ(buffer.data(), kTestPath);
}

TEST_F(FormFillerJavaScriptTest, GetFilePathEmpty) {
FormFillerTestClient client;
EXPECT_CALL(client, GetURL).Times(2).WillRepeatedly(Return(std::string()));
PDFiumEngine engine(&client, PDFiumFormFiller::ScriptOption::kJavaScript);

// TODO(dhoss): The return value should be 1.
EXPECT_EQ(TriggerGetFilePath(engine, /*file_path=*/nullptr, /*length=*/0), 0);
EXPECT_EQ(TriggerGetFilePath(engine, /*file_path=*/nullptr, /*length=*/0), 1);

char buffer[] = "buffer";
EXPECT_EQ(TriggerGetFilePath(engine, buffer, /*length=*/1), 0);
EXPECT_EQ(TriggerGetFilePath(engine, buffer, /*length=*/1), 1);

// TODO(dhoss): `buffer` should be "" (i.e., its first character should be a
// null terminator).
EXPECT_STRNE(buffer, "");
// The trailing null should be copied over.
EXPECT_STREQ(buffer, "");
}

TEST_F(FormFillerJavaScriptTest, GetFilePathShortBuffer) {
Expand All @@ -271,14 +265,12 @@ TEST_F(FormFillerJavaScriptTest, GetFilePathShortBuffer) {
PDFiumEngine engine(&client, PDFiumFormFiller::ScriptOption::kJavaScript);

std::vector<char> buffer(kTestPathSize - 1, 'X');

// TODO(dhoss): The return value should be `kTestPathSize`.
EXPECT_EQ(TriggerGetFilePath(engine, buffer.data(), buffer.size()),
kTestPathSize - 1);
kTestPathSize);

// TODO(dhoss): Nothing should be copied over. The buffer size is too small to
// contain a trailing null.
EXPECT_THAT(buffer, Not(Contains('X').Times(buffer.size())));
// Nothing should be copied over. The buffer size is too small to contain a
// trailing null.
EXPECT_THAT(buffer, Contains('X').Times(buffer.size()));
}
#endif // defined(PDF_ENABLE_V8)

Expand Down

0 comments on commit f83005f

Please sign in to comment.