Fix Linux build and tests
RegexEngine.cc uses std::uint32_t without including <cstdint>; 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 <noreply@anthropic.com>
This commit is contained in:
@@ -158,7 +158,9 @@ static const mode_t g_process_umask = [] {
|
|||||||
static bool
|
static bool
|
||||||
write_in_place(const std::string &path, const ByteChunks &data, std::string &err)
|
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
|
#ifdef O_CLOEXEC
|
||||||
flags |= O_CLOEXEC;
|
flags |= O_CLOEXEC;
|
||||||
#endif
|
#endif
|
||||||
@@ -168,7 +170,12 @@ write_in_place(const std::string &path, const ByteChunks &data, std::string &err
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
struct stat before{};
|
struct stat before{};
|
||||||
const bool have_mode = ::fstat(fd, &before) == 0;
|
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);
|
bool ok = write_all_fd(fd, data, err);
|
||||||
// Without privilege, writing clears setuid/setgid; put them back.
|
// Without privilege, writing clears setuid/setgid; put them back.
|
||||||
if (ok && have_mode)
|
if (ok && have_mode)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "RegexEngine.h"
|
#include "RegexEngine.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
|
||||||
#include "RegexGuard.h"
|
#include "RegexGuard.h"
|
||||||
|
|||||||
+11
-11
@@ -324,7 +324,7 @@ TEST (Undo_RoundTrip_Lossless_RandomEdits)
|
|||||||
|
|
||||||
// Build a short random sequence of inserts and deletes.
|
// Build a short random sequence of inserts and deletes.
|
||||||
for (int i = 0; i < 200; ++i) {
|
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);
|
const bool do_insert = (cur.empty() || pick(rng) == 0);
|
||||||
if (do_insert) {
|
if (do_insert) {
|
||||||
char c = static_cast<char>(ch(rng));
|
char c = static_cast<char>(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.
|
// Undo back to start.
|
||||||
for (int i = 0; i < 1000; ++i) {
|
for (int i = 0; i < 1000; ++i) {
|
||||||
std::string before = b.AsString();
|
std::string before = std::string(b.ContentView());
|
||||||
u->undo();
|
u->undo();
|
||||||
if (b.AsString() == before)
|
if (std::string(b.ContentView()) == before)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Redo forward; should end at exact final bytes.
|
// Redo forward; should end at exact final bytes.
|
||||||
for (int i = 0; i < 1000; ++i) {
|
for (int i = 0; i < 1000; ++i) {
|
||||||
std::string before = b.AsString();
|
std::string before = std::string(b.ContentView());
|
||||||
u->redo(0);
|
u->redo(0);
|
||||||
if (b.AsString() == before)
|
if (std::string(b.ContentView()) == before)
|
||||||
break;
|
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).
|
// 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"));
|
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
|
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).
|
// Group: delete content rows then insert replacements (simulates reflow).
|
||||||
(void) u->BeginGroup();
|
(void) u->BeginGroup();
|
||||||
@@ -1652,15 +1652,15 @@ TEST (Undo_GroupedDeleteAndInsertRows_UndoesAsUnit)
|
|||||||
|
|
||||||
u->EndGroup();
|
u->EndGroup();
|
||||||
|
|
||||||
const std::string reflowed = b.AsString();
|
const std::string reflowed = std::string(b.ContentView());
|
||||||
|
|
||||||
// Single undo should restore original content.
|
// Single undo should restore original content.
|
||||||
u->undo();
|
u->undo();
|
||||||
ASSERT_EQ(b.AsString(), original);
|
ASSERT_EQ(std::string(b.ContentView()), original);
|
||||||
|
|
||||||
// Redo should restore the reflowed state.
|
// Redo should restore the reflowed state.
|
||||||
u->redo();
|
u->redo();
|
||||||
ASSERT_EQ(b.AsString(), reflowed);
|
ASSERT_EQ(std::string(b.ContentView()), reflowed);
|
||||||
|
|
||||||
validate_undo_tree(*u);
|
validate_undo_tree(*u);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user