Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/save-laz-leaks.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 6 additions & 3 deletions code/save_laz.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "save_laz.h"
#include <iostream>
#include <laszip/laszip_api.h>
#include <memory>
#include <tracy/Tracy.hpp>

nlohmann::json mandeye::LazStats::produceStatus() const
Expand Down Expand Up @@ -62,6 +63,7 @@ std::optional<mandeye::LazStats> mandeye::saveLaz(const std::string& filename, L
fprintf(stderr, "DLL ERROR: creating laszip writer\n");
return nullopt;
}
std::unique_ptr<void, decltype(&laszip_destroy)> writerHandle(laszip_writer, laszip_destroy);

// get a pointer to the header of the writer so we can populate it

Expand Down Expand Up @@ -125,6 +127,7 @@ std::optional<mandeye::LazStats> 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<void, decltype(&laszip_close_writer)> openWriter(laszip_writer, laszip_close_writer);

fprintf(stderr, "writing file '%s' %scompressed\n", filename.c_str(), (compress ? "" : "un"));

Expand Down Expand Up @@ -179,15 +182,15 @@ std::optional<mandeye::LazStats> 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;
}

// destroy the writer

if(laszip_destroy(laszip_writer))
if(laszip_destroy(writerHandle.release()))
{
fprintf(stderr, "DLL ERROR: destroying laszip writer\n");
return nullopt;
Expand All @@ -208,4 +211,4 @@ std::optional<mandeye::LazStats> mandeye::saveLaz(const std::string& filename, L
TracyPlot("laz_save_duration_sec", (double)stats.m_saveDurationSec1);

return stats;
}
}
16 changes: 16 additions & 0 deletions tests/save_laz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
60 changes: 60 additions & 0 deletions tests/save_laz/save_laz_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "save_laz.h"
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <laszip/laszip_api.h>

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<mandeye::LidarPointsBuffer>();
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;
}
Loading