Add DelimitedFileReader and use it in Linux/Android’s ProcessInfo
This implements a non-stdio-based getline() equivalent. getline() is not in the Android NDK until API 21 (Android 5.0.0), while Chrome builds for 32-bit platforms with API 16 (Android 4.1.0). Although a getline() declaration could be provided in compat for use with older NDK headers, it’s desirable to move away from stdio entirely. The C++ DelimitedFileReader interface is also a bit more comfortable to use than getline(). A getdelim() equivalent is also provided, and is also used in the Linux/Android ProcessInfo implementation. Bug: crashpad:30 Test: crashpad_util_test FileLineReader.*:ProcessInfo.* Change-Id: Ic1664758a87cfe4953ab22bd3ae190761404b22c Reviewed-on: https://chromium-review.googlesource.com/455998 Reviewed-by: Joshua Peraza <jperaza@chromium.org> Commit-Queue: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Родитель
b10d9118de
Коммит
51b21d8874
|
@ -0,0 +1,109 @@
|
|||
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "util/file/delimited_file_reader.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "base/numerics/safe_conversions.h"
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
DelimitedFileReader::DelimitedFileReader(FileReaderInterface* file_reader)
|
||||
: file_reader_(file_reader), buf_pos_(0), buf_len_(0), eof_(false) {
|
||||
static_assert(sizeof(buf_) <= std::numeric_limits<decltype(buf_pos_)>::max(),
|
||||
"buf_pos_ must cover buf_");
|
||||
static_assert(sizeof(buf_) <= std::numeric_limits<decltype(buf_len_)>::max(),
|
||||
"buf_len_ must cover buf_");
|
||||
}
|
||||
|
||||
DelimitedFileReader::~DelimitedFileReader() {}
|
||||
|
||||
DelimitedFileReader::Result DelimitedFileReader::GetDelim(char delimiter,
|
||||
std::string* field) {
|
||||
if (eof_) {
|
||||
DCHECK_EQ(buf_pos_, buf_len_);
|
||||
|
||||
// Allow subsequent calls to attempt to read more data from the file. If the
|
||||
// file is still at EOF in the future, the read will return 0 and cause
|
||||
// kEndOfFile to be returned anyway.
|
||||
eof_ = false;
|
||||
|
||||
return Result::kEndOfFile;
|
||||
}
|
||||
|
||||
std::string local_field;
|
||||
while (true) {
|
||||
if (buf_pos_ == buf_len_) {
|
||||
// buf_ is empty. Refill it.
|
||||
FileOperationResult read_result = file_reader_->Read(buf_, sizeof(buf_));
|
||||
if (read_result < 0) {
|
||||
return Result::kError;
|
||||
} else if (read_result == 0) {
|
||||
if (!local_field.empty()) {
|
||||
// The file ended with a field that wasn’t terminated by a delimiter
|
||||
// character.
|
||||
//
|
||||
// This is EOF, but EOF can’t be returned because there’s a field that
|
||||
// needs to be returned to the caller. Cache the detected EOF so it
|
||||
// can be returned next time. This is done to support proper semantics
|
||||
// for weird “files” like terminal input that can reach EOF and then
|
||||
// “grow”, allowing subsequent reads past EOF to block while waiting
|
||||
// for more data. Once EOF is detected by a read that returns 0, that
|
||||
// EOF signal should propagate to the caller before attempting a new
|
||||
// read. Here, it will be returned on the next call to this method
|
||||
// without attempting to read more data.
|
||||
eof_ = true;
|
||||
field->swap(local_field);
|
||||
return Result::kSuccess;
|
||||
}
|
||||
return Result::kEndOfFile;
|
||||
}
|
||||
|
||||
DCHECK_LE(static_cast<size_t>(read_result), arraysize(buf_));
|
||||
DCHECK(
|
||||
base::IsValueInRangeForNumericType<decltype(buf_len_)>(read_result));
|
||||
buf_len_ = static_cast<decltype(buf_len_)>(read_result);
|
||||
buf_pos_ = 0;
|
||||
}
|
||||
|
||||
const char* const start = buf_ + buf_pos_;
|
||||
const char* const end = buf_ + buf_len_;
|
||||
const char* const found = std::find(start, end, delimiter);
|
||||
|
||||
local_field.append(start, found);
|
||||
buf_pos_ = static_cast<decltype(buf_pos_)>(found - buf_);
|
||||
DCHECK_LE(buf_pos_, buf_len_);
|
||||
|
||||
if (found != end) {
|
||||
// A real delimiter character was found. Append it to the field being
|
||||
// built and return it.
|
||||
local_field.push_back(delimiter);
|
||||
++buf_pos_;
|
||||
DCHECK_LE(buf_pos_, buf_len_);
|
||||
field->swap(local_field);
|
||||
return Result::kSuccess;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DelimitedFileReader::Result DelimitedFileReader::GetLine(std::string* line) {
|
||||
return GetDelim('\n', line);
|
||||
}
|
||||
|
||||
} // namespace crashpad
|
|
@ -0,0 +1,93 @@
|
|||
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef CRASHPAD_UTIL_FILE_DELIMITED_FILE_READER_H_
|
||||
#define CRASHPAD_UTIL_FILE_DELIMITED_FILE_READER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/macros.h"
|
||||
#include "util/file/file_reader.h"
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
//! \brief Reads a file one field or line at a time.
|
||||
//!
|
||||
//! The file is interpreted as a series of fields separated by delimiter
|
||||
//! characters. When the delimiter character is the newline character
|
||||
//! (<code>'\\n'</code>), the file is interpreted as a series of lines.
|
||||
//!
|
||||
//! It is safe to mix GetDelim() and GetLine() calls, if appropriate for the
|
||||
//! format being interpreted.
|
||||
//!
|
||||
//! This is a replacement for the standard library’s `getdelim()` and
|
||||
//! `getline()` functions, adapted to work with FileReaderInterface objects
|
||||
//! instead of `FILE*` streams.
|
||||
class DelimitedFileReader {
|
||||
public:
|
||||
//! \brief The result of a GetDelim() or GetLine() call.
|
||||
enum class Result {
|
||||
//! \brief An error occurred, and a message was logged.
|
||||
kError = -1,
|
||||
|
||||
//! \brief A field or line was read from the file.
|
||||
kSuccess,
|
||||
|
||||
//! \brief The end of the file was encountered.
|
||||
kEndOfFile,
|
||||
};
|
||||
|
||||
explicit DelimitedFileReader(FileReaderInterface* file_reader);
|
||||
~DelimitedFileReader();
|
||||
|
||||
//! \brief Reads a single field from the file.
|
||||
//!
|
||||
//! \param[in] delimiter The delimiter character that terminates the field.
|
||||
//! It is safe to call this method multiple times while changing the value
|
||||
//! of this parameter, if appropriate for the format being interpreted.
|
||||
//! \param[out] field The field read from the file. This parameter will
|
||||
//! include the field’s terminating delimiter character unless the field
|
||||
//! was at the end of the file and was read without such a character.
|
||||
//! This parameter will not be empty.
|
||||
//!
|
||||
//! \return a #Result value. \a field is only valid when Result::kSuccess is
|
||||
//! returned.
|
||||
Result GetDelim(char delimiter, std::string* field);
|
||||
|
||||
//! \brief Reads a single line from the file.
|
||||
//!
|
||||
//! \param[out] line The line read from the file. This parameter will include
|
||||
//! the line terminating delimiter character unless the line was at the
|
||||
//! end of the file and was read without such a character. This parameter
|
||||
//! will not be empty.
|
||||
//!
|
||||
//! \return a #Result value. \a line is only valid when Result::kSuccess is
|
||||
//! returned.
|
||||
Result GetLine(std::string* line);
|
||||
|
||||
private:
|
||||
char buf_[4096];
|
||||
FileReaderInterface* file_reader_; // weak
|
||||
uint16_t buf_pos_; // Index into buf_ of the start of the next field.
|
||||
uint16_t buf_len_; // The size of buf_ that’s been filled.
|
||||
bool eof_; // Caches the EOF signal when detected following a partial field.
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(DelimitedFileReader);
|
||||
};
|
||||
|
||||
} // namespace crashpad
|
||||
|
||||
#endif // CRASHPAD_UTIL_FILE_DELIMITED_FILE_READER_H_
|
|
@ -0,0 +1,363 @@
|
|||
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "util/file/delimited_file_reader.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "base/format_macros.h"
|
||||
#include "base/strings/stringprintf.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "util/file/string_file.h"
|
||||
|
||||
namespace crashpad {
|
||||
namespace test {
|
||||
namespace {
|
||||
|
||||
TEST(DelimitedFileReader, EmptyFile) {
|
||||
StringFile string_file;
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string line;
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, EmptyOneLineFile) {
|
||||
StringFile string_file;
|
||||
string_file.SetString("\n");
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string line;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(string_file.string(), line);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, SmallOneLineFile) {
|
||||
StringFile string_file;
|
||||
string_file.SetString("one line\n");
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string line;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(string_file.string(), line);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, SmallOneLineFileWithoutNewline) {
|
||||
StringFile string_file;
|
||||
string_file.SetString("no newline");
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string line;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(string_file.string(), line);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, SmallMultiLineFile) {
|
||||
StringFile string_file;
|
||||
string_file.SetString("first\nsecond line\n3rd\n");
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string line;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ("first\n", line);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ("second line\n", line);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ("3rd\n", line);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, SmallMultiFieldFile) {
|
||||
StringFile string_file;
|
||||
string_file.SetString("first,second field\ntwo lines,3rd,");
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string field;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetDelim(',', &field));
|
||||
EXPECT_EQ("first,", field);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetDelim(',', &field));
|
||||
EXPECT_EQ("second field\ntwo lines,", field);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetDelim(',', &field));
|
||||
EXPECT_EQ("3rd,", field);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetDelim(',', &field));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetDelim(',', &field));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, SmallMultiFieldFile_MixedDelimiters) {
|
||||
StringFile string_file;
|
||||
string_file.SetString("first,second, still 2nd\t3rd\nalso\tnewline\n55555$");
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string field;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetDelim(',', &field));
|
||||
EXPECT_EQ("first,", field);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetDelim('\t', &field));
|
||||
EXPECT_EQ("second, still 2nd\t", field);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&field));
|
||||
EXPECT_EQ("3rd\n", field);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetDelim('\n', &field));
|
||||
EXPECT_EQ("also\tnewline\n", field);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetDelim('$', &field));
|
||||
EXPECT_EQ("55555$", field);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetDelim('?', &field));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&field));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, EmptyLineMultiLineFile) {
|
||||
StringFile string_file;
|
||||
string_file.SetString("first\n\n\n4444\n");
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string line;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ("first\n", line);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ("\n", line);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ("\n", line);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ("4444\n", line);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, LongOneLineFile) {
|
||||
std::string contents(50000, '!');
|
||||
contents[1] = '?';
|
||||
contents.push_back('\n');
|
||||
|
||||
StringFile string_file;
|
||||
string_file.SetString(contents);
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string line;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(contents, line);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
}
|
||||
|
||||
void TestLongMultiLineFile(int base_length) {
|
||||
std::vector<std::string> lines;
|
||||
std::string contents;
|
||||
for (size_t line_index = 0; line_index <= 'z' - 'a'; ++line_index) {
|
||||
char c = 'a' + static_cast<char>(line_index);
|
||||
|
||||
// Mix up the lengths a little.
|
||||
std::string line(base_length + line_index * ((line_index % 3) - 1), c);
|
||||
|
||||
// Mix up the data a little too.
|
||||
ASSERT_LT(line_index, line.size());
|
||||
line[line_index] -= ('a' - 'A');
|
||||
|
||||
line.push_back('\n');
|
||||
contents.append(line);
|
||||
lines.push_back(line);
|
||||
}
|
||||
|
||||
StringFile string_file;
|
||||
string_file.SetString(contents);
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string line;
|
||||
for (size_t line_index = 0; line_index < lines.size(); ++line_index) {
|
||||
SCOPED_TRACE(base::StringPrintf("line_index %" PRIuS, line_index));
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(lines[line_index], line);
|
||||
}
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, LongMultiLineFile) {
|
||||
TestLongMultiLineFile(500);
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, ReallyLongMultiLineFile) {
|
||||
TestLongMultiLineFile(5000);
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, EmbeddedNUL) {
|
||||
const char kString[] = "embedded\0NUL\n";
|
||||
StringFile string_file;
|
||||
string_file.SetString(std::string(kString, arraysize(kString) - 1));
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string line;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(string_file.string(), line);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, NULDelimiter) {
|
||||
const char kString[] = "aa\0b\0ccc\0";
|
||||
StringFile string_file;
|
||||
string_file.SetString(std::string(kString, arraysize(kString) - 1));
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string field;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetDelim('\0', &field));
|
||||
EXPECT_EQ(std::string("aa\0", 3), field);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetDelim('\0', &field));
|
||||
EXPECT_EQ(std::string("b\0", 2), field);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetDelim('\0', &field));
|
||||
EXPECT_EQ(std::string("ccc\0", 4), field);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetDelim('\0', &field));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetDelim('\0', &field));
|
||||
}
|
||||
|
||||
TEST(DelimitedFileReader, EdgeCases) {
|
||||
const size_t kSizes[] = {4094, 4095, 4096, 4097, 8190, 8191, 8192, 8193};
|
||||
for (size_t index = 0; index < arraysize(kSizes); ++index) {
|
||||
size_t size = kSizes[index];
|
||||
SCOPED_TRACE(
|
||||
base::StringPrintf("index %" PRIuS ", size %" PRIuS, index, size));
|
||||
|
||||
std::string line_0(size, '$');
|
||||
line_0.push_back('\n');
|
||||
|
||||
StringFile string_file;
|
||||
string_file.SetString(line_0);
|
||||
DelimitedFileReader delimited_file_reader(&string_file);
|
||||
|
||||
std::string line;
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(line_0, line);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
std::string line_1(size, '@');
|
||||
line_1.push_back('\n');
|
||||
|
||||
string_file.SetString(line_0 + line_1);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(line_0, line);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(line_1, line);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
line_1[size] = '?';
|
||||
|
||||
string_file.SetString(line_0 + line_1);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(line_0, line);
|
||||
ASSERT_EQ(DelimitedFileReader::Result::kSuccess,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
EXPECT_EQ(line_1, line);
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
|
||||
// The file is still at EOF.
|
||||
EXPECT_EQ(DelimitedFileReader::Result::kEndOfFile,
|
||||
delimited_file_reader.GetLine(&line));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace test
|
||||
} // namespace crashpad
|
|
@ -17,7 +17,6 @@
|
|||
#include <ctype.h>
|
||||
#include <elf.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/ptrace.h>
|
||||
#include <sys/uio.h>
|
||||
|
@ -26,13 +25,14 @@
|
|||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/files/scoped_file.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/posix/eintr_wrapper.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "base/strings/string_piece.h"
|
||||
#include "util/file/delimited_file_reader.h"
|
||||
#include "util/file/file_reader.h"
|
||||
#include "util/string/split_string.h"
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
|
@ -78,14 +78,6 @@ bool AdvancePastNumber(const char** input, T* value) {
|
|||
return false;
|
||||
}
|
||||
|
||||
struct BufferFreer {
|
||||
void operator()(char** buffer_ptr) const {
|
||||
free(*buffer_ptr);
|
||||
*buffer_ptr = nullptr;
|
||||
}
|
||||
};
|
||||
using ScopedBufferPtr = std::unique_ptr<char*, BufferFreer>;
|
||||
|
||||
bool ReadEntireFile(const char* path, std::string* contents) {
|
||||
FileReader file;
|
||||
if (!file.Open(base::FilePath(path))) {
|
||||
|
@ -158,86 +150,98 @@ bool ProcessInfo::Initialize(pid_t pid) {
|
|||
{
|
||||
char path[32];
|
||||
snprintf(path, sizeof(path), "/proc/%d/status", pid_);
|
||||
base::ScopedFILE status_file(fopen(path, "re"));
|
||||
if (!status_file.get()) {
|
||||
PLOG(ERROR) << "fopen " << path;
|
||||
FileReader status_file;
|
||||
if (!status_file.Open(base::FilePath(path))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t buffer_size = 0;
|
||||
char* buffer = nullptr;
|
||||
ScopedBufferPtr buffer_owner(&buffer);
|
||||
DelimitedFileReader status_file_line_reader(&status_file);
|
||||
|
||||
bool have_ppid = false;
|
||||
bool have_uids = false;
|
||||
bool have_gids = false;
|
||||
bool have_groups = false;
|
||||
ssize_t len;
|
||||
while ((len = getline(&buffer, &buffer_size, status_file.get())) > 0) {
|
||||
const char* line = buffer;
|
||||
std::string line;
|
||||
DelimitedFileReader::Result result;
|
||||
while ((result = status_file_line_reader.GetLine(&line)) ==
|
||||
DelimitedFileReader::Result::kSuccess) {
|
||||
if (line.back() != '\n') {
|
||||
LOG(ERROR) << "format error: unterminated line at EOF";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (AdvancePastPrefix(&line, "PPid:\t")) {
|
||||
bool understood_line = false;
|
||||
const char* line_c = line.c_str();
|
||||
if (AdvancePastPrefix(&line_c, "PPid:\t")) {
|
||||
if (have_ppid) {
|
||||
LOG(ERROR) << "format error: multiple PPid lines";
|
||||
return false;
|
||||
}
|
||||
have_ppid = AdvancePastNumber(&line, &ppid_);
|
||||
have_ppid = AdvancePastNumber(&line_c, &ppid_);
|
||||
if (!have_ppid) {
|
||||
LOG(ERROR) << "format error: unrecognized PPid format";
|
||||
return false;
|
||||
}
|
||||
} else if (AdvancePastPrefix(&line, "Uid:\t")) {
|
||||
understood_line = true;
|
||||
} else if (AdvancePastPrefix(&line_c, "Uid:\t")) {
|
||||
if (have_uids) {
|
||||
LOG(ERROR) << "format error: multiple Uid lines";
|
||||
return false;
|
||||
}
|
||||
have_uids =
|
||||
AdvancePastNumber(&line, &uid_) &&
|
||||
AdvancePastPrefix(&line, "\t") &&
|
||||
AdvancePastNumber(&line, &euid_) &&
|
||||
AdvancePastPrefix(&line, "\t") &&
|
||||
AdvancePastNumber(&line, &suid_);
|
||||
uid_t fsuid;
|
||||
have_uids = AdvancePastNumber(&line_c, &uid_) &&
|
||||
AdvancePastPrefix(&line_c, "\t") &&
|
||||
AdvancePastNumber(&line_c, &euid_) &&
|
||||
AdvancePastPrefix(&line_c, "\t") &&
|
||||
AdvancePastNumber(&line_c, &suid_) &&
|
||||
AdvancePastPrefix(&line_c, "\t") &&
|
||||
AdvancePastNumber(&line_c, &fsuid);
|
||||
if (!have_uids) {
|
||||
LOG(ERROR) << "format error: unrecognized Uid format";
|
||||
return false;
|
||||
}
|
||||
} else if (AdvancePastPrefix(&line, "Gid:\t")) {
|
||||
understood_line = true;
|
||||
} else if (AdvancePastPrefix(&line_c, "Gid:\t")) {
|
||||
if (have_gids) {
|
||||
LOG(ERROR) << "format error: multiple Gid lines";
|
||||
return false;
|
||||
}
|
||||
have_gids =
|
||||
AdvancePastNumber(&line, &gid_) &&
|
||||
AdvancePastPrefix(&line, "\t") &&
|
||||
AdvancePastNumber(&line, &egid_) &&
|
||||
AdvancePastPrefix(&line, "\t") &&
|
||||
AdvancePastNumber(&line, &sgid_);
|
||||
gid_t fsgid;
|
||||
have_gids = AdvancePastNumber(&line_c, &gid_) &&
|
||||
AdvancePastPrefix(&line_c, "\t") &&
|
||||
AdvancePastNumber(&line_c, &egid_) &&
|
||||
AdvancePastPrefix(&line_c, "\t") &&
|
||||
AdvancePastNumber(&line_c, &sgid_) &&
|
||||
AdvancePastPrefix(&line_c, "\t") &&
|
||||
AdvancePastNumber(&line_c, &fsgid);
|
||||
if (!have_gids) {
|
||||
LOG(ERROR) << "format error: unrecognized Gid format";
|
||||
return false;
|
||||
}
|
||||
} else if (AdvancePastPrefix(&line, "Groups:\t")) {
|
||||
understood_line = true;
|
||||
} else if (AdvancePastPrefix(&line_c, "Groups:\t")) {
|
||||
if (have_groups) {
|
||||
LOG(ERROR) << "format error: multiple Groups lines";
|
||||
return false;
|
||||
}
|
||||
gid_t group;
|
||||
while (AdvancePastNumber(&line, &group)) {
|
||||
while (AdvancePastNumber(&line_c, &group)) {
|
||||
supplementary_groups_.insert(group);
|
||||
if (!AdvancePastPrefix(&line, " ")) {
|
||||
LOG(ERROR) << "format error";
|
||||
if (!AdvancePastPrefix(&line_c, " ")) {
|
||||
LOG(ERROR) << "format error: unrecognized Groups format";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!AdvancePastPrefix(&line, "\n") || line != buffer + len) {
|
||||
LOG(ERROR) << "format error: unrecognized Groups format";
|
||||
return false;
|
||||
}
|
||||
have_groups = true;
|
||||
understood_line = true;
|
||||
}
|
||||
|
||||
if (understood_line && line_c != &line.back()) {
|
||||
LOG(ERROR) << "format error: unconsumed trailing data";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!feof(status_file.get())) {
|
||||
PLOG(ERROR) << "getline";
|
||||
if (result != DelimitedFileReader::Result::kEndOfFile) {
|
||||
return false;
|
||||
}
|
||||
if (!have_ppid || !have_uids || !have_gids || !have_groups) {
|
||||
|
@ -486,18 +490,29 @@ bool ProcessInfo::Arguments(std::vector<std::string>* argv) const {
|
|||
|
||||
char path[32];
|
||||
snprintf(path, sizeof(path), "/proc/%d/cmdline", pid_);
|
||||
std::string command;
|
||||
if (!ReadEntireFile(path, &command)) {
|
||||
FileReader cmdline_file;
|
||||
if (!cmdline_file.Open(base::FilePath(path))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (command.size() == 0 || command.back() != '\0') {
|
||||
LOG(ERROR) << "format error";
|
||||
DelimitedFileReader cmdline_file_field_reader(&cmdline_file);
|
||||
|
||||
std::vector<std::string> local_argv;
|
||||
std::string argument;
|
||||
DelimitedFileReader::Result result;
|
||||
while ((result = cmdline_file_field_reader.GetDelim('\0', &argument)) ==
|
||||
DelimitedFileReader::Result::kSuccess) {
|
||||
if (argument.back() != '\0') {
|
||||
LOG(ERROR) << "format error";
|
||||
return false;
|
||||
}
|
||||
argument.pop_back();
|
||||
local_argv.push_back(argument);
|
||||
}
|
||||
if (result != DelimitedFileReader::Result::kEndOfFile) {
|
||||
return false;
|
||||
}
|
||||
|
||||
command.pop_back();
|
||||
std::vector<std::string> local_argv = SplitString(command, '\0');
|
||||
argv->swap(local_argv);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -30,6 +30,8 @@
|
|||
'<(INTERMEDIATE_DIR)',
|
||||
],
|
||||
'sources': [
|
||||
'file/delimited_file_reader.cc',
|
||||
'file/delimited_file_reader.h',
|
||||
'file/file_io.cc',
|
||||
'file/file_io.h',
|
||||
'file/file_io_posix.cc',
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
'..',
|
||||
],
|
||||
'sources': [
|
||||
'file/delimited_file_reader_test.cc',
|
||||
'file/file_io_test.cc',
|
||||
'file/string_file_test.cc',
|
||||
'mac/launchd_test.mm',
|
||||
|
|
Загрузка…
Ссылка в новой задаче