From 4c8156ad9bc401cb437b63786836dbe9806dd5e8 Mon Sep 17 00:00:00 2001 From: "K. Isom" Date: Tue, 17 Nov 2026 12:33:28 -0800 Subject: [PATCH] Fix Linux build and tests RegexEngine.cc uses std::uint32_t without including ; libc++ pulls it in transitively, libstdc++ does not. The in-place save for hard-linked files read the mode after opening with O_TRUNC, but Linux clears setuid/setgid on that truncate, so the bits were lost. Read the mode first, then ftruncate. Two undo tests compared Buffer::AsString(), a debug label, instead of the contents, so they only checked line counts; the random-edits test also deleted from an empty buffer under libstdc++'s RNG sequence. Compare ContentView() instead. Co-Authored-By: Claude Opus 5.5 --- Buffer.cc | 13 ++++++++++--- RegexEngine.cc | 1 + tests/test_undo.cc | 22 +++++++++++----------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Buffer.cc b/Buffer.cc index e062184..a54b238 100644 --- a/Buffer.cc +++ b/Buffer.cc @@ -158,7 +158,9 @@ static const mode_t g_process_umask = [] { static bool write_in_place(const std::string &path, const ByteChunks &data, std::string &err) { - int flags = O_WRONLY | O_TRUNC; + // No O_TRUNC: Linux clears setuid/setgid when the open truncates, so + // read the mode first and truncate afterwards. + int flags = O_WRONLY; #ifdef O_CLOEXEC flags |= O_CLOEXEC; #endif @@ -168,8 +170,13 @@ write_in_place(const std::string &path, const ByteChunks &data, std::string &err return false; } struct stat before{}; - const bool have_mode = ::fstat(fd, &before) == 0; - bool ok = write_all_fd(fd, data, err); + const bool have_mode = kte::syscall::Fstat(fd, &before) == 0; + if (kte::syscall::Ftruncate(fd, 0) != 0) { + err = std::string("Failed to truncate file: ") + std::strerror(errno); + (void) kte::syscall::Close(fd); + return false; + } + bool ok = write_all_fd(fd, data, err); // Without privilege, writing clears setuid/setgid; put them back. if (ok && have_mode) (void) kte::syscall::Fchmod(fd, before.st_mode & 07777); diff --git a/RegexEngine.cc b/RegexEngine.cc index c37233c..9ab7601 100644 --- a/RegexEngine.cc +++ b/RegexEngine.cc @@ -1,5 +1,6 @@ #include "RegexEngine.h" +#include #include #include "RegexGuard.h" diff --git a/tests/test_undo.cc b/tests/test_undo.cc index c0b03af..f850245 100644 --- a/tests/test_undo.cc +++ b/tests/test_undo.cc @@ -324,7 +324,7 @@ TEST (Undo_RoundTrip_Lossless_RandomEdits) // Build a short random sequence of inserts and deletes. for (int i = 0; i < 200; ++i) { - const std::string cur = b.AsString(); + const std::string cur = std::string(b.ContentView()); const bool do_insert = (cur.empty() || pick(rng) == 0); if (do_insert) { char c = static_cast(ch(rng)); @@ -347,22 +347,22 @@ TEST (Undo_RoundTrip_Lossless_RandomEdits) } } - const std::string final = b.AsString(); + const std::string final = std::string(b.ContentView()); // Undo back to start. for (int i = 0; i < 1000; ++i) { - std::string before = b.AsString(); + std::string before = std::string(b.ContentView()); u->undo(); - if (b.AsString() == before) + if (std::string(b.ContentView()) == before) break; } // Redo forward; should end at exact final bytes. for (int i = 0; i < 1000; ++i) { - std::string before = b.AsString(); + std::string before = std::string(b.ContentView()); u->redo(0); - if (b.AsString() == before) + if (std::string(b.ContentView()) == before) break; } - ASSERT_EQ(b.AsString(), final); + ASSERT_EQ(std::string(b.ContentView()), final); } @@ -1623,7 +1623,7 @@ TEST (Undo_GroupedDeleteAndInsertRows_UndoesAsUnit) // Seed three lines (with trailing newline so delete_row/insert_row work cleanly). b.insert_text(0, 0, std::string_view("aaa\nbbb\nccc\n")); ASSERT_EQ(b.Nrows(), (std::size_t) 4); // 3 content + 1 empty trailing - const std::string original = b.AsString(); + const std::string original = std::string(b.ContentView()); // Group: delete content rows then insert replacements (simulates reflow). (void) u->BeginGroup(); @@ -1652,15 +1652,15 @@ TEST (Undo_GroupedDeleteAndInsertRows_UndoesAsUnit) u->EndGroup(); - const std::string reflowed = b.AsString(); + const std::string reflowed = std::string(b.ContentView()); // Single undo should restore original content. u->undo(); - ASSERT_EQ(b.AsString(), original); + ASSERT_EQ(std::string(b.ContentView()), original); // Redo should restore the reflowed state. u->redo(); - ASSERT_EQ(b.AsString(), reflowed); + ASSERT_EQ(std::string(b.ContentView()), reflowed); validate_undo_tree(*u); }