Files
cmake/Tests/RunCMake/File_Archive/extract-missing-pattern.cmake
Daksh Mamodiya c31361d089 file(ARCHIVE_EXTRACT): Fix resource leaks on error paths
extract_tar() allocates archive_read, archive_write_disk, and
archive_match handles, but several early-return error paths did not free
them. Most notably, a PATTERNS entry matching nothing leaked all three
handles on every call, which accumulates inside a long-running
configure.

Scope the three handles with std::unique_ptr custom deleters so every
return path releases them automatically.  std::unique_ptr does not
invoke the deleter for a null pointer, and the libarchive *_free
functions are themselves no-ops on null, so no explicit guarding is
required.  archive_read_free()/archive_write_free() implicitly close the
handle if needed, matching the previous behavior.

Fixes: #27872
2026-06-15 11:47:24 -04:00

27 lines
894 B
CMake

# A PATTERNS entry that matches no entry in the archive must fail with a clear
# "Not found in archive" diagnostic. This exercises the most user-reachable
# error path in extract_tar() (the one that previously leaked all three
# libarchive handles), so it guards that the error path still behaves correctly
# after the RAII cleanup refactor.
set(archive "${CMAKE_CURRENT_BINARY_DIR}/test.zip")
set(src "${CMAKE_CURRENT_BINARY_DIR}/src")
set(dest "${CMAKE_CURRENT_BINARY_DIR}/dest")
file(REMOVE "${archive}")
file(REMOVE_RECURSE "${src}" "${dest}")
file(MAKE_DIRECTORY "${src}")
file(WRITE "${src}/f1.txt" "content\n")
file(MAKE_DIRECTORY "${dest}")
file(ARCHIVE_CREATE
OUTPUT "${archive}"
FORMAT zip
PATHS "${src}")
# "no_such_entry" matches nothing in the archive, so extraction must fail.
file(ARCHIVE_EXTRACT
INPUT "${archive}"
DESTINATION "${dest}"
PATTERNS no_such_entry)