From 67bbc1205a645932be2dd00b4be806dd16785de9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Fri, 5 Apr 2024 10:39:07 +0300
Subject: [PATCH] extract: explicitly return ECANCELLED

and additional sanity checks on the extraction state
---
 src/app_manifest.c | 2 +-
 src/app_mkndx.c    | 2 +-
 src/extract_v2.c   | 6 +++---
 src/package.c      | 4 ++--
 4 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/app_manifest.c b/src/app_manifest.c
index 5caf7ba4..64c01271 100644
--- a/src/app_manifest.c
+++ b/src/app_manifest.c
@@ -148,7 +148,7 @@ static void process_file(struct apk_database *db, const char *match)
 	}
 
 	r = apk_extract(&ctx.ectx, apk_istream_from_file(AT_FDCWD, match));
-	if (r < 0) apk_err(out, "%s: %s", match, apk_error_str(r));
+	if (r < 0 && r != -ECANCELED) apk_err(out, "%s: %s", match, apk_error_str(r));
 }
 
 static int process_match(struct apk_database *db, const char *match, struct apk_name *name, void *ctx)
diff --git a/src/app_mkndx.c b/src/app_mkndx.c
index d7f1d26d..b1e809b6 100644
--- a/src/app_mkndx.c
+++ b/src/app_mkndx.c
@@ -272,7 +272,7 @@ static int mkndx_main(void *pctx, struct apk_ctx *ac, struct apk_string_array *a
 			apk_extract_generate_identity(&ctx->ectx, &csum);
 			csum.type = APK_CHECKSUM_NONE;
 			r = apk_extract(&ctx->ectx, apk_istream_from_file(AT_FDCWD, *parg));
-			if (r < 0) goto err_pkg;
+			if (r < 0 && r != -ECANCELED) goto err_pkg;
 
 			adb_wo_int(&ctx->pkginfo, ADBI_PI_FILE_SIZE, ctx->file_size);
 			if (csum.type != APK_CHECKSUM_NONE)
diff --git a/src/extract_v2.c b/src/extract_v2.c
index 922b785f..b0060b57 100644
--- a/src/extract_v2.c
+++ b/src/extract_v2.c
@@ -346,10 +346,10 @@ int apk_extract_v2(struct apk_extract_ctx *ectx, struct apk_istream *is)
 	r = apk_tar_parse(
 		apk_istream_gunzip_mpart(is, apk_sign_ctx_mpart_cb, &sctx),
 		apk_extract_v2_entry, ectx, apk_ctx_get_id_cache(ac));
-	if (r == -ECANCELED) r = 0;
-	if ((r == 0 || r == -APKE_EOF) && !ectx->is_package && !ectx->is_index)
+	if ((r == 0 || r == -ECANCELED || r == -APKE_EOF) && !ectx->is_package && !ectx->is_index)
 		r = -APKE_FORMAT_INVALID;
-	if (r == 0) r = sctx.verify_error;
+	if (r == 0 && (!sctx.data_verified || !sctx.end_seen)) r = -APKE_V2PKG_INTEGRITY;
+	if ((r == 0 || r == -ECANCELED) && sctx.verify_error) r = sctx.verify_error;
 	if (r == -APKE_SIGNATURE_UNTRUSTED && sctx.allow_untrusted) r = 0;
 	if (ectx->generate_identity) apk_checksum_from_digest(ectx->identity, &sctx.identity);
 	apk_sign_ctx_free(&sctx);
diff --git a/src/package.c b/src/package.c
index 9bf7a5da..68d2e60b 100644
--- a/src/package.c
+++ b/src/package.c
@@ -663,11 +663,11 @@ int apk_pkg_read(struct apk_database *db, const char *file, struct apk_package *
 	apk_extract_generate_identity(&ctx.ectx, &ctx.pkg->csum);
 
 	r = apk_extract(&ctx.ectx, apk_istream_from_file(AT_FDCWD, file));
-	if (r < 0) goto err;
+	if (r < 0 && r != -ECANCELED) goto err;
 	if (ctx.pkg->csum.type == APK_CHECKSUM_NONE ||
 	    ctx.pkg->name == NULL ||
 	    ctx.pkg->uninstallable) {
-		r = -APKE_FORMAT_NOT_SUPPORTED;
+		r = -APKE_V2PKG_FORMAT;
 		goto err;
 	}
 	*apk_string_array_add(&db->filename_array) = strdup(file);
-- 
GitLab