From: Jakub Jelen <jjelen@redhat.com>
Date: Thu, 23 Nov 2023 16:18:31 +0100
Subject: authentic: Avoid memory leaks

Thanks oss-fuzz

Origin: https://github.com/OpenSC/OpenSC/commit/6d1fcd9cf82c6501089898066656fbe6737f3ced
Bug: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=64439
---
 src/pkcs15init/pkcs15-authentic.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/pkcs15init/pkcs15-authentic.c b/src/pkcs15init/pkcs15-authentic.c
index c6894dd..aa38d4f 100644
--- a/src/pkcs15init/pkcs15-authentic.c
+++ b/src/pkcs15init/pkcs15-authentic.c
@@ -854,17 +854,19 @@ authentic_emu_update_tokeninfo(struct sc_profile *profile, struct sc_pkcs15_card
 	unsigned char buffer[8];
 	int rv,len;
 
-        sc_format_path(AUTHENTIC_CACHE_TIMESTAMP_PATH, &path);
-        rv = sc_select_file(p15card->card, &path, &file);
-        if (!rv)   {
+	sc_format_path(AUTHENTIC_CACHE_TIMESTAMP_PATH, &path);
+	rv = sc_select_file(p15card->card, &path, &file);
+	if (!rv) {
 		rv = sc_get_challenge(p15card->card, buffer, sizeof(buffer));
-		LOG_TEST_RET(ctx, rv, "Get challenge error");
+		if (!rv) {
+			sc_file_free(file);
+			LOG_TEST_RET(ctx, rv, "Get challenge error");
+		}
 
 		len = file->size > sizeof(buffer) ? sizeof(buffer) : file->size;
-	        rv = sc_update_binary(p15card->card, 0, buffer, len, 0);
-		LOG_TEST_RET(ctx, rv, "Update binary error");
-
+		rv = sc_update_binary(p15card->card, 0, buffer, len, 0);
 		sc_file_free(file);
+		LOG_TEST_RET(ctx, rv, "Update binary error");
 	}
 
 	LOG_FUNC_RETURN(ctx, SC_SUCCESS);
