From 697b0c5185f2b379cf23330fddc5f8b2d691db17 Mon Sep 17 00:00:00 2001 From: Rich Salz Date: Mon, 12 Aug 2019 13:23:17 +1000 Subject: [PATCH 1/1] Fix doc example code to follow coding style Reviewed-by: Paul Dale Reviewed-by: Matt Caswell Reviewed-by: Richard Levitte (Merged from https://github.com/openssl/openssl/pull/9577) --- doc/man3/OSSL_PARAM_construct_from_text.pod | 48 +++++++++++---------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/doc/man3/OSSL_PARAM_construct_from_text.pod b/doc/man3/OSSL_PARAM_construct_from_text.pod index 28d5e6fc13..e8e2639864 100644 --- a/doc/man3/OSSL_PARAM_construct_from_text.pod +++ b/doc/man3/OSSL_PARAM_construct_from_text.pod @@ -89,14 +89,13 @@ Code that looked like this: { int rv; char *stmp, *vtmp = NULL; + stmp = OPENSSL_strdup(value); - if (!stmp) + if (stmp == NULL) return -1; vtmp = strchr(stmp, ':'); - if (vtmp) { - *vtmp = 0; - vtmp++; - } + if (vtmp != NULL) + *vtmp++ = '\0'; rv = EVP_MAC_ctrl_str(ctx, stmp, vtmp); OPENSSL_free(stmp); return rv; @@ -105,9 +104,9 @@ Code that looked like this: ... - char *macopt; for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) { - macopt = sk_OPENSSL_STRING_value(macopts, i); + char *macopt = sk_OPENSSL_STRING_value(macopts, i); + if (pkey_ctrl_string(mac_ctx, macopt) <= 0) { BIO_printf(bio_err, "MAC parameter error \"%s\"\n", macopt); @@ -119,37 +118,40 @@ Code that looked like this: Can be written like this instead: OSSL_PARAM *params = - OPENSSL_zalloc(sizeof(OSSL_PARAM) + OPENSSL_zalloc(sizeof(*params) * (sk_OPENSSL_STRING_num(opts) + 1)); const OSSL_PARAM *paramdefs = EVP_MAC_CTX_set_param_types(mac); size_t params_n; + char *opt = ""; for (params_n = 0; params_n < (size_t)sk_OPENSSL_STRING_num(opts); params_n++) { - char *opt = sk_OPENSSL_STRING_value(opts, (int)params_n); char *stmp, *vtmp = NULL; + opt = sk_OPENSSL_STRING_value(opts, (int)params_n); if ((stmp = OPENSSL_strdup(opt)) == NULL - || (vtmp = strchr(stmp, ':')) == NULL - || (*vtmp++ = '\0') /* Always zero */ - || !OSSL_PARAM_allocate_from_text(¶ms[params_n], - paramdefs, - stmp, vtmp, strlen(vtmp))) { - BIO_printf(bio_err, "MAC parameter error '%s'\n", opt); - ERR_print_errors(bio_err); + || (vtmp = strchr(stmp, ':')) == NULL) + goto err; + + *vtmp++ = '\0'; + if (!OSSL_PARAM_allocate_from_text(¶ms[params_n], + paramdefs, stmp, + vtmp, strlen(vtmp))) goto err; - } } params[params_n] = OSSL_PARAM_construct_end(); - if (!EVP_MAC_CTX_set_params(ctx, params)) { - BIO_printf(bio_err, "MAC parameter error\n"); - ERR_print_errors(bio_err); + if (!EVP_MAC_CTX_set_params(ctx, params)) goto err; - } - for (; params_n-- > 0;) { + while (params_n-- > 0) OPENSSL_free(params[params_n].data); - } OPENSSL_free(params); + /* ... */ + return; + + err: + BIO_printf(bio_err, "MAC parameter error '%s'\n", opt); + ERR_print_errors(bio_err); + =head1 SEE ALSO -- 2.34.1