Skip to content

Commit

Permalink
Fix CONFIG REWRITE append duplicate newlines
Browse files Browse the repository at this point in the history
In the old code, everytime we perform a config rewrite, an
additional newline is appended, see #1396

Because iostream::eof will only return true after reading the
end of the stream. It does not indicate, that the next read will
be the end of the stream.

So everytime config rewrite is performed, the old loop will
append a newline in `lines`, and then we will append it to the
conf file.

In addition, the same modification has been made to other similar
places. This PR fixes #1396
  • Loading branch information
enjoy-binbin committed Apr 20, 2023
1 parent d2aa7b5 commit 8daaf6a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/cluster/cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ Status Cluster::LoadClusterNodes(const std::string &file_path) {
int64_t version = -1;
std::string id, nodes_info;
std::string line;
while (!file.eof()) {
std::getline(file, line);
while (std::getline(file, line)) {
if (file.eof()) break;

auto parsed = ParseConfigLine(line);
if (!parsed) return parsed.ToStatus().Prefixed("malformed line");
Expand Down
4 changes: 2 additions & 2 deletions src/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,8 @@ Status Config::Rewrite() {
std::ifstream file(path_);
if (file.is_open()) {
std::string raw_line;
while (!file.eof()) {
std::getline(file, raw_line);
while (std::getline(file, raw_line)) {
if (file.eof()) break;
auto parsed = ParseConfigLine(raw_line);
if (!parsed || parsed->first.empty()) {
lines.emplace_back(raw_line);
Expand Down
4 changes: 2 additions & 2 deletions utils/kvrocks2redis/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ Status Config::Load(std::string path) {

std::string line;
int line_num = 1;
while (!file.eof()) {
std::getline(file, line);
while (std::getline(file, line)) {
if (file.eof()) break;
Status s = parseConfigFromString(line);
if (!s.IsOK()) {
return s.Prefixed(fmt::format("at line #L{}", line_num));
Expand Down

0 comments on commit 8daaf6a

Please sign in to comment.