Commit dcfe84388 for clamav.net
commit dcfe843880066cd2f2ce6a4d11e45d701455ec8f
Author: Val S. <mx.val@icloud.com>
Date: Wed Jan 7 14:07:36 2026 -0500
Fix error handling when a scan fails to push a new embedded layer to the scan context
If pushing a new layer to the recursion stack in the scan context fails,
we need to restore the original recursion level, and undo any changes to
the convenience `ctx->this_level_...` convenience pointers.
This fixes a crash observed when scanning certain files on Windows with
`--leave-temps` enabled and also `--tempdir` set to "C:\temp".
CLAM-2924
diff --git a/libclamav/others.c b/libclamav/others.c
index d14a6aecb..ee3cf32eb 100644
--- a/libclamav/others.c
+++ b/libclamav/others.c
@@ -1766,10 +1766,13 @@ cl_error_t cli_recursion_stack_push(cli_ctx *ctx, cl_fmap_t *map, cli_file_t typ
cli_scan_layer_t *current_layer = NULL;
cli_scan_layer_t *new_layer = NULL;
+ uint32_t old_recursion_level = 0;
char *new_temp_path = NULL;
char *fmap_basename = NULL;
+ old_recursion_level = ctx->recursion_level;
+
// Check the regular limits
if (CL_SUCCESS != (status = cli_checklimits("cli_recursion_stack_push", ctx, map->len, 0, 0))) {
cli_dbgmsg("cli_recursion_stack_push: Some content was skipped. The scan result will not be cached.\n");
@@ -1996,6 +1999,18 @@ cl_error_t cli_recursion_stack_push(cli_ctx *ctx, cl_fmap_t *map, cli_file_t typ
done:
+ if (CL_SUCCESS != status) {
+ // The push failed, so roll back the recursion level change.
+ ctx->recursion_level = old_recursion_level;
+
+ ctx->this_layer_tmpdir = ctx->recursion_stack[ctx->recursion_level].tmpdir;
+ ctx->fmap = ctx->recursion_stack[ctx->recursion_level].fmap;
+
+ if (SCAN_COLLECT_METADATA) {
+ ctx->this_layer_metadata_json = ctx->recursion_stack[ctx->recursion_level].metadata_json;
+ }
+ }
+
if (new_temp_path) {
free(new_temp_path);
}