diff --git a/project.cpp b/project.cpp index 3b33cee..2aceae7 100644 --- a/project.cpp +++ b/project.cpp @@ -334,6 +334,8 @@ extern "C" Configuration CrafterBuildProject(std::span a .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("DependencyNotes").Dependencies({ CrafterBuildLib.get() }) .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); + cfg.AddTest("SiblingOwnPointers").Dependencies({ CrafterBuildLib.get() }) + .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("ConcurrentDependencyReset").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("JobGroup").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("JobLimit").Dependencies({ CrafterBuildLib.get() }); diff --git a/tests/SiblingOwnPointers/fixture/project.cpp b/tests/SiblingOwnPointers/fixture/project.cpp new file mode 100644 index 0000000..b766a0c --- /dev/null +++ b/tests/SiblingOwnPointers/fixture/project.cpp @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +using namespace Crafter; + +// A library whose test depends on it: the test's dependency is a pointer to +// the Configuration this function returns. A library with no sources is +// enough, since the test that loads this file reads the pointer and builds +// nothing. +extern "C" Configuration CrafterBuildProject(std::span) { + Configuration cfg; + cfg.path = "./"; + cfg.name = "own"; + cfg.outputName = "own"; + cfg.type = ConfigurationType::LibraryStatic; + cfg.AddTest("OwnTest").Dependencies({ &cfg }); + return cfg; +} diff --git a/tests/SiblingOwnPointers/fixture/tests/OwnTest/main.cpp b/tests/SiblingOwnPointers/fixture/tests/OwnTest/main.cpp new file mode 100644 index 0000000..97eb9af --- /dev/null +++ b/tests/SiblingOwnPointers/fixture/tests/OwnTest/main.cpp @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +int main() { + return 0; +} diff --git a/tests/SiblingOwnPointers/main.cpp b/tests/SiblingOwnPointers/main.cpp new file mode 100644 index 0000000..60348cb --- /dev/null +++ b/tests/SiblingOwnPointers/main.cpp @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +namespace fs = std::filesystem; +using namespace Crafter; + +// A project file may keep pointers to the Configuration it returns: a test +// that links the library the file describes is declared with +// cfg.AddTest("T").Dependencies({ &cfg }). Loaded by another project through +// LocalProject or GitProject, that Configuration has to stay where the +// project file built it. +// +// The loader moved it: out of the call's return value, and then into its +// cache. The test's dependency was left pointing at an object that no +// longer existed, and whatever followed it read freed memory. +// `crafter-build lint` follows it for a sibling inside the project's own +// directory, when it works out the compile command of each of the sibling's +// tests for a rule that needs an AST. +int main() { + const fs::path installedHome = GetCrafterBuildHome(); + if (!fs::exists(installedHome / "Crafter.Build.cppm")) { + std::println(std::cerr, "SKIP: no module sources at {}", installedHome.string()); + return 77; + } + + // Staged because the load drops a build/ tree next to the project file. + const fs::path scratch = fs::temp_directory_path() / "crafter-build-sibling-own-pointers"; + std::error_code ec; + fs::remove_all(scratch, ec); + fs::copy(fs::current_path() / "tests" / "SiblingOwnPointers" / "fixture", scratch, fs::copy_options::recursive); + + Configuration* sibling = nullptr; + try { + sibling = LocalProject({ .projectFile = scratch / "project.cpp" }); + } catch (const std::exception& e) { + std::println(std::cerr, "FAIL: loading the sibling threw: {}", e.what()); + return 1; + } + // Compared, never followed: where the two differ, the pointer leads + // nowhere. + if (sibling->tests.size() != 1 || sibling->tests[0].config.dependencies != std::vector{sibling}) { + std::println(std::cerr, "FAIL: the pointer a sibling's project file took to the Configuration it returns is not the Configuration its consumers get"); + return 1; + } + fs::remove_all(scratch, ec); + return 0; +}