diff --git a/.github/workflows/save-laz-leaks.yml b/.github/workflows/save-laz-leaks.yml new file mode 100644 index 0000000..54b4cec --- /dev/null +++ b/.github/workflows/save-laz-leaks.yml @@ -0,0 +1,25 @@ +name: saveLaz LeakSanitizer + +on: + pull_request: + push: + branches: [main] + +permissions: + contents: read + +jobs: + leak-test: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + - name: Install test dependencies + run: | + git submodule update --init 3rd/LASzip 3rd/tracy + sudo apt-get update -qq + sudo apt-get install -y --no-install-recommends nlohmann-json3-dev + - name: Run saveLaz tests with LeakSanitizer + run: | + cmake -S tests/save_laz -B build-laz-test -DCMAKE_BUILD_TYPE=RelWithDebInfo + cmake --build build-laz-test --parallel + ctest --test-dir build-laz-test --output-on-failure diff --git a/code/save_laz.cpp b/code/save_laz.cpp index 9bf8571..4749708 100644 --- a/code/save_laz.cpp +++ b/code/save_laz.cpp @@ -1,6 +1,7 @@ #include "save_laz.h" #include #include +#include #include nlohmann::json mandeye::LazStats::produceStatus() const @@ -62,6 +63,7 @@ std::optional mandeye::saveLaz(const std::string& filename, L fprintf(stderr, "DLL ERROR: creating laszip writer\n"); return nullopt; } + std::unique_ptr writerHandle(laszip_writer, laszip_destroy); // get a pointer to the header of the writer so we can populate it @@ -125,6 +127,7 @@ std::optional mandeye::saveLaz(const std::string& filename, L fprintf(stderr, "DLL ERROR: opening laszip writer for '%s'\n", filename.c_str()); return nullopt; } + std::unique_ptr openWriter(laszip_writer, laszip_close_writer); fprintf(stderr, "writing file '%s' %scompressed\n", filename.c_str(), (compress ? "" : "un")); @@ -179,7 +182,7 @@ std::optional mandeye::saveLaz(const std::string& filename, L stats.m_pointsCount = p_count; // close the writer - if(laszip_close_writer(laszip_writer)) + if(laszip_close_writer(openWriter.release())) { fprintf(stderr, "DLL ERROR: closing laszip writer\n"); return nullopt; @@ -187,7 +190,7 @@ std::optional mandeye::saveLaz(const std::string& filename, L // destroy the writer - if(laszip_destroy(laszip_writer)) + if(laszip_destroy(writerHandle.release())) { fprintf(stderr, "DLL ERROR: destroying laszip writer\n"); return nullopt; @@ -208,4 +211,4 @@ std::optional mandeye::saveLaz(const std::string& filename, L TracyPlot("laz_save_duration_sec", (double)stats.m_saveDurationSec1); return stats; -} \ No newline at end of file +} diff --git a/tests/save_laz/CMakeLists.txt b/tests/save_laz/CMakeLists.txt new file mode 100644 index 0000000..a6a344f --- /dev/null +++ b/tests/save_laz/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.13) +project(save_laz_leak_test LANGUAGES C CXX) + +set(controller_root "${CMAKE_CURRENT_SOURCE_DIR}/../..") +add_subdirectory("${controller_root}/3rd/LASzip" LASzip EXCLUDE_FROM_ALL) + +add_executable(save_laz_test save_laz_test.cpp "${controller_root}/code/save_laz.cpp") +target_compile_features(save_laz_test PRIVATE cxx_std_17) +target_include_directories(save_laz_test PRIVATE + "${controller_root}/code" "${controller_root}/3rd/LASzip/include" "${controller_root}/3rd/tracy/public") +target_link_libraries(save_laz_test PRIVATE laszip) +target_link_options(save_laz_test PRIVATE -fsanitize=leak) + +enable_testing() +add_test(NAME save_laz_success COMMAND save_laz_test success) +add_test(NAME save_laz_open_failure COMMAND save_laz_test open-failure) diff --git a/tests/save_laz/save_laz_test.cpp b/tests/save_laz/save_laz_test.cpp new file mode 100644 index 0000000..b154bdf --- /dev/null +++ b/tests/save_laz/save_laz_test.cpp @@ -0,0 +1,60 @@ +#include "save_laz.h" +#include +#include +#include +#include + +namespace +{ +bool containsTestPoint(const std::filesystem::path& path) +{ + laszip_POINTER reader; + if(laszip_create(&reader)) + return false; + + laszip_BOOL compressed; + bool valid = false; + if(!laszip_open_reader(reader, path.c_str(), &compressed)) + { + laszip_header* header; + laszip_point* point; + valid = compressed && !laszip_get_header_pointer(reader, &header) && header->number_of_point_records == 1 && !laszip_read_point(reader) && + !laszip_get_point_pointer(reader, &point) && point->intensity == 37; + if(laszip_close_reader(reader)) + valid = false; + } + return !laszip_destroy(reader) && valid; +} +} // namespace + +int main(int argc, char** argv) +{ + if(argc != 2) + return 2; + const std::string mode = argv[1]; + if(mode != "success" && mode != "open-failure") + return 2; + + char directory[] = "save-laz-XXXXXX"; + if(!mkdtemp(directory)) + return 1; + const std::filesystem::path root(directory); + const auto path = (mode == "success" ? root : root / "missing") / "scan.laz"; + auto points = std::make_shared(); + points->emplace_back().intensity = 37; + const auto stats = mandeye::saveLaz(path.string(), points); + + bool passed; + if(mode == "success") + { + passed = stats && stats->m_pointsCount == 1 && containsTestPoint(path); + } + else + { + passed = !stats && !std::filesystem::exists(path); + } + std::filesystem::remove_all(root); + if(!passed) + std::cerr << "Unexpected saveLaz result for " << mode << '\n'; + return passed ? 0 : 1; +}