Skip to content

Commit 3998bca

Browse files
olszomalmtrojnar
authored andcommitted
Simplify BIO chain free up and FILE_FORMAT_CTX cleanup
1 parent fa40c57 commit 3998bca

File tree

8 files changed

+41
-65
lines changed

8 files changed

+41
-65
lines changed

appx.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ static int appx_remove_pkcs7(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
256256
static int appx_process_data(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
257257
static PKCS7 *appx_pkcs7_signature_new(FILE_FORMAT_CTX *ctx, BIO *hash);
258258
static int appx_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7);
259-
static BIO *appx_bio_free(BIO *hash, BIO *outdata);
260-
static void appx_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
259+
static void appx_bio_free(BIO *hash, BIO *outdata);
260+
static void appx_ctx_cleanup(FILE_FORMAT_CTX *ctx);
261261

262262
FILE_FORMAT file_format_appx = {
263263
.ctx_new = appx_ctx_new,
@@ -757,11 +757,10 @@ static int appx_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
757757
* [out] outdata: outdata file BIO
758758
* [returns] none
759759
*/
760-
static BIO *appx_bio_free(BIO *hash, BIO *outdata)
760+
static void appx_bio_free(BIO *hash, BIO *outdata)
761761
{
762762
BIO_free_all(outdata);
763763
BIO_free_all(hash);
764-
return NULL; /* OK */
765764
}
766765

767766
/*
@@ -771,12 +770,8 @@ static BIO *appx_bio_free(BIO *hash, BIO *outdata)
771770
* [in] outdata: outdata file BIO
772771
* [returns] none
773772
*/
774-
static void appx_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
773+
static void appx_ctx_cleanup(FILE_FORMAT_CTX *ctx)
775774
{
776-
if (outdata) {
777-
BIO_free_all(hash);
778-
BIO_free_all(outdata);
779-
}
780775
freeZip(ctx->appx_ctx->zip);
781776
OPENSSL_free(ctx->appx_ctx->calculatedBMHash);
782777
OPENSSL_free(ctx->appx_ctx->calculatedCTHash);

cab.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ static int cab_process_data(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
5454
static PKCS7 *cab_pkcs7_signature_new(FILE_FORMAT_CTX *ctx, BIO *hash);
5555
static int cab_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7);
5656
static void cab_update_data_size(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7);
57-
static BIO *cab_bio_free(BIO *hash, BIO *outdata);
58-
static void cab_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
57+
static void cab_bio_free(BIO *hash, BIO *outdata);
58+
static void cab_ctx_cleanup(FILE_FORMAT_CTX *ctx);
5959
static int cab_is_detaching_supported(void);
6060

6161
FILE_FORMAT file_format_cab = {
@@ -599,13 +599,11 @@ static void cab_update_data_size(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
599599
* [out] outdata: outdata file BIO (unused)
600600
* [returns] none
601601
*/
602-
static BIO *cab_bio_free(BIO *hash, BIO *outdata)
602+
static void cab_bio_free(BIO *hash, BIO *outdata)
603603
{
604604
/* squash the unused parameter warning */
605605
(void)outdata;
606-
607606
BIO_free_all(hash);
608-
return NULL;
609607
}
610608

611609
/*
@@ -616,11 +614,8 @@ static BIO *cab_bio_free(BIO *hash, BIO *outdata)
616614
* [in] outdata: outdata file BIO
617615
* [returns] none
618616
*/
619-
static void cab_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
617+
static void cab_ctx_cleanup(FILE_FORMAT_CTX *ctx)
620618
{
621-
if (outdata) {
622-
BIO_free_all(hash);
623-
}
624619
unmap_file(ctx->options->indata, ctx->cab_ctx->fileend);
625620
OPENSSL_free(ctx->cab_ctx);
626621
OPENSSL_free(ctx);

cat.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ static int cat_verify_digests(FILE_FORMAT_CTX *ctx, PKCS7 *p7);
4040
static PKCS7 *cat_pkcs7_extract(FILE_FORMAT_CTX *ctx);
4141
static PKCS7 *cat_pkcs7_signature_new(FILE_FORMAT_CTX *ctx, BIO *hash);
4242
static int cat_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7);
43-
static BIO *cat_bio_free(BIO *hash, BIO *outdata);
44-
static void cat_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
43+
static void cat_bio_free(BIO *hash, BIO *outdata);
44+
static void cat_ctx_cleanup(FILE_FORMAT_CTX *ctx);
4545

4646
FILE_FORMAT file_format_cat = {
4747
.ctx_new = cat_ctx_new,
@@ -192,13 +192,11 @@ static int cat_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
192192
* [out] outdata: outdata file BIO (unused)
193193
* [returns] none
194194
*/
195-
static BIO *cat_bio_free(BIO *hash, BIO *outdata)
195+
static void cat_bio_free(BIO *hash, BIO *outdata)
196196
{
197197
/* squash the unused parameter warning */
198198
(void)outdata;
199-
200199
BIO_free_all(hash);
201-
return NULL;
202200
}
203201

204202
/*
@@ -209,11 +207,8 @@ static BIO *cat_bio_free(BIO *hash, BIO *outdata)
209207
* [in] outdata: outdata file BIO
210208
* [returns] none
211209
*/
212-
static void cat_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
210+
static void cat_ctx_cleanup(FILE_FORMAT_CTX *ctx)
213211
{
214-
if (outdata) {
215-
BIO_free_all(hash);
216-
}
217212
unmap_file(ctx->options->indata, ctx->cat_ctx->fileend);
218213
PKCS7_free(ctx->cat_ctx->p7);
219214
OPENSSL_free(ctx->cat_ctx);

msi.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ static int msi_remove_pkcs7(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
201201
static int msi_process_data(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
202202
static PKCS7 *msi_pkcs7_signature_new(FILE_FORMAT_CTX *ctx, BIO *hash);
203203
static int msi_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7);
204-
static BIO *msi_bio_free(BIO *hash, BIO *outdata);
205-
static void msi_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
204+
static void msi_bio_free(BIO *hash, BIO *outdata);
205+
static void msi_ctx_cleanup(FILE_FORMAT_CTX *ctx);
206206
static int msi_is_detaching_supported(void);
207207

208208
FILE_FORMAT file_format_msi = {
@@ -671,11 +671,10 @@ static int msi_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
671671
* [out] outdata: outdata file BIO
672672
* [returns] none
673673
*/
674-
static BIO *msi_bio_free(BIO *hash, BIO *outdata)
674+
static void msi_bio_free(BIO *hash, BIO *outdata)
675675
{
676676
BIO_free_all(hash);
677677
BIO_free_all(outdata);
678-
return NULL;
679678
}
680679

681680
/*
@@ -686,12 +685,8 @@ static BIO *msi_bio_free(BIO *hash, BIO *outdata)
686685
* [out] outdata: outdata file BIO
687686
* [returns] none
688687
*/
689-
static void msi_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
688+
static void msi_ctx_cleanup(FILE_FORMAT_CTX *ctx)
690689
{
691-
if (outdata) {
692-
BIO_free_all(hash);
693-
BIO_free_all(outdata);
694-
}
695690
unmap_file(ctx->options->indata, ctx->msi_ctx->fileend);
696691
msi_file_free(ctx->msi_ctx->msi);
697692
msi_dirent_free(ctx->msi_ctx->dirent);

osslsigncode.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,7 +2743,7 @@ static int verify_signed_file(FILE_FORMAT_CTX *ctx, GLOBAL_OPTIONS *options)
27432743
return 1; /* FAILED */
27442744
}
27452745
p7 = cat_ctx->format->pkcs7_extract(cat_ctx);
2746-
cat_ctx->format->ctx_cleanup(cat_ctx, NULL, NULL);
2746+
cat_ctx->format->ctx_cleanup(cat_ctx);
27472747
OPENSSL_free(cat_options);
27482748
} else {
27492749
if (!ctx->format->pkcs7_extract) {
@@ -2970,11 +2970,11 @@ static int check_attached_data(GLOBAL_OPTIONS *options)
29702970
}
29712971
if (verify_signed_file(ctx, tmp_options)) {
29722972
printf("Signature mismatch\n");
2973-
ctx->format->ctx_cleanup(ctx, NULL, NULL);
2973+
ctx->format->ctx_cleanup(ctx);
29742974
OPENSSL_free(tmp_options);
29752975
return 1; /* Failed */
29762976
}
2977-
ctx->format->ctx_cleanup(ctx, NULL, NULL);
2977+
ctx->format->ctx_cleanup(ctx);
29782978
OPENSSL_free(tmp_options);
29792979
return 0; /* OK */
29802980
}
@@ -4440,6 +4440,7 @@ int main(int argc, char **argv)
44404440
}
44414441
BIO_free_all(hash);
44424442
BIO_free_all(outdata);
4443+
outdata = NULL;
44434444
ret = 1; /* FAILED */
44444445
DO_EXIT_0("Initialization error or unsupported input file type.\n");
44454446
}
@@ -4580,7 +4581,8 @@ int main(int argc, char **argv)
45804581

45814582
skip_signing:
45824583
if (ctx->format->bio_free) {
4583-
outdata = ctx->format->bio_free(hash, outdata);
4584+
ctx->format->bio_free(hash, outdata);
4585+
outdata = NULL;
45844586
}
45854587
if (!ret && options.cmd == CMD_ATTACH) {
45864588
ret = check_attached_data(&options);
@@ -4594,12 +4596,17 @@ int main(int argc, char **argv)
45944596
}
45954597

45964598
err_cleanup:
4597-
if (outdata && options.outfile) {
4598-
/* unlink outfile */
4599-
remove_file(options.outfile);
4599+
if (outdata) {
4600+
if (options.outfile) {
4601+
/* unlink outfile */
4602+
remove_file(options.outfile);
4603+
}
4604+
if (hash)
4605+
BIO_free(hash);
4606+
BIO_free(outdata);
46004607
}
46014608
if (ctx && ctx->format->ctx_cleanup) {
4602-
ctx->format->ctx_cleanup(ctx, hash, outdata);
4609+
ctx->format->ctx_cleanup(ctx);
46034610
}
46044611
#if OPENSSL_VERSION_NUMBER>=0x30000000L
46054612
providers_cleanup();

osslsigncode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ struct file_format_st {
523523
PKCS7 *(*pkcs7_signature_new) (FILE_FORMAT_CTX *ctx, BIO *hash);
524524
int (*append_pkcs7) (FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7);
525525
void (*update_data_size) (FILE_FORMAT_CTX *data, BIO *outdata, PKCS7 *p7);
526-
BIO *(*bio_free) (BIO *hash, BIO *outdata);
527-
void (*ctx_cleanup) (FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
526+
void (*bio_free) (BIO *hash, BIO *outdata);
527+
void (*ctx_cleanup) (FILE_FORMAT_CTX *ctx);
528528
int (*is_detaching_supported) (void);
529529
};
530530

pe.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ static int pe_process_data(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
5656
static PKCS7 *pe_pkcs7_signature_new(FILE_FORMAT_CTX *ctx, BIO *hash);
5757
static int pe_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7);
5858
static void pe_update_data_size(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7);
59-
static BIO *pe_bio_free(BIO *hash, BIO *outdata);
60-
static void pe_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
59+
static void pe_bio_free(BIO *hash, BIO *outdata);
60+
static void pe_ctx_cleanup(FILE_FORMAT_CTX *ctx);
6161
static int pe_is_detaching_supported(void);
6262

6363
FILE_FORMAT file_format_pe = {
@@ -496,13 +496,11 @@ static void pe_update_data_size(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
496496
* [out] outdata: outdata file BIO (unused)
497497
* [returns] none
498498
*/
499-
static BIO *pe_bio_free(BIO *hash, BIO *outdata)
499+
static void pe_bio_free(BIO *hash, BIO *outdata)
500500
{
501501
/* squash the unused parameter warning */
502502
(void)outdata;
503-
504503
BIO_free_all(hash);
505-
return NULL;
506504
}
507505

508506
/*
@@ -513,11 +511,8 @@ static BIO *pe_bio_free(BIO *hash, BIO *outdata)
513511
* [in] outdata: outdata file BIO
514512
* [returns] none
515513
*/
516-
static void pe_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
514+
static void pe_ctx_cleanup(FILE_FORMAT_CTX *ctx)
517515
{
518-
if (outdata) {
519-
BIO_free_all(hash);
520-
}
521516
unmap_file(ctx->options->indata, ctx->pe_ctx->fileend);
522517
OPENSSL_free(ctx->pe_ctx);
523518
OPENSSL_free(ctx);

script.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ static int script_remove_pkcs7(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
6262
static int script_process_data(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
6363
static PKCS7 *script_pkcs7_signature_new(FILE_FORMAT_CTX *ctx, BIO *hash);
6464
static int script_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7);
65-
static BIO *script_bio_free(BIO *hash, BIO *outdata);
66-
static void script_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata);
65+
static void script_bio_free(BIO *hash, BIO *outdata);
66+
static void script_ctx_cleanup(FILE_FORMAT_CTX *ctx);
6767
static int script_is_detaching_supported(void);
6868

6969
FILE_FORMAT file_format_script = {
@@ -575,12 +575,10 @@ static int script_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
575575
* [out] outdata: outdata file BIO
576576
* [returns] none
577577
*/
578-
static BIO *script_bio_free(BIO *hash, BIO *outdata)
578+
static void script_bio_free(BIO *hash, BIO *outdata)
579579
{
580580
BIO_free_all(hash);
581581
BIO_free_all(outdata);
582-
/* FIXME: why doesn't the function return void instead of BIO *? */
583-
return NULL;
584582
}
585583

586584
/*
@@ -590,12 +588,8 @@ static BIO *script_bio_free(BIO *hash, BIO *outdata)
590588
* [out] outdata: outdata file BIO
591589
* [returns] none
592590
*/
593-
static void script_ctx_cleanup(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
591+
static void script_ctx_cleanup(FILE_FORMAT_CTX *ctx)
594592
{
595-
if (outdata) {
596-
BIO_free_all(hash);
597-
BIO_free_all(outdata);
598-
}
599593
unmap_file(ctx->options->indata, ctx->script_ctx->fileend);
600594
OPENSSL_free(ctx->script_ctx);
601595
OPENSSL_free(ctx);

0 commit comments

Comments
 (0)