From db1fc92fdd012affd1db9fc05d419032d2fe3ec9 Mon Sep 17 00:00:00 2001 From: Tim Stack Date: Thu, 30 Mar 2023 22:06:19 -0700 Subject: [PATCH] [ansi] missed resizing a buffer when scrubbing ansi Fixes #1135 --- src/logfile.cc | 2 +- src/shared_buffer.cc | 14 ++++++++------ src/shared_buffer.hh | 13 ++++++++++--- test/Makefile.am | 1 + ...sh_7b00f32a3fff7fc2d78a87045ae842e58be88480.err | 0 ...sh_7b00f32a3fff7fc2d78a87045ae842e58be88480.out | 10 ++++++++++ test/test_text_file.sh | 3 +++ test/textfile_ansi_expanding.0 | 10 ++++++++++ 8 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 test/expected/test_text_file.sh_7b00f32a3fff7fc2d78a87045ae842e58be88480.err create mode 100644 test/expected/test_text_file.sh_7b00f32a3fff7fc2d78a87045ae842e58be88480.out create mode 100644 test/textfile_ansi_expanding.0 diff --git a/src/logfile.cc b/src/logfile.cc index 9cac0ea7..e2dedda1 100644 --- a/src/logfile.cc +++ b/src/logfile.cc @@ -610,7 +610,7 @@ logfile::rebuild_index(nonstd::optional deadline) auto tmp_line = sbr.to_string_fragment().to_string(); scrub_ansi_string(tmp_line, nullptr); - memcpy(sbr.get_writable_data(), + memcpy(sbr.get_writable_data(tmp_line.length()), tmp_line.c_str(), tmp_line.length()); sbr.narrow(0, tmp_line.length()); diff --git a/src/shared_buffer.cc b/src/shared_buffer.cc index 7ad168ac..7102884e 100644 --- a/src/shared_buffer.cc +++ b/src/shared_buffer.cc @@ -112,16 +112,18 @@ shared_buffer_ref::shared_buffer_ref(shared_buffer_ref&& other) noexcept } bool -shared_buffer_ref::take_ownership() +shared_buffer_ref::take_ownership(size_t length) { - if (this->sb_owner != nullptr && this->sb_data != nullptr) { - char* new_data; - - if ((new_data = (char*) malloc(this->sb_length)) == nullptr) { + if ((this->sb_owner != nullptr && this->sb_data != nullptr) + || this->sb_length != length) + { + auto* new_data = (char*) malloc(length); + if (new_data == nullptr) { return false; } - memcpy(new_data, this->sb_data, this->sb_length); + memcpy(new_data, this->sb_data, std::min(length, this->sb_length)); + this->sb_length = length; this->sb_data = new_data; this->sb_owner->sb_refs.erase(find(this->sb_owner->sb_refs.begin(), this->sb_owner->sb_refs.end(), diff --git a/src/shared_buffer.hh b/src/shared_buffer.hh index 61aa5e0d..e0029e9b 100644 --- a/src/shared_buffer.hh +++ b/src/shared_buffer.hh @@ -112,15 +112,20 @@ public: file_range::metadata& get_metadata() { return this->sb_metadata; } - char* get_writable_data() + char* get_writable_data(size_t length) { - if (this->take_ownership()) { + if (this->take_ownership(length)) { return const_cast(this->sb_data); } return nullptr; } + char* get_writable_data() + { + return this->get_writable_data(this->sb_length); + } + string_fragment to_string_fragment(off_t offset, size_t len) const { return string_fragment{ @@ -151,7 +156,9 @@ public: void erase_ansi(); - bool take_ownership(); + bool take_ownership(size_t length); + + bool take_ownership() { return this->take_ownership(this->sb_length); } void disown(); diff --git a/test/Makefile.am b/test/Makefile.am index 85eb48d7..e1f52128 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -354,6 +354,7 @@ dist_noinst_DATA = \ mvwattrline_output.0 \ textfile_0.md \ textfile_ansi.0 \ + textfile_ansi_expanding.0 \ textfile_json_indented.0 \ textfile_json_one_line.0 \ textfile_quoted_json.0 \ diff --git a/test/expected/test_text_file.sh_7b00f32a3fff7fc2d78a87045ae842e58be88480.err b/test/expected/test_text_file.sh_7b00f32a3fff7fc2d78a87045ae842e58be88480.err new file mode 100644 index 00000000..e69de29b diff --git a/test/expected/test_text_file.sh_7b00f32a3fff7fc2d78a87045ae842e58be88480.out b/test/expected/test_text_file.sh_7b00f32a3fff7fc2d78a87045ae842e58be88480.out new file mode 100644 index 00000000..4855b3de --- /dev/null +++ b/test/expected/test_text_file.sh_7b00f32a3fff7fc2d78a87045ae842e58be88480.out @@ -0,0 +1,10 @@ + * Package: sys-libs/glibc-2.36-r7:2.2 + * Repository: gentoo + * Maintainer: toolchain@gentoo.org + * USE: abi_x86_64 amd64 caps elibc_glibc kernel_linux multiarch ssp static-libs test userland_GNU + * FEATURES: network-sandbox preserve-libs sandbox test userpriv usersandbox + * Checking whether python3_11 is suitable ... + * >=dev-lang/python-3.11.1-r1:3.11 ... + [ ok ] + * Using python3.11 to build (via PYTHON_COMPAT iteration) +>>> Unpacking source... diff --git a/test/test_text_file.sh b/test/test_text_file.sh index 9f326a9d..166f7907 100644 --- a/test/test_text_file.sh +++ b/test/test_text_file.sh @@ -31,3 +31,6 @@ run_cap_test ${lnav_test} -n \ run_cap_test ${lnav_test} -n \ -c ';SELECT * FROM lnav_file_metadata' \ ${test_dir}/textfile_0.md + +run_cap_test ${lnav_test} -n \ + ${test_dir}/textfile_ansi_expanding.0 diff --git a/test/textfile_ansi_expanding.0 b/test/textfile_ansi_expanding.0 new file mode 100644 index 00000000..2fefb6f3 --- /dev/null +++ b/test/textfile_ansi_expanding.0 @@ -0,0 +1,10 @@ + * Package: sys-libs/glibc-2.36-r7:2.2 + * Repository: gentoo + * Maintainer: toolchain@gentoo.org + * USE: abi_x86_64 amd64 caps elibc_glibc kernel_linux multiarch ssp static-libs test userland_GNU + * FEATURES: network-sandbox preserve-libs sandbox test userpriv usersandbox + * Checking whether python3_11 is suitable ... + * >=dev-lang/python-3.11.1-r1:3.11 ... + [ ok ] + * Using python3.11 to build (via PYTHON_COMPAT iteration) +>>> Unpacking source...