Skip to content

Commit

Permalink
Fixup demo exit status magic numbers
Browse files Browse the repository at this point in the history
The demo code is quite often block copied for new demos,
so this PR changes demos to use EXIT_SUCCESS & EXIT_FAILURE
instead of using 0 and 1.
Internal functions use the normal notation of 0 = error, 1 = success,
but the value returned by main() must use EXIT_SUCCESS and EXIT_FAILURE.

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>

(cherry picked from commit 09ff84b)

Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from #22728)
  • Loading branch information
slontis authored and t8m committed Nov 16, 2023
1 parent 530672c commit d339f40
Show file tree
Hide file tree
Showing 47 changed files with 240 additions and 251 deletions.
4 changes: 3 additions & 1 deletion demos/bio/client-arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ int main(int argc, char **argv)
char **args = argv + 1;
const char *connect_str = "localhost:4433";
int nargs = argc - 1;
int ret = EXIT_FAILURE;

ctx = SSL_CTX_new(TLS_client_method());
cctx = SSL_CONF_CTX_new();
Expand Down Expand Up @@ -100,9 +101,10 @@ int main(int argc, char **argv)
break;
BIO_write(out, tmpbuf, len);
}
ret = EXIT_SUCCESS;
end:
SSL_CONF_CTX_free(cctx);
BIO_free_all(sbio);
BIO_free(out);
return 0;
return ret;
}
5 changes: 4 additions & 1 deletion demos/bio/client-conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ int main(int argc, char **argv)
CONF_VALUE *cnf;
const char *connect_str = "localhost:4433";
long errline = -1;
int ret = EXIT_FAILURE;

conf = NCONF_new(NULL);

Expand Down Expand Up @@ -108,10 +109,12 @@ int main(int argc, char **argv)
break;
BIO_write(out, tmpbuf, len);
}
ret = EXIT_SUCCESS;

end:
SSL_CONF_CTX_free(cctx);
BIO_free_all(sbio);
BIO_free(out);
NCONF_free(conf);
return 0;
return ret;
}
6 changes: 3 additions & 3 deletions demos/cipher/aesccm.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ int aes_ccm_decrypt(void)
int main(int argc, char **argv)
{
if (!aes_ccm_encrypt())
return 1;
return EXIT_FAILURE;

if (!aes_ccm_decrypt())
return 1;
return EXIT_FAILURE;

return 0;
return EXIT_SUCCESS;
}
6 changes: 3 additions & 3 deletions demos/cipher/aesgcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ int aes_gcm_decrypt(void)
int main(int argc, char **argv)
{
if (!aes_gcm_encrypt())
return 1;
return EXIT_FAILURE;

if (!aes_gcm_decrypt())
return 1;
return EXIT_FAILURE;

return 0;
return EXIT_SUCCESS;
}
7 changes: 3 additions & 4 deletions demos/cipher/aeskeywrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,10 @@ int aes_wrap_decrypt(void)
int main(int argc, char **argv)
{
if (!aes_wrap_encrypt())
return 1;
return EXIT_FAILURE;

if (!aes_wrap_decrypt())
return 1;
return EXIT_FAILURE;

return 0;
return EXIT_SUCCESS;
}

6 changes: 3 additions & 3 deletions demos/cipher/ariacbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ int aria_cbc_decrypt(void)
int main(int argc, char **argv)
{
if (!aria_cbc_encrypt())
return 1;
return EXIT_FAILURE;

if (!aria_cbc_decrypt())
return 1;
return EXIT_FAILURE;

return 0;
return EXIT_SUCCESS;
}
6 changes: 3 additions & 3 deletions demos/cms/cms_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main(int argc, char **argv)
{
BIO *in = NULL, *out = NULL;
CMS_ContentInfo *cms = NULL;
int ret = 1;
int ret = EXIT_FAILURE;

/*
* On OpenSSL 1.0.0+ only:
Expand Down Expand Up @@ -48,11 +48,11 @@ int main(int argc, char **argv)
if (!SMIME_write_CMS(out, cms, in, flags))
goto err;

ret = 0;
ret = EXIT_SUCCESS;

err:

if (ret) {
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Compressing Data\n");
ERR_print_errors_fp(stderr);
}
Expand Down
6 changes: 3 additions & 3 deletions demos/cms/cms_ddec.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main(int argc, char **argv)
X509 *rcert = NULL;
EVP_PKEY *rkey = NULL;
CMS_ContentInfo *cms = NULL;
int ret = 1;
int ret = EXIT_FAILURE;

OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
Expand Down Expand Up @@ -68,11 +68,11 @@ int main(int argc, char **argv)
if (!CMS_decrypt(cms, rkey, rcert, dcont, out, 0))
goto err;

ret = 0;
ret = EXIT_SUCCESS;

err:

if (ret) {
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Decrypting Data\n");
ERR_print_errors_fp(stderr);
}
Expand Down
7 changes: 3 additions & 4 deletions demos/cms/cms_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char **argv)
X509 *rcert = NULL;
EVP_PKEY *rkey = NULL;
CMS_ContentInfo *cms = NULL;
int ret = 1;
int ret = EXIT_FAILURE;

OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
Expand Down Expand Up @@ -59,11 +59,10 @@ int main(int argc, char **argv)
if (!CMS_decrypt(cms, rkey, rcert, NULL, out, 0))
goto err;

ret = 0;
ret = EXIT_SUCCESS;

err:

if (ret) {
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Decrypting Data\n");
ERR_print_errors_fp(stderr);
}
Expand Down
8 changes: 3 additions & 5 deletions demos/cms/cms_denc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main(int argc, char **argv)
X509 *rcert = NULL;
STACK_OF(X509) *recips = NULL;
CMS_ContentInfo *cms = NULL;
int ret = 1;
int ret = EXIT_FAILURE;

int flags = CMS_STREAM | CMS_DETACHED;

Expand Down Expand Up @@ -77,11 +77,9 @@ int main(int argc, char **argv)
if (!PEM_write_bio_CMS(out, cms))
goto err;

ret = 0;

ret = EXIT_SUCCESS;
err:

if (ret) {
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Encrypting Data\n");
ERR_print_errors_fp(stderr);
}
Expand Down
8 changes: 3 additions & 5 deletions demos/cms/cms_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char **argv)
X509 *rcert = NULL;
STACK_OF(X509) *recips = NULL;
CMS_ContentInfo *cms = NULL;
int ret = 1;
int ret = EXIT_FAILURE;

/*
* On OpenSSL 1.0.0 and later only:
Expand Down Expand Up @@ -73,11 +73,9 @@ int main(int argc, char **argv)
if (!SMIME_write_CMS(out, cms, in, flags))
goto err;

ret = 0;

ret = EXIT_SUCCESS;
err:

if (ret) {
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Encrypting Data\n");
ERR_print_errors_fp(stderr);
}
Expand Down
8 changes: 3 additions & 5 deletions demos/cms/cms_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char **argv)
X509 *scert = NULL;
EVP_PKEY *skey = NULL;
CMS_ContentInfo *cms = NULL;
int ret = 1;
int ret = EXIT_FAILURE;

/*
* For simple S/MIME signing use CMS_DETACHED. On OpenSSL 1.0.0 only: for
Expand Down Expand Up @@ -69,11 +69,9 @@ int main(int argc, char **argv)
if (!SMIME_write_CMS(out, cms, in, flags))
goto err;

ret = 0;

ret = EXIT_SUCCESS;
err:

if (ret) {
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Signing Data\n");
ERR_print_errors_fp(stderr);
}
Expand Down
8 changes: 3 additions & 5 deletions demos/cms/cms_sign2.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char **argv)
X509 *scert = NULL, *scert2 = NULL;
EVP_PKEY *skey = NULL, *skey2 = NULL;
CMS_ContentInfo *cms = NULL;
int ret = 1;
int ret = EXIT_FAILURE;

OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
Expand Down Expand Up @@ -77,11 +77,9 @@ int main(int argc, char **argv)
if (!SMIME_write_CMS(out, cms, in, CMS_STREAM))
goto err;

ret = 0;

ret = EXIT_SUCCESS;
err:

if (ret) {
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Signing Data\n");
ERR_print_errors_fp(stderr);
}
Expand Down
8 changes: 3 additions & 5 deletions demos/cms/cms_uncomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main(int argc, char **argv)
{
BIO *in = NULL, *out = NULL;
CMS_ContentInfo *cms = NULL;
int ret = 1;
int ret = EXIT_FAILURE;

OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
Expand All @@ -42,11 +42,9 @@ int main(int argc, char **argv)
if (!CMS_uncompress(cms, out, NULL, 0))
goto err;

ret = 0;

ret = EXIT_SUCCESS;
err:

if (ret) {
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Uncompressing Data\n");
ERR_print_errors_fp(stderr);
}
Expand Down
9 changes: 3 additions & 6 deletions demos/cms/cms_ver.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ int main(int argc, char **argv)
X509_STORE *st = NULL;
X509 *cacert = NULL;
CMS_ContentInfo *cms = NULL;

int ret = 1;
int ret = EXIT_FAILURE;

OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
Expand Down Expand Up @@ -69,11 +68,9 @@ int main(int argc, char **argv)

fprintf(stderr, "Verification Successful\n");

ret = 0;

ret = EXIT_SUCCESS;
err:

if (ret) {
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Verifying Data\n");
ERR_print_errors_fp(stderr);
}
Expand Down
19 changes: 9 additions & 10 deletions demos/digest/BIO_f_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@

int main(int argc, char * argv[])
{
int result = 1;
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *library_context = NULL;
BIO *input = NULL;
BIO *bio_digest = NULL;
BIO *bio_digest = NULL, *reading = NULL;
EVP_MD *md = NULL;
unsigned char buffer[512];
size_t readct, writect;
size_t digest_size;
char *digest_value=NULL;
char *digest_value = NULL;
int j;

input = BIO_new_fd( fileno(stdin), 1 );
Expand Down Expand Up @@ -89,9 +88,9 @@ int main(int argc, char * argv[])
* We will use BIO chaining so that as we read, the digest gets updated
* See the man page for BIO_push
*/
BIO *reading = BIO_push( bio_digest, input );
while( BIO_read(reading, buffer, sizeof(buffer)) > 0 )
reading = BIO_push(bio_digest, input);

while (BIO_read(reading, buffer, sizeof(buffer)) > 0)
;

/*-
Expand All @@ -106,10 +105,10 @@ int main(int argc, char * argv[])
fprintf(stdout, "%02x", (unsigned char)digest_value[j]);
}
fprintf(stdout, "\n");
result = 0;
ret = EXIT_SUCCESS;

cleanup:
if (result != 0)
if (ret != EXIT_SUCCESS)
ERR_print_errors_fp(stderr);

OPENSSL_free(digest_value);
Expand All @@ -118,5 +117,5 @@ int main(int argc, char * argv[])
EVP_MD_free(md);
OSSL_LIB_CTX_free(library_context);

return result;
return ret;
}
11 changes: 5 additions & 6 deletions demos/digest/EVP_MD_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const unsigned char known_answer[] = {
int demonstrate_digest(void)
{
OSSL_LIB_CTX *library_context;
int result = 0;
int ret = 0;
const char *option_properties = NULL;
EVP_MD *message_digest = NULL;
EVP_MD_CTX *digest_context = NULL;
Expand Down Expand Up @@ -161,23 +161,22 @@ int demonstrate_digest(void)
fprintf(stdout, "\nDigest does not match known answer\n");
} else {
fprintf(stdout, "Digest computed properly.\n");
result = 1;
ret = 1;
}


cleanup:
if (result != 1)
if (ret != 1)
ERR_print_errors_fp(stderr);
/* OpenSSL free functions will ignore NULL arguments */
EVP_MD_CTX_free(digest_context);
OPENSSL_free(digest_value);
EVP_MD_free(message_digest);

OSSL_LIB_CTX_free(library_context);
return result;
return ret;
}

int main(void)
{
return demonstrate_digest() == 0;
return demonstrate_digest() ? EXIT_SUCCESS : EXIT_FAILURE;
}

0 comments on commit d339f40

Please sign in to comment.