You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by hu...@apache.org on 2023/04/20 12:58:29 UTC

[incubator-kvrocks] branch unstable updated: Fix CONFIG REWRITE append duplicate newlines (#1397)

This is an automated email from the ASF dual-hosted git repository.

hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new 1f8fa1ac Fix CONFIG REWRITE append duplicate newlines (#1397)
1f8fa1ac is described below

commit 1f8fa1ac584847f6146ebdd20ac51285976bd89c
Author: Binbin <bi...@qq.com>
AuthorDate: Thu Apr 20 20:58:23 2023 +0800

    Fix CONFIG REWRITE append duplicate newlines (#1397)
    
    In the old code, every time 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 every time 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
---
 src/cluster/cluster.cc        | 4 +---
 src/config/config.cc          | 6 ++----
 utils/kvrocks2redis/config.cc | 3 +--
 3 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/src/cluster/cluster.cc b/src/cluster/cluster.cc
index e7d76efc..9c4e4986 100644
--- a/src/cluster/cluster.cc
+++ b/src/cluster/cluster.cc
@@ -613,9 +613,7 @@ 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 (file.good() && std::getline(file, line)) {
     auto parsed = ParseConfigLine(line);
     if (!parsed) return parsed.ToStatus().Prefixed("malformed line");
     if (parsed->first.empty() || parsed->second.empty()) continue;
diff --git a/src/config/config.cc b/src/config/config.cc
index 3e3c2866..8016b063 100644
--- a/src/config/config.cc
+++ b/src/config/config.cc
@@ -726,8 +726,7 @@ Status Config::Load(const CLIOptions &opts) {
 
     std::string line;
     int line_num = 1;
-    while (!in->eof()) {
-      std::getline(*in, line);
+    while (in->good() && std::getline(*in, line)) {
       if (auto s = parseConfigFromString(line, line_num); !s.IsOK()) {
         return s.Prefixed(fmt::format("at line #L{}", line_num));
       }
@@ -829,8 +828,7 @@ Status Config::Rewrite() {
   std::ifstream file(path_);
   if (file.is_open()) {
     std::string raw_line;
-    while (!file.eof()) {
-      std::getline(file, raw_line);
+    while (file.good() && std::getline(file, raw_line)) {
       auto parsed = ParseConfigLine(raw_line);
       if (!parsed || parsed->first.empty()) {
         lines.emplace_back(raw_line);
diff --git a/utils/kvrocks2redis/config.cc b/utils/kvrocks2redis/config.cc
index efa75116..907c9495 100644
--- a/utils/kvrocks2redis/config.cc
+++ b/utils/kvrocks2redis/config.cc
@@ -126,8 +126,7 @@ Status Config::Load(std::string path) {
 
   std::string line;
   int line_num = 1;
-  while (!file.eof()) {
-    std::getline(file, line);
+  while (file.good() && std::getline(file, line)) {
     Status s = parseConfigFromString(line);
     if (!s.IsOK()) {
       return s.Prefixed(fmt::format("at line #L{}", line_num));