crashpad/util/file/delimited_file_reader.cc
Mark Mentovai cc166d71f4 Use base::size where appropriate, and ArraySize elsewhere
This is a follow-up to c8a016b99d97, following the post-landing
discussion at
https://chromium-review.googlesource.com/c/crashpad/crashpad/+/1393921/5#message-2058541d8c4505d20a990ab7734cd758e437a5f7

base::size, and std::size that will eventually replace it when C++17 is
assured, does not allow the size of non-static data members to be taken
in constant expression context. The remaining uses of ArraySize are in:

minidump/minidump_exception_writer.cc (×1)
minidump/minidump_system_info_writer.cc (×2, also uses base::size)
snapshot/cpu_context.cc (×4, also uses base::size)
util/misc/arraysize_test.cc (×10, of course)

The first of these occurs when initializing a constexpr variable. All
others are in expressions used with static_assert.

Includes:
Update mini_chromium to 737433ebade4d446643c6c07daae02a67e8deccao

f701716d9546 Add Windows ARM64 build target to mini_chromium
87a95a3d6ac2 Remove the arraysize macro
1f7255ead1f7 Placate MSVC in areas of base::size usage
737433ebade4 Add cast

Bug: chromium:837308
Change-Id: I6a5162654461b1bdd9b7b6864d0d71a734bcde19
Reviewed-on: https://chromium-review.googlesource.com/c/1396108
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
2019-01-04 22:42:57 +00:00

111 lines
3.9 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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"
#include "base/stl_util.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 wasnt terminated by a delimiter
// character.
//
// This is EOF, but EOF cant be returned because theres 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), base::size(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