]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
fix: handle CRLF line endings in AsciiFile parser 1174/head
authorSanket Jadhav <sj546400@gmail.com>
Thu, 30 Jul 2026 05:44:21 +0000 (05:44 +0000)
committerSanket Jadhav <sj546400@gmail.com>
Thu, 30 Jul 2026 05:44:21 +0000 (05:44 +0000)
Signed-off-by: Sanket Jadhav <sj546400@gmail.com>
snapper/AsciiFile.cc
testsuite/Makefile.am
testsuite/sysconfig-crlf.cc [new file with mode: 0644]
testsuite/sysconfig-crlf.txt [new file with mode: 0644]

index f1b4d0cacc4c97e5d6daab6c258e7c37c73953c3..053fde4078bb24f439445e7417a226ec92fdcdb2 100644 (file)
@@ -172,6 +172,9 @@ namespace snapper
        if (n > 0 && buffer[n - 1] == '\n')
            n--;
 
+       if (n > 0 && buffer[n - 1] == '\r')
+           n--;
+
        line = string(buffer, 0, n);
 
        return true;
@@ -348,6 +351,8 @@ namespace snapper
            if (p2)
            {
                line += string(p1, p2 - p1);
+               if (!line.empty() && line.back() == '\r')
+                   line.pop_back();
                buffer_read = p2 - buffer.data() + 1;
                return true;
            }
index a6289e1e044692a5bdb02a257c04c477ac83a09b..7c28f63e42630f74803fda244a89c75987b59b5e 100644 (file)
@@ -6,7 +6,7 @@ AM_CPPFLAGS = -I$(top_srcdir) $(DBUS_CFLAGS)
 
 LDADD = ../snapper/libsnapper.la ../dbus/libdbus.la -lboost_unit_test_framework
 
-check_PROGRAMS = sysconfig-get1.test dirname1.test basename1.test              \
+check_PROGRAMS = sysconfig-get1.test sysconfig-crlf.test dirname1.test basename1.test          \
        equal-date.test cmp-lt.test humanstring.test uuid.test                  \
        table.test table-formatter.test csv-formatter.test json-formatter.test  \
        getopts.test scan-datetime.test root-prefix.test range.test limit.test
@@ -23,7 +23,7 @@ TESTS = $(check_PROGRAMS)
 
 AM_DEFAULT_SOURCE_EXT = .cc
 
-EXTRA_DIST = $(noinst_SCRIPTS) sysconfig-get1.txt sysconfig-set1.txt
+EXTRA_DIST = $(noinst_SCRIPTS) sysconfig-get1.txt sysconfig-set1.txt sysconfig-crlf.txt
 
 equal_date_test_LDADD = -lboost_unit_test_framework ../client/utils/libutils.la
 
diff --git a/testsuite/sysconfig-crlf.cc b/testsuite/sysconfig-crlf.cc
new file mode 100644 (file)
index 0000000..b6039db
--- /dev/null
@@ -0,0 +1,19 @@
+
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_MODULE sysconfig_crlf
+
+#include <boost/test/unit_test.hpp>
+
+#include <snapper/AsciiFile.h>
+
+using namespace snapper;
+
+
+BOOST_AUTO_TEST_CASE(sysconfig_crlf)
+{
+    SysconfigFile s("sysconfig-crlf.txt");
+
+    string value;
+    BOOST_REQUIRE(s.get_value("KEY", value));
+    BOOST_CHECK_EQUAL(value, "value");
+}
diff --git a/testsuite/sysconfig-crlf.txt b/testsuite/sysconfig-crlf.txt
new file mode 100644 (file)
index 0000000..5e3aaa1
--- /dev/null
@@ -0,0 +1 @@
+KEY="value"