diff options
| author | tb <> | 2024-03-26 00:24:11 +0000 |
|---|---|---|
| committer | tb <> | 2024-03-26 00:24:11 +0000 |
| commit | e45e73d8f6ec2c8e3433199494319c6045bc2ae5 (patch) | |
| tree | 9206263e9d30ccb31454e0d0f5967eaedef6d7a9 /src | |
| parent | 284a92902e7e2cadbab54fcc5e2de56838bfb3c6 (diff) | |
| download | openbsd-e45e73d8f6ec2c8e3433199494319c6045bc2ae5.tar.gz openbsd-e45e73d8f6ec2c8e3433199494319c6045bc2ae5.tar.bz2 openbsd-e45e73d8f6ec2c8e3433199494319c6045bc2ae5.zip | |
Clean up conf's module_init()
Immediately error out when no name or value is passed instead of hiding
this in a a combination of ternary operator and strdup error check.
Use calloc(). Unindent some stupid, don't pretend this function can return
anything but -1 and 1, turn the whole thing into single exit and call the
now existing imodule_free() instead of handrolling it.
ok jsing
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/conf/conf_mod.c | 66 |
1 files changed, 27 insertions, 39 deletions
diff --git a/src/lib/libcrypto/conf/conf_mod.c b/src/lib/libcrypto/conf/conf_mod.c index 280d8edd20..85d74e6fb6 100644 --- a/src/lib/libcrypto/conf/conf_mod.c +++ b/src/lib/libcrypto/conf/conf_mod.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: conf_mod.c,v 1.36 2024/03/20 22:11:07 tb Exp $ */ | 1 | /* $OpenBSD: conf_mod.c,v 1.37 2024/03/26 00:24:11 tb Exp $ */ |
| 2 | /* Written by Stephen Henson (steve@openssl.org) for the OpenSSL | 2 | /* Written by Stephen Henson (steve@openssl.org) for the OpenSSL |
| 3 | * project 2001. | 3 | * project 2001. |
| 4 | */ | 4 | */ |
| @@ -279,62 +279,50 @@ module_find(char *name) | |||
| 279 | static int | 279 | static int |
| 280 | module_init(CONF_MODULE *mod, char *name, char *value, const CONF *cnf) | 280 | module_init(CONF_MODULE *mod, char *name, char *value, const CONF *cnf) |
| 281 | { | 281 | { |
| 282 | int ret = 1; | ||
| 283 | int init_called = 0; | ||
| 284 | CONF_IMODULE *imod = NULL; | 282 | CONF_IMODULE *imod = NULL; |
| 283 | int need_finish = 0; | ||
| 284 | int ret = -1; | ||
| 285 | 285 | ||
| 286 | /* Otherwise add initialized module to list */ | 286 | if (name == NULL || value == NULL) |
| 287 | imod = malloc(sizeof(CONF_IMODULE)); | 287 | goto err; |
| 288 | if (!imod) | 288 | |
| 289 | if ((imod = calloc(1, sizeof(*imod))) == NULL) | ||
| 289 | goto err; | 290 | goto err; |
| 290 | 291 | ||
| 291 | imod->mod = mod; | 292 | imod->mod = mod; |
| 292 | imod->name = name ? strdup(name) : NULL; | 293 | |
| 293 | imod->value = value ? strdup(value) : NULL; | 294 | if ((imod->name = strdup(name)) == NULL) |
| 294 | imod->usr_data = NULL; | 295 | goto err; |
| 295 | 296 | if ((imod->value = strdup(value)) == NULL) | |
| 296 | if (!imod->name || !imod->value) | 297 | goto err; |
| 297 | goto memerr; | 298 | |
| 298 | 299 | if (mod->init != NULL) { | |
| 299 | /* Try to initialize module */ | 300 | need_finish = 1; |
| 300 | if (mod->init) { | 301 | if (mod->init(imod, cnf) <= 0) |
| 301 | ret = mod->init(imod, cnf); | ||
| 302 | init_called = 1; | ||
| 303 | /* Error occurred, exit */ | ||
| 304 | if (ret <= 0) | ||
| 305 | goto err; | 302 | goto err; |
| 306 | } | 303 | } |
| 307 | 304 | ||
| 308 | if (initialized_modules == NULL) { | 305 | if (initialized_modules == NULL) |
| 309 | initialized_modules = sk_CONF_IMODULE_new_null(); | 306 | initialized_modules = sk_CONF_IMODULE_new_null(); |
| 310 | if (!initialized_modules) { | 307 | if (initialized_modules == NULL) |
| 311 | CONFerror(ERR_R_MALLOC_FAILURE); | 308 | goto err; |
| 312 | goto err; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | 309 | ||
| 316 | if (!sk_CONF_IMODULE_push(initialized_modules, imod)) { | 310 | if (!sk_CONF_IMODULE_push(initialized_modules, imod)) |
| 317 | CONFerror(ERR_R_MALLOC_FAILURE); | ||
| 318 | goto err; | 311 | goto err; |
| 319 | } | 312 | imod = NULL; |
| 313 | need_finish = 0; | ||
| 320 | 314 | ||
| 321 | mod->links++; | 315 | mod->links++; |
| 322 | 316 | ||
| 323 | return ret; | 317 | ret = 1; |
| 324 | 318 | ||
| 325 | err: | 319 | err: |
| 326 | /* We've started the module so we'd better finish it */ | 320 | if (need_finish && mod->finish != NULL) |
| 327 | if (mod->finish && init_called) | ||
| 328 | mod->finish(imod); | 321 | mod->finish(imod); |
| 329 | 322 | ||
| 330 | memerr: | 323 | imodule_free(imod); |
| 331 | if (imod) { | ||
| 332 | free(imod->name); | ||
| 333 | free(imod->value); | ||
| 334 | free(imod); | ||
| 335 | } | ||
| 336 | 324 | ||
| 337 | return -1; | 325 | return ret; |
| 338 | } | 326 | } |
| 339 | 327 | ||
| 340 | /* Unload any dynamic modules that have a link count of zero: | 328 | /* Unload any dynamic modules that have a link count of zero: |
