From: Alberto Leiva Popper Date: Fri, 23 Jun 2023 15:49:23 +0000 (-0600) Subject: Panic on ENOMEMs X-Git-Tag: 1.6.0~80^2~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=330240b2b5de670858a492703f016da21cc374bd;p=thirdparty%2FFORT-validator.git Panic on ENOMEMs Trying to recover is incorrect because we don't want to advertise an incomplete or outdated VRP table to the routers. We don't want to rely on the OOM-killer; we NEED to die on memory allocation failures ASAP. Though this doesn't have much to do with the RRDP refactor, I'm doing it early to get plenty of time for testing and review. Partially F1xes #40. (Still need to update some dependency usages.) --- diff --git a/src/asn1/oid.c b/src/asn1/oid.c index 6f4043d8..03cb8594 100644 --- a/src/asn1/oid.c +++ b/src/asn1/oid.c @@ -29,7 +29,7 @@ oid2arcs(OBJECT_IDENTIFIER_t *oid, struct oid_arcs *result) result->arcs = malloc(MAX_ARCS * sizeof(asn_oid_arc_t)); if (result->arcs == NULL) - return pr_enomem(); + enomem_panic(); count = OBJECT_IDENTIFIER_get_arcs(oid, result->arcs, MAX_ARCS); if (count < 0) { @@ -43,10 +43,8 @@ oid2arcs(OBJECT_IDENTIFIER_t *oid, struct oid_arcs *result) /* If necessary, reallocate arcs array and try again. */ if (count > MAX_ARCS) { tmp = realloc(result->arcs, count * sizeof(asn_oid_arc_t)); - if (tmp == NULL) { - free(result->arcs); - return pr_enomem(); - } + if (tmp == NULL) + enomem_panic(); result->arcs = tmp; count2 = OBJECT_IDENTIFIER_get_arcs(oid, result->arcs, count); diff --git a/src/asn1/signed_data.c b/src/asn1/signed_data.c index 41e4f816..1f7dade3 100644 --- a/src/asn1/signed_data.c +++ b/src/asn1/signed_data.c @@ -20,7 +20,7 @@ static const OID oid_mda = OID_MESSAGE_DIGEST_ATTR; static const OID oid_sta = OID_SIGNING_TIME_ATTR; static const OID oid_bsta = OID_BINARY_SIGNING_TIME_ATTR; -int +void signed_object_args_init(struct signed_object_args *args, struct rpki_uri *uri, STACK_OF(X509_CRL) *crls, @@ -28,12 +28,11 @@ signed_object_args_init(struct signed_object_args *args, { args->res = resources_create(force_inherit); if (args->res == NULL) - return pr_enomem(); + enomem_panic(); args->uri = uri; args->crls = crls; memset(&args->refs, 0, sizeof(args->refs)); - return 0; } void @@ -416,10 +415,8 @@ signed_data_decode_pkcs7(ANY_t *coded, struct SignedData **result) return error; sdata = calloc(1, sizeof(struct SignedData)); - if (sdata == NULL) { - error = pr_enomem(); - goto release_sdata_pkcs7; - } + if (sdata == NULL) + enomem_panic(); /* Parse content as OCTET STRING */ error = asn1_decode_any(sdata_pkcs7->encapContentInfo.eContent, @@ -446,7 +443,6 @@ signed_data_decode_pkcs7(ANY_t *coded, struct SignedData **result) release_sdata: free(sdata); -release_sdata_pkcs7: ASN_STRUCT_FREE(asn_DEF_SignedDataPKCS7, sdata_pkcs7); return error; } diff --git a/src/asn1/signed_data.h b/src/asn1/signed_data.h index d627def8..b27b404c 100644 --- a/src/asn1/signed_data.h +++ b/src/asn1/signed_data.h @@ -27,7 +27,7 @@ struct signed_object_args { struct certificate_refs refs; }; -int signed_object_args_init(struct signed_object_args *, struct rpki_uri *, +void signed_object_args_init(struct signed_object_args *, struct rpki_uri *, STACK_OF(X509_CRL) *, bool); void signed_object_args_cleanup(struct signed_object_args *); diff --git a/src/cert_stack.c b/src/cert_stack.c index ed5e90ee..0d9246e1 100644 --- a/src/cert_stack.c +++ b/src/cert_stack.c @@ -105,7 +105,7 @@ certstack_create(struct cert_stack **result) stack = malloc(sizeof(struct cert_stack)); if (stack == NULL) - return pr_enomem(); + enomem_panic(); stack->x509s = sk_X509_new_null(); if (stack->x509s == NULL) { @@ -182,21 +182,20 @@ certstack_destroy(struct cert_stack *stack) free(stack); } -int +void deferstack_push(struct cert_stack *stack, struct deferred_cert *deferred) { struct defer_node *node; node = malloc(sizeof(struct defer_node)); if (node == NULL) - return pr_enomem(); + enomem_panic(); node->type = DNT_CERT; node->deferred = *deferred; uri_refget(deferred->uri); rpp_refget(deferred->pp); SLIST_INSERT_HEAD(&stack->defers, node, next); - return 0; } static void @@ -261,7 +260,7 @@ init_resources(X509 *x509, enum rpki_policy policy, enum cert_type type, result = resources_create(false); if (result == NULL) - return pr_enomem(); + enomem_panic(); resources_set_policy(result, policy); error = certificate_get_resources(x509, result, type); @@ -317,7 +316,7 @@ create_separator(void) result = malloc(sizeof(struct defer_node)); if (result == NULL) - return NULL; + enomem_panic(); result->type = DNT_SEPARATOR; return result; @@ -335,7 +334,7 @@ x509stack_push(struct cert_stack *stack, struct rpki_uri *uri, X509 *x509, meta = malloc(sizeof(struct metadata_node)); if (meta == NULL) - return pr_enomem(); + enomem_panic(); meta->uri = uri; uri_refget(uri); @@ -350,10 +349,6 @@ x509stack_push(struct cert_stack *stack, struct rpki_uri *uri, X509 *x509, goto destroy_resources; defer_separator = create_separator(); - if (defer_separator == NULL) { - error = pr_enomem(); - goto destroy_resources; - } ok = sk_X509_push(stack->x509s, x509); if (ok <= 0) { @@ -427,8 +422,8 @@ x509stack_peek_level(struct cert_stack *stack) return (meta != NULL) ? meta->level : 0; } -static int -get_current_file_name(char **_result) +static char * +get_current_file_name(void) { char const *file_name; char *result; @@ -439,10 +434,9 @@ get_current_file_name(char **_result) result = strdup(file_name); if (result == NULL) - return pr_enomem(); + enomem_panic(); - *_result = result; - return 0; + return result; } /** @@ -452,7 +446,7 @@ get_current_file_name(char **_result) * * This function will steal ownership of @number on success. */ -int +void x509stack_store_serial(struct cert_stack *stack, BIGNUM *number) { struct metadata_node *meta; @@ -460,14 +454,13 @@ x509stack_store_serial(struct cert_stack *stack, BIGNUM *number) array_index i; struct serial_number duplicate; char *string; - int error; /* Remember to free @number if you return 0 but don't store it. */ meta = SLIST_FIRST(&stack->metas); if (meta == NULL) { BN_free(number); - return 0; /* The TA lacks siblings, so serial is unique. */ + return; /* The TA lacks siblings, so serial is unique. */ } /* @@ -498,20 +491,14 @@ x509stack_store_serial(struct cert_stack *stack, BIGNUM *number) string, cursor->file); BN_free(number); free(string); - return 0; + return; } } duplicate.number = number; - error = get_current_file_name(&duplicate.file); - if (error) - return error; - - error = serial_numbers_add(&meta->serials, &duplicate); - if (error) - free(duplicate.file); + duplicate.file = get_current_file_name(); - return error; + serial_numbers_add(&meta->serials, &duplicate); } STACK_OF(X509) * diff --git a/src/cert_stack.h b/src/cert_stack.h index 61c209f4..f69b05ae 100644 --- a/src/cert_stack.h +++ b/src/cert_stack.h @@ -38,7 +38,7 @@ struct deferred_cert { int certstack_create(struct cert_stack **); void certstack_destroy(struct cert_stack *); -int deferstack_push(struct cert_stack *, struct deferred_cert *cert); +void deferstack_push(struct cert_stack *, struct deferred_cert *cert); int deferstack_pop(struct cert_stack *, struct deferred_cert *cert); bool deferstack_is_empty(struct cert_stack *); @@ -49,7 +49,7 @@ X509 *x509stack_peek(struct cert_stack *); struct rpki_uri *x509stack_peek_uri(struct cert_stack *); struct resources *x509stack_peek_resources(struct cert_stack *); unsigned int x509stack_peek_level(struct cert_stack *); -int x509stack_store_serial(struct cert_stack *, BIGNUM *); +void x509stack_store_serial(struct cert_stack *, BIGNUM *); typedef int (*subject_pk_check_cb)(bool *, char const *, void *); int x509stack_store_subject(struct cert_stack *, struct rfc5280_name *, subject_pk_check_cb, void *); diff --git a/src/common.c b/src/common.c index 7ea07249..3f2acefd 100644 --- a/src/common.c +++ b/src/common.c @@ -106,11 +106,11 @@ process_file(char const *dir_name, char const *file_name, char const *file_ext, /* Get the full file path */ tmp = strdup(dir_name); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); tmp = realloc(tmp, strlen(tmp) + 1 + strlen(file_name) + 1); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); strcat(tmp, "/"); strcat(tmp, file_name); @@ -299,7 +299,7 @@ create_dir_recursive(char const *path) localuri = strdup(path); if (localuri == NULL) - return pr_enomem(); + enomem_panic(); for (i = 1; localuri[i] != '\0'; i++) { if (localuri[i] == '/') { @@ -363,7 +363,7 @@ delete_dir_recursive_bottom_up(char const *path) config_repo = strdup(config_get_local_repository()); if (config_repo == NULL) - return pr_enomem(); + enomem_panic(); /* Stop dir removal when the work_dir has this length */ config_len = strlen(config_repo); @@ -373,7 +373,7 @@ delete_dir_recursive_bottom_up(char const *path) work_loc = strdup(path); if (work_loc == NULL) - return pr_enomem(); + enomem_panic(); do { tmp = strrchr(work_loc, '/'); @@ -434,9 +434,8 @@ get_current_time(time_t *result) * * Returns 0 on success, otherwise an error code. */ -int -map_uri_to_local(char const *uri, char const *uri_prefix, char const *workspace, - char **result) +char * +map_uri_to_local(char const *uri, char const *uri_prefix, char const *workspace) { char const *repository; char *local; @@ -460,10 +459,9 @@ map_uri_to_local(char const *uri, char const *uri_prefix, char const *workspace, if (workspace != NULL) workspace_len = strlen(workspace); - local = malloc(repository_len + extra_slash + workspace_len + uri_len + - 1); + local = malloc(repository_len + extra_slash + workspace_len + uri_len + 1); if (local == NULL) - return pr_enomem(); + enomem_panic(); offset = 0; strcpy(local + offset, repository); @@ -480,6 +478,5 @@ map_uri_to_local(char const *uri, char const *uri_prefix, char const *workspace, offset += uri_len; local[offset] = '\0'; - *result = local; - return 0; + return local; } diff --git a/src/common.h b/src/common.h index 1a0771ac..b8a2068e 100644 --- a/src/common.h +++ b/src/common.h @@ -67,6 +67,6 @@ int delete_dir_recursive_bottom_up(char const *); int get_current_time(time_t *); -int map_uri_to_local(char const *, char const*, char const *, char **); +char *map_uri_to_local(char const *, char const*, char const *); #endif /* SRC_RTR_COMMON_H_ */ diff --git a/src/config.c b/src/config.c index 36ff9723..0766d6de 100644 --- a/src/config.c +++ b/src/config.c @@ -891,7 +891,7 @@ is_alphanumeric(int chara) * "struct option" is the array that getopt expects. * "struct args_flag" is our option metadata. */ -static int +static void construct_getopt_options(struct option **_long_opts, char **_short_opts) { struct option_field const *opt; @@ -914,12 +914,10 @@ construct_getopt_options(struct option **_long_opts, char **_short_opts) /* +1 NULL end, means end of array. */ long_opts = calloc(total_long_options + 1, sizeof(struct option)); if (long_opts == NULL) - return pr_enomem(); + enomem_panic(); short_opts = malloc(total_short_options + 1); - if (short_opts == NULL) { - free(long_opts); - return pr_enomem(); - } + if (short_opts == NULL) + enomem_panic(); *_long_opts = long_opts; *_short_opts = short_opts; @@ -942,7 +940,6 @@ construct_getopt_options(struct option **_long_opts, char **_short_opts) } *short_opts = '\0'; - return 0; } static void @@ -960,7 +957,7 @@ print_config(void) pr_op_info("}"); } -static int +static void set_default_values(void) { static char const *recursive_rsync_args[] = { @@ -984,22 +981,16 @@ set_default_values(void) "$LOCAL", }; - int error; - /* * Values that might need to be freed WILL be freed, so use heap * duplicates. */ - error = string_array_init(&rpki_config.server.address, NULL, 0); - if (error) - return error; + string_array_init(&rpki_config.server.address, NULL, 0); rpki_config.server.port = strdup("323"); - if (rpki_config.server.port == NULL) { - error = pr_enomem(); - goto revert_address; - } + if (rpki_config.server.port == NULL) + enomem_panic(); rpki_config.server.backlog = SOMAXCONN; rpki_config.server.interval.validation = 3600; @@ -1012,10 +1003,8 @@ set_default_values(void) rpki_config.slurm = NULL; rpki_config.local_repository = strdup("/tmp/fort/repository"); - if (rpki_config.local_repository == NULL) { - error = pr_enomem(); - goto revert_port; - } + if (rpki_config.local_repository == NULL) + enomem_panic(); rpki_config.sync_strategy = RSYNC_ROOT_EXCEPT_TA; rpki_config.shuffle_tal_uris = false; @@ -1030,20 +1019,13 @@ set_default_values(void) rpki_config.rsync.retry.count = 1; rpki_config.rsync.retry.interval = 4; rpki_config.rsync.program = strdup("rsync"); - if (rpki_config.rsync.program == NULL) { - error = pr_enomem(); - goto revert_repository; - } + if (rpki_config.rsync.program == NULL) + enomem_panic(); - error = string_array_init(&rpki_config.rsync.args.recursive, + string_array_init(&rpki_config.rsync.args.recursive, recursive_rsync_args, ARRAY_LEN(recursive_rsync_args)); - if (error) - goto revert_rsync_program; - - error = string_array_init(&rpki_config.rsync.args.flat, + string_array_init(&rpki_config.rsync.args.flat, flat_rsync_args, ARRAY_LEN(flat_rsync_args)); - if (error) - goto revert_recursive_array; /* By default, has a higher priority than rsync */ rpki_config.http.enabled = true; @@ -1051,10 +1033,8 @@ set_default_values(void) rpki_config.http.retry.count = 1; rpki_config.http.retry.interval = 4; rpki_config.http.user_agent = strdup(PACKAGE_NAME "/" PACKAGE_VERSION); - if (rpki_config.http.user_agent == NULL) { - error = pr_enomem(); - goto revert_flat_array; - } + if (rpki_config.http.user_agent == NULL) + enomem_panic(); rpki_config.http.connect_timeout = 30; rpki_config.http.transfer_timeout = 0; rpki_config.http.low_speed_limit = 100000; @@ -1091,10 +1071,8 @@ set_default_values(void) rpki_config.validation_log.filename_format = FNF_GLOBAL; rpki_config.validation_log.facility = LOG_DAEMON; rpki_config.validation_log.tag = strdup("Validation"); - if (rpki_config.validation_log.tag == NULL) { - error = pr_enomem(); - goto revert_validation_log_tag; - } + if (rpki_config.validation_log.tag == NULL) + enomem_panic(); rpki_config.output.roa = NULL; rpki_config.output.bgpsec = NULL; @@ -1110,24 +1088,6 @@ set_default_values(void) rpki_config.thread_pool.server.max = 20; /* Usually 5 TALs, let a few more available */ rpki_config.thread_pool.validation.max = 5; - - return 0; - -revert_validation_log_tag: - free(rpki_config.http.user_agent); -revert_flat_array: - string_array_cleanup(&rpki_config.rsync.args.flat); -revert_recursive_array: - string_array_cleanup(&rpki_config.rsync.args.recursive); -revert_rsync_program: - free(rpki_config.rsync.program); -revert_repository: - free(rpki_config.local_repository); -revert_port: - free(rpki_config.server.port); -revert_address: - string_array_cleanup(&rpki_config.server.address); - return error; } static bool @@ -1239,15 +1199,11 @@ handle_flags_config(int argc, char **argv) int error; program_name = argv[0]; - error = set_default_values(); - if (error) - return error; + set_default_values(); long_opts = NULL; short_opts = NULL; - error = construct_getopt_options(&long_opts, &short_opts); - if (error) - goto end; /* Error msg already printed. */ + construct_getopt_options(&long_opts, &short_opts); while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { diff --git a/src/config/str.c b/src/config/str.c index d822ffc1..f1f5cbfa 100644 --- a/src/config/str.c +++ b/src/config/str.c @@ -34,7 +34,10 @@ string_parse_argv(struct option_field const *field, char const *str, __string_free(result); DEREFERENCE(result) = strdup(str); - return (DEREFERENCE(result) != NULL) ? 0 : pr_enomem(); + if (DEREFERENCE(result) == NULL) + enomem_panic(); + + return 0; } static int diff --git a/src/config/string_array.c b/src/config/string_array.c index e2ca0a2f..49bd1fac 100644 --- a/src/config/string_array.c +++ b/src/config/string_array.c @@ -9,7 +9,7 @@ #include "str_token.h" #include "config/str.h" -int +void string_array_init(struct string_array *array, char const *const *values, size_t len) { @@ -19,22 +19,20 @@ string_array_init(struct string_array *array, char const *const *values, if (len == 0) { array->array = NULL; - return 0; + return; } array->array = calloc(len, sizeof(char *)); if (array->array == NULL) - return pr_enomem(); + enomem_panic(); for (i = 0; i < len; i++) { array->array[i] = strdup(values[i]); if (array->array[i] == NULL) { string_array_cleanup(array); - return pr_enomem(); + enomem_panic(); } } - - return 0; } void @@ -108,7 +106,7 @@ string_array_parse_json(struct option_field const *opt, json_t *json, result->array = calloc(len, sizeof(char *)); if (result->array == NULL) - return pr_enomem(); + enomem_panic(); result->length = len; for (i = 0; i < len; i++) { @@ -118,10 +116,8 @@ string_array_parse_json(struct option_field const *opt, json_t *json, goto fail; result->array[i] = strdup(tmp); - if (result->array[i] == NULL) { - error = pr_enomem(); - goto fail; - } + if (result->array[i] == NULL) + enomem_panic(); } return 0; @@ -138,7 +134,6 @@ string_array_parse_argv(struct option_field const *opt, char const *str, struct string_tokenizer tokenizer; struct string_array *result; size_t i, len; - int error; result = _result; @@ -154,20 +149,13 @@ string_array_parse_argv(struct option_field const *opt, char const *str, result->array = calloc(len, sizeof(char *)); if (result->array == NULL) - return pr_enomem(); + enomem_panic(); result->length = len; - for (i = 0; string_tokenizer_next(&tokenizer); i++) { - error = token_read(&tokenizer, &result->array[i]); - if (error) - goto fail; - } + for (i = 0; string_tokenizer_next(&tokenizer); i++) + result->array[i] = token_read(&tokenizer); return 0; - -fail: - __string_array_free(result); - return error; } static void diff --git a/src/config/string_array.h b/src/config/string_array.h index 14e0eb9d..90bbf994 100644 --- a/src/config/string_array.h +++ b/src/config/string_array.h @@ -12,7 +12,7 @@ struct string_array { extern const struct global_type gt_string_array; -int string_array_init(struct string_array *, char const *const *, size_t); +void string_array_init(struct string_array *, char const *const *, size_t); void string_array_cleanup(struct string_array *); #endif /* SRC_CONFIG_STRING_ARRAY_H_ */ diff --git a/src/crypto/base64.c b/src/crypto/base64.c index f839bc78..b2e50590 100644 --- a/src/crypto/base64.c +++ b/src/crypto/base64.c @@ -65,7 +65,9 @@ base64_decode(BIO *in, unsigned char *out, bool has_nl, size_t out_len, b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) { error = ERR_peek_last_error(); - return error ? error_ul2i(error) : pr_enomem(); + if (error) + return error_ul2i(error); + enomem_panic(); } /* @@ -150,7 +152,7 @@ base64url_decode(char const *str_encoded, unsigned char **result, str_copy = malloc(encoded_len + pad + 1); if (str_copy == NULL) - return pr_enomem(); + enomem_panic(); /* Set all with pad char, then replace with the original string */ memset(str_copy, '=', encoded_len + pad); memcpy(str_copy, str_encoded, encoded_len); @@ -172,10 +174,8 @@ base64url_decode(char const *str_encoded, unsigned char **result, alloc_size = EVP_DECODE_LENGTH(strlen(str_copy)); *result = malloc(alloc_size + 1); - if (*result == NULL) { - error = pr_enomem(); - goto free_enc; - } + if (*result == NULL) + enomem_panic(); memset(*result, 0, alloc_size); (*result)[alloc_size] = '\0'; @@ -194,15 +194,14 @@ base64url_decode(char const *str_encoded, unsigned char **result, return 0; free_all: free(*result); -free_enc: BIO_free(encoded); free_copy: free(str_copy); return error; } -static int -to_base64url(char *base, size_t base_len, char **out) +static char * +to_base64url(char *base, size_t base_len) { char *pad, *tmp; size_t len; @@ -219,7 +218,7 @@ to_base64url(char *base, size_t base_len, char **out) tmp = malloc(len + 1); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); memcpy(tmp, base, len); tmp[len] = '\0'; @@ -231,8 +230,7 @@ to_base64url(char *base, size_t base_len, char **out) tmp[i] = '_'; } - *out = tmp; - return 0; + return tmp; } /* @@ -251,7 +249,7 @@ base64url_encode(unsigned char const *in, int in_len, char **result) mem = BIO_new(BIO_s_mem()); if (mem == NULL) { error = ERR_peek_last_error(); - return error ? error_ul2i(error) : pr_enomem(); + goto peeked; } b64 = BIO_new(BIO_f_base64()); @@ -266,13 +264,15 @@ base64url_encode(unsigned char const *in, int in_len, char **result) BIO_flush(b64); BIO_get_mem_ptr(mem, &mem_buf); - error = to_base64url(mem_buf->data, mem_buf->length, result); - if (error) - goto free_mem; + *result = to_base64url(mem_buf->data, mem_buf->length); BIO_free_all(b64); return 0; + free_mem: BIO_free_all(b64); - return error ? error_ul2i(error) : pr_enomem(); +peeked: + if (error) + return error_ul2i(error); + enomem_panic(); } diff --git a/src/crypto/hash.c b/src/crypto/hash.c index 68b820f7..fa3079ba 100644 --- a/src/crypto/hash.c +++ b/src/crypto/hash.c @@ -64,20 +64,16 @@ hash_local_file(char const *algorithm, char const *uri, unsigned char *result, buffer_len = stat.st_blksize; buffer = malloc(buffer_len); - if (buffer == NULL) { - error = pr_enomem(); - goto end1; - } + if (buffer == NULL) + enomem_panic(); ctx = EVP_MD_CTX_new(); - if (ctx == NULL) { - error = pr_enomem(); - goto end2; - } + if (ctx == NULL) + enomem_panic(); if (!EVP_DigestInit_ex(ctx, md, NULL)) { error = val_crypto_err("EVP_DigestInit_ex() failed"); - goto end3; + goto end; } do { @@ -86,12 +82,12 @@ hash_local_file(char const *algorithm, char const *uri, unsigned char *result, if (error) { pr_val_err("File reading error. Error message (apparently): %s", strerror(error)); - goto end3; + goto end; } if (!EVP_DigestUpdate(ctx, buffer, consumed)) { error = val_crypto_err("EVP_DigestUpdate() failed"); - goto end3; + goto end; } } while (!feof(file)); @@ -99,11 +95,9 @@ hash_local_file(char const *algorithm, char const *uri, unsigned char *result, if (!EVP_DigestFinal_ex(ctx, result, result_len)) error = val_crypto_err("EVP_DigestFinal_ex() failed"); -end3: +end: EVP_MD_CTX_free(ctx); -end2: free(buffer); -end1: file_close(file); return error; } @@ -195,7 +189,7 @@ hash_buffer(char const *algorithm, ctx = EVP_MD_CTX_new(); if (ctx == NULL) - return pr_enomem(); + enomem_panic(); if (!EVP_DigestInit_ex(ctx, md, NULL) || !EVP_DigestUpdate(ctx, content, content_len) @@ -252,7 +246,7 @@ hash_str(char const *algorithm, char const *str, unsigned char *result, src = malloc(strlen(str)); if (src == NULL) - return pr_enomem(); + enomem_panic(); memcpy(src, str, strlen(str)); diff --git a/src/daemon.c b/src/daemon.c index 3fadba68..98cb670e 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -32,7 +32,7 @@ daemonize(daemon_log_cb log_cb) /* Get the working dir, the daemon will use (and free) it later */ pwd = getcwd(NULL, 0); if (pwd == NULL) - return pr_enomem(); + enomem_panic(); pid = fork(); if (pid < 0) { diff --git a/src/data_structure/array_list.h b/src/data_structure/array_list.h index 01808b47..86248f3d 100644 --- a/src/data_structure/array_list.h +++ b/src/data_structure/array_list.h @@ -19,7 +19,7 @@ #define DECLARE_ARRAY_LIST_FUNCTIONS(name, elem_type) \ void name##_init(struct name *); \ void name##_cleanup(struct name *, void (*cb)(elem_type *)); \ - int name##_add(struct name *list, elem_type *elem); + void name##_add(struct name *list, elem_type *elem); #define DEFINE_ARRAY_LIST_FUNCTIONS(name, elem_type, modifiers) \ modifiers void \ @@ -42,7 +42,7 @@ } \ \ /* Will store a shallow copy, not @elem */ \ - modifiers int \ + modifiers void \ name##_add(struct name *list, elem_type *elem) \ { \ elem_type *tmp; \ @@ -52,7 +52,7 @@ list->array = malloc(list->capacity \ * sizeof(elem_type)); \ if (list->array == NULL) \ - return pr_enomem(); \ + enomem_panic(); \ } \ \ list->len++; \ @@ -62,12 +62,11 @@ tmp = realloc(list->array, list->capacity \ * sizeof(elem_type)); \ if (tmp == NULL) \ - return pr_enomem(); \ + enomem_panic(); \ list->array = tmp; \ } \ \ list->array[list->len - 1] = *elem; \ - return 0; \ } #define ARRAY_LIST(name, elem_type) \ diff --git a/src/data_structure/uthash_nonfatal.h b/src/data_structure/uthash_nonfatal.h deleted file mode 100644 index b3b54de6..00000000 --- a/src/data_structure/uthash_nonfatal.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef SRC_DATA_STRUCTURE_UTHASH_NONFATAL_H_ -#define SRC_DATA_STRUCTURE_UTHASH_NONFATAL_H_ - -#include - -/* - * "To enable "returning a failure" if memory cannot be allocated, define the - * macro HASH_NONFATAL_OOM before including the uthash.h header file." - * (http://troydhanson.github.io/uthash/userguide.html#_out_of_memory) - * - * The errno variable will be set to ENOMEM, so that the caller can detect the - * error. - * - * This validation (check for errno) must be done on ops that allocate memory, - * so set 'errno' to 0 before this ops are made. The 'obj' won't be freed, - * this is the caller's responsibility. - * - * Functions that can OOM: - * - * HASH_ADD_TO_TABLE - * HASH_ADD_KEYPTR_BYHASHVALUE_INORDER - * HASH_REPLACE_BYHASHVALUE_INORDER - * HASH_REPLACE_INORDER - * HASH_ADD_KEYPTR_INORDER - * HASH_ADD_INORDER - * HASH_ADD_BYHASHVALUE_INORDER - * HASH_ADD_KEYPTR_BYHASHVALUE - * HASH_REPLACE_BYHASHVALUE - * HASH_REPLACE (*) - * HASH_REPLACE_STR (**) - * HASH_REPLACE_INT - * HASH_REPLACE_PTR - * HASH_ADD_KEYPTR - * HASH_ADD - * HASH_ADD_BYHASHVALUE - * HASH_SELECT - * - * (*) Used by Fort - * (**) Used by Fort, but only in its fatal uthash form. - */ -#define HASH_NONFATAL_OOM 1 -#define uthash_nonfatal_oom(obj) \ - errno = ENOMEM; \ - -#include "data_structure/uthash.h" - -#endif /* SRC_DATA_STRUCTURE_UTHASH_NONFATAL_H_ */ diff --git a/src/delete_dir_daemon.c b/src/delete_dir_daemon.c index ea4a327d..f1579385 100644 --- a/src/delete_dir_daemon.c +++ b/src/delete_dir_daemon.c @@ -142,10 +142,7 @@ get_local_path(char const *rcvd, char const *workspace, char **result) int error; /* Currently, only rsync URIs are utilized */ - local_path = NULL; - error = map_uri_to_local(rcvd, "rsync://", workspace, &local_path); - if (error) - return error; + local_path = map_uri_to_local(rcvd, "rsync://", workspace); error = stat(local_path, &attr); if (error) { @@ -170,10 +167,8 @@ get_local_path(char const *rcvd, char const *workspace, char **result) tmp_size--; tmp = malloc(tmp_size + 1); - if (tmp == NULL) { - error = pr_enomem(); - goto release_local; - } + if (tmp == NULL) + enomem_panic(); strncpy(tmp, local_path, tmp_size); tmp[tmp_size] = '\0'; @@ -186,12 +181,6 @@ release_local: return error; } -/* - * Soft/hard error logic utilized, beware to prepare caller: - * - '> 0' is a soft error - * - '< 0' is a hard error - * - '= 0' no error - */ static int rename_local_path(char const *rcvd, char **result) { @@ -205,7 +194,7 @@ rename_local_path(char const *rcvd, char **result) tmp_size = rcvd_size + 1 + (sizeof(RAND_MAX) * 2); tmp = malloc(tmp_size + 1); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); /* Rename the path with a random suffix */ random_init(); @@ -213,12 +202,12 @@ rename_local_path(char const *rcvd, char **result) snprintf(tmp, tmp_size + 1, "%s_%08lX", rcvd, random_sfx); - error = rename(rcvd, tmp); - if (error) { + if (rename(rcvd, tmp) != 0) { + error = errno; free(tmp); pr_op_debug("Couldn't rename '%s' to delete it (discarding): %s", - rcvd, strerror(errno)); - return errno; /* Soft error */ + rcvd, strerror(error)); + return error; } *result = tmp; @@ -244,9 +233,7 @@ rename_all_roots(struct rem_dirs *rem_dirs, char **src, char const *workspace) delete_path = NULL; error = rename_local_path(local_path, &delete_path); free(local_path); - if (error < 0) - return error; - if (error > 0) + if (error) continue; rem_dirs->arr[rem_dirs->arr_set++] = delete_path; } @@ -254,26 +241,23 @@ rename_all_roots(struct rem_dirs *rem_dirs, char **src, char const *workspace) return 0; } -static int -rem_dirs_create(size_t arr_len, struct rem_dirs **result) +static struct rem_dirs * +rem_dirs_create(size_t arr_len) { struct rem_dirs *tmp; tmp = malloc(sizeof(struct rem_dirs)); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); tmp->arr = calloc(arr_len, sizeof(char *)); - if (tmp->arr == NULL) { - free(tmp); - return pr_enomem(); - } + if (tmp->arr == NULL) + enomem_panic(); tmp->arr_len = arr_len; tmp->arr_set = 0; - *result = tmp; - return 0; + return tmp; } static void @@ -302,10 +286,7 @@ delete_dir_daemon_start(char **roots, size_t roots_len, char const *workspace) struct rem_dirs *arg; int error; - arg = NULL; - error = rem_dirs_create(roots_len, &arg); - if (error) - return error; + arg = rem_dirs_create(roots_len); error = rename_all_roots(arg, roots, workspace); if (error) { @@ -314,11 +295,7 @@ delete_dir_daemon_start(char **roots, size_t roots_len, char const *workspace) } /* Thread arg is released at thread */ - error = internal_pool_push("Directory deleter", remove_from_root, arg); - if (error) { - rem_dirs_destroy(arg); - return error; - } + internal_pool_push("Directory deleter", remove_from_root, arg); return 0; } diff --git a/src/file.c b/src/file.c index 27aeeb46..3a36d6e7 100644 --- a/src/file.c +++ b/src/file.c @@ -71,10 +71,8 @@ file_load(char const *file_name, struct file_contents *fc) fc->buffer_size = stat.st_size; fc->buffer = malloc(fc->buffer_size); - if (fc->buffer == NULL) { - error = pr_enomem(); - goto end; - } + if (fc->buffer == NULL) + enomem_panic(); fread_result = fread(fc->buffer, 1, fc->buffer_size, file); if (fread_result < fc->buffer_size) { diff --git a/src/http/http.c b/src/http/http.c index 785b4c81..48a003cf 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -111,14 +111,14 @@ setopt_writedata(CURL *curl, struct write_callback_arg *arg) } } -static int +static void http_easy_init(struct http_handler *handler) { CURL *result; result = curl_easy_init(); if (result == NULL) - return pr_enomem(); + enomem_panic(); setopt_str(result, CURLOPT_USERAGENT, config_get_http_user_agent()); @@ -162,7 +162,6 @@ http_easy_init(struct http_handler *handler) setopt_long(result, CURLOPT_NOSIGNAL, 1L); handler->curl = result; - return 0; } static char const * @@ -365,7 +364,7 @@ __http_download_file(struct rpki_uri *uri, long *response_code, long ims_value, tmp_file = malloc(strlen(original_file) + strlen(tmp_suffix) + 1); if (tmp_file == NULL) - return pr_enomem(); + enomem_panic(); strcpy(tmp_file, original_file); strcat(tmp_file, tmp_suffix); @@ -378,9 +377,7 @@ __http_download_file(struct rpki_uri *uri, long *response_code, long ims_value, goto delete_dir; do { - error = http_easy_init(&handler); - if (error) - goto close_file; + http_easy_init(&handler); /* Set "If-Modified-Since" header only if a value is specified */ if (ims_value > 0) { @@ -427,8 +424,6 @@ __http_download_file(struct rpki_uri *uri, long *response_code, long ims_value, end: free(tmp_file); return 0; -close_file: - file_close(out); delete_dir: delete_dir_recursive_bottom_up(tmp_file); release_tmp: @@ -526,31 +521,27 @@ http_direct_download(char const *remote, char const *dest) tmp_file = strdup(dest); if (tmp_file == NULL) - return pr_enomem(); + enomem_panic(); tmp = realloc(tmp_file, strlen(tmp_file) + strlen(tmp_suffix) + 1); - if (tmp == NULL) { - error = pr_enomem(); - goto release_tmp; - } + if (tmp == NULL) + enomem_panic(); tmp_file = tmp; strcat(tmp_file, tmp_suffix); error = file_write(tmp_file, &out); if (error) - goto release_tmp; - - error = http_easy_init(&handler); - if (error) - goto close_file; + goto end; + http_easy_init(&handler); error = http_fetch(&handler, remote, &response_code, &cond_met, true, out); http_easy_cleanup(&handler); + file_close(out); if (error) - goto release_tmp; + goto end; /* Overwrite the original file */ error = rename(tmp_file, dest); @@ -558,14 +549,10 @@ http_direct_download(char const *remote, char const *dest) error = errno; pr_val_err("Renaming temporal file from '%s' to '%s': %s", tmp_file, dest, strerror(error)); - goto release_tmp; + goto end; } - free(tmp_file); - return 0; -close_file: - file_close(out); -release_tmp: +end: free(tmp_file); return error; } diff --git a/src/init.c b/src/init.c index f26ba926..021ed1fc 100644 --- a/src/init.c +++ b/src/init.c @@ -39,7 +39,7 @@ fetch_url(char const *url) dest = malloc(dest_dir_len + extra_slash + strlen(dest_file) + 1); if (dest == NULL) - return pr_enomem(); + enomem_panic(); offset = 0; strcpy(dest + offset, dest_dir); diff --git a/src/internal_pool.c b/src/internal_pool.c index 22c5338a..3a5d3bef 100644 --- a/src/internal_pool.c +++ b/src/internal_pool.c @@ -27,10 +27,10 @@ internal_pool_init(void) return 0; } -int +void internal_pool_push(char const *task_name, thread_pool_task_cb cb, void *arg) { - return thread_pool_push(pool, task_name, cb, arg); + thread_pool_push(pool, task_name, cb, arg); } void diff --git a/src/internal_pool.h b/src/internal_pool.h index 01aed8c2..1eeacad6 100644 --- a/src/internal_pool.h +++ b/src/internal_pool.h @@ -4,7 +4,7 @@ #include "thread/thread_pool.h" int internal_pool_init(void); -int internal_pool_push(char const *, thread_pool_task_cb, void *); +void internal_pool_push(char const *, thread_pool_task_cb, void *); void internal_pool_cleanup(void); #endif /* SRC_INTERNAL_POOL_H_ */ diff --git a/src/json_handler.c b/src/json_handler.c index a7cee0f5..b6454e5f 100644 --- a/src/json_handler.c +++ b/src/json_handler.c @@ -7,8 +7,8 @@ #include "log.h" #include "config/types.h" -static int -find_json(struct json_t *root, char const *full_name, json_t **result) +static json_t * +find_json(struct json_t *root, char const *full_name) { struct { char *opt_name; /* full token sequence string */ @@ -20,7 +20,7 @@ find_json(struct json_t *root, char const *full_name, json_t **result) /* strtok_r() needs a non-const string */ strtok.opt_name = strdup(full_name); if (strtok.opt_name == NULL) - return pr_enomem(); + enomem_panic(); node = root; strtok.token = strtok_r(strtok.opt_name, ".", &strtok.saveptr); @@ -31,8 +31,7 @@ find_json(struct json_t *root, char const *full_name, json_t **result) } free(strtok.opt_name); - *result = node; - return 0; + return node; } static int @@ -43,9 +42,7 @@ json_to_config(struct json_t *root) int error; FOREACH_OPTION(get_option_metadatas(), opt, AVAILABILITY_JSON) { - error = find_json(root, opt->name, &child); - if (error) - return error; + child = find_json(root, opt->name); if (child == NULL) continue; diff --git a/src/line_file.c b/src/line_file.c index ffce6c27..fe29b168 100644 --- a/src/line_file.c +++ b/src/line_file.c @@ -24,7 +24,7 @@ lfile_open(const char *file_name, struct line_file **result) lfile = malloc(sizeof(struct line_file)); if (lfile == NULL) - return pr_enomem(); + enomem_panic(); lfile->file = fopen(file_name, "r"); if (lfile->file == NULL) { diff --git a/src/log.c b/src/log.c index bb3466e3..9a4af223 100644 --- a/src/log.c +++ b/src/log.c @@ -633,8 +633,8 @@ val_crypto_err(const char *format, ...) return crypto_err(&val_config, pr_val_err); } -int -pr_enomem(void) +__dead void +enomem_panic(void) { static char const *ENOMEM_MSG = "Out of memory.\n"; ssize_t garbage; @@ -645,16 +645,20 @@ pr_enomem(void) */ if (LOG_ERR > op_config.level) - return -ENOMEM; + goto done; if (op_config.fprintf_enabled) { lock_mutex(); /* * write() is AS-Safe, which implies it doesn't allocate, * unlike printf(). + * + * "garbage" prevents write()'s warn_unused_result (compiler + * warning). */ garbage = write(STDERR_FILENO, ENOMEM_MSG, strlen(ENOMEM_MSG)); unlock_mutex(); + /* Prevents "set but not used" warning. */ garbage++; } @@ -665,7 +669,7 @@ pr_enomem(void) unlock_mutex(); } - return -ENOMEM; +done: exit(ENOMEM); } __dead void diff --git a/src/log.h b/src/log.h index 4575afa8..c261940f 100644 --- a/src/log.h +++ b/src/log.h @@ -91,8 +91,11 @@ int pr_val_err(const char *, ...) CHECK_FORMAT(1, 2); /* Like pr_val_err(), except it prints libcrypto's error stack as well. */ int val_crypto_err(const char *, ...) CHECK_FORMAT(1, 2); -/* Like pr_*_err(), specific to out-of-memory situations. */ -int pr_enomem(void); +/* + * Like pr_*_err(), specific to out-of-memory situations. + * Also terminates the program. + */ +__dead void enomem_panic(void); /* Programming errors */ __dead void pr_crit(const char *, ...) CHECK_FORMAT(1, 2); diff --git a/src/object/bgpsec.c b/src/object/bgpsec.c index e847efb1..e6274140 100644 --- a/src/object/bgpsec.c +++ b/src/object/bgpsec.c @@ -36,7 +36,7 @@ handle_bgpsec(X509 *cert, unsigned char const *ski, struct resources *resources) cert_spk = malloc(RK_SPKI_LEN); if (cert_spk == NULL) - return pr_enomem(); + enomem_panic(); /* Use a temporal pointer, since i2d_X509_PUBKEY moves it */ tmp = cert_spk; diff --git a/src/object/certificate.c b/src/object/certificate.c index c6f61efd..56c3d30d 100644 --- a/src/object/certificate.c +++ b/src/object/certificate.c @@ -106,7 +106,6 @@ validate_serial_number(X509 *cert) { struct validation *state; BIGNUM *number; - int error; state = state_retrieve(); if (state == NULL) @@ -119,11 +118,8 @@ validate_serial_number(X509 *cert) if (log_val_enabled(LOG_DEBUG)) debug_serial_number(number); - error = x509stack_store_serial(validation_certstack(state), number); - if (error) - BN_free(number); - - return error; + x509stack_store_serial(validation_certstack(state), number); + return 0; } static int @@ -832,7 +828,7 @@ update_crl_time(STACK_OF(X509_CRL) *crls, X509_CRL *original_crl) clone = X509_CRL_dup(original_crl); if (clone == NULL) { ASN1_STRING_free(tm); - return pr_enomem(); + enomem_panic(); } X509_CRL_set1_nextUpdate(clone, tm); @@ -2242,7 +2238,7 @@ verify_mft: * This will return: * rsync:// */ -static int +static void get_rsync_server_uri(struct rpki_uri *src, char **result, size_t *result_len) { char const *global; @@ -2265,18 +2261,16 @@ get_rsync_server_uri(struct rpki_uri *src, char **result, size_t *result_len) tmp = malloc(i + 1); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); strncpy(tmp, global, i); tmp[i] = '\0'; *result_len = i; *result = tmp; - - return 0; } -static int +static void set_repository_level(bool is_ta, struct validation *state, struct rpki_uri *cert_uri, struct sia_ca_uris *sia_uris, bool *updated) { @@ -2284,12 +2278,11 @@ set_repository_level(bool is_ta, struct validation *state, size_t parent_server_len, current_server_len; unsigned int new_level; bool update; - int error; new_level = 0; if (is_ta || cert_uri == NULL) { working_repo_push_level(new_level); - return 0; + return; } /* Warning killer */ @@ -2299,17 +2292,9 @@ set_repository_level(bool is_ta, struct validation *state, current_server_len = 0; /* Both are rsync URIs, check the server part */ - error = get_rsync_server_uri(cert_uri, &parent_server, - &parent_server_len); - if (error) - return error; - - error = get_rsync_server_uri(sia_uris->caRepository.uri, - ¤t_server, ¤t_server_len); - if (error) { - free(parent_server); - return error; - } + get_rsync_server_uri(cert_uri, &parent_server, &parent_server_len); + get_rsync_server_uri(sia_uris->caRepository.uri, ¤t_server, + ¤t_server_len); if (parent_server_len != current_server_len) { update = true; @@ -2328,7 +2313,6 @@ end: free(current_server); (*updated) = update; - return 0; } /** Boilerplate code for CA certificate validation and recursive traversal. */ @@ -2432,10 +2416,7 @@ certificate_traverse(struct rpp *rpp_parent, struct rpki_uri *cert_uri) /* Identify if this is a new repository before fetching it */ new_level = false; - error = set_repository_level(IS_TA, state, cert_uri, &sia_uris, - &new_level); - if (error) - goto revert_uris; + set_repository_level(IS_TA, state, cert_uri, &sia_uris, &new_level); /* * RFC 6481 section 5: "when the repository publication point contents diff --git a/src/object/ghostbusters.c b/src/object/ghostbusters.c index b9372c9e..e67ecbd1 100644 --- a/src/object/ghostbusters.c +++ b/src/object/ghostbusters.c @@ -37,9 +37,7 @@ ghostbusters_traverse(struct rpki_uri *uri, struct rpp *pp) error = rpp_crl(pp, &crl); if (error) goto revert_sobj; - error = signed_object_args_init(&sobj_args, uri, crl, true); - if (error) - goto revert_sobj; + signed_object_args_init(&sobj_args, uri, crl, true); /* Validate everything */ error = signed_object_validate(&sobj, &arcs, &sobj_args); diff --git a/src/object/manifest.c b/src/object/manifest.c index 68c2fb77..56da892c 100644 --- a/src/object/manifest.c +++ b/src/object/manifest.c @@ -162,7 +162,7 @@ build_rpp(struct Manifest *mft, struct rpki_uri *mft_uri, bool rrdp_workspace, *pp = rpp_create(); if (*pp == NULL) - return pr_enomem(); + enomem_panic(); for (i = 0; i < mft->fileList.list.count; i++) { fah = mft->fileList.list.array[i]; @@ -200,13 +200,13 @@ build_rpp(struct Manifest *mft, struct rpki_uri *mft_uri, bool rrdp_workspace, } if (uri_has_extension(uri, ".cer")) - error = rpp_add_cert(*pp, uri); + rpp_add_cert(*pp, uri); else if (uri_has_extension(uri, ".roa")) - error = rpp_add_roa(*pp, uri); + rpp_add_roa(*pp, uri); else if (uri_has_extension(uri, ".crl")) error = rpp_add_crl(*pp, uri); else if (uri_has_extension(uri, ".gbr")) - error = rpp_add_ghostbusters(*pp, uri); + rpp_add_ghostbusters(*pp, uri); else uri_refput(uri); /* ignore it. */ @@ -265,9 +265,7 @@ handle_manifest(struct rpki_uri *uri, bool rrdp_workspace, struct rpp **pp) error = rpp_crl(*pp, &crl); if (error) goto revert_rpp; - error = signed_object_args_init(&sobj_args, uri, crl, false); - if (error) - goto revert_rpp; + signed_object_args_init(&sobj_args, uri, crl, false); /* Validate everything */ error = signed_object_validate(&sobj, &arcs, &sobj_args); diff --git a/src/object/name.c b/src/object/name.c index fd64bb42..7c6cb530 100644 --- a/src/object/name.c +++ b/src/object/name.c @@ -32,7 +32,7 @@ name2string(X509_NAME_ENTRY *name, char **_result) result = malloc(data->length + 1); if (result == NULL) - return pr_enomem(); + enomem_panic(); memcpy(result, data->data, data->length); result[data->length] = '\0'; @@ -53,7 +53,7 @@ x509_name_decode(X509_NAME *name, char const *what, result = malloc(sizeof(struct rfc5280_name)); if (result == NULL) - return pr_enomem(); + enomem_panic(); result->commonName = NULL; result->serialNumber = NULL; diff --git a/src/object/roa.c b/src/object/roa.c index fc979e54..40a5409a 100644 --- a/src/object/roa.c +++ b/src/object/roa.c @@ -278,9 +278,7 @@ roa_traverse(struct rpki_uri *uri, struct rpp *pp) error = rpp_crl(pp, &crl); if (error) goto revert_roa; - error = signed_object_args_init(&sobj_args, uri, crl, false); - if (error) - goto revert_roa; + signed_object_args_init(&sobj_args, uri, crl, false); /* Validate and handle everything */ error = signed_object_validate(&sobj, &arcs, &sobj_args); diff --git a/src/object/tal.c b/src/object/tal.c index 470e39c4..a2997ab7 100644 --- a/src/object/tal.c +++ b/src/object/tal.c @@ -72,7 +72,7 @@ struct tal_param { struct threads_list threads; }; -static int +static void uris_init(struct uris *uris) { uris->count = 0; @@ -80,7 +80,8 @@ uris_init(struct uris *uris) uris->https_count = 0; uris->size = 4; /* Most TALs only define one. */ uris->array = malloc(uris->size * sizeof(struct rpki_uri *)); - return (uris->array != NULL) ? 0 : pr_enomem(); + if (uris->array == NULL) + enomem_panic(); } static void @@ -102,7 +103,6 @@ uris_add(struct uris *uris, char *uri) error = uri_create_mixed_str(&new, uri, strlen(uri)); if (error == ENOTSUPPORTED) return pr_op_err("TAL has non-RSYNC/HTTPS URI."); - if (error) return error; @@ -115,10 +115,8 @@ uris_add(struct uris *uris, char *uri) uris->size *= 2; tmp = realloc(uris->array, uris->size * sizeof(struct rpki_uri *)); - if (tmp == NULL) { - uri_refput(new); - return pr_enomem(); - } + if (tmp == NULL) + enomem_panic(); uris->array = tmp; } @@ -236,13 +234,11 @@ base64_sanitize(struct line_file *lfile, char **out) new_size = original_size + (original_size / BUF_SIZE); result = malloc(new_size + 1); if (result == NULL) - return pr_enomem(); + enomem_panic(); buf = malloc(BUF_SIZE); - if (buf == NULL) { - free(result); - return pr_enomem(); - } + if (buf == NULL) + enomem_panic(); fd = lfile_fd(lfile); offset = 0; @@ -286,10 +282,8 @@ base64_sanitize(struct line_file *lfile, char **out) /* Reallocate to exact size and add nul char */ if (offset != new_size) { eol = realloc(result, offset + 1); - if (eol == NULL) { - error = pr_enomem(); - goto free_result; - } + if (eol == NULL) + enomem_panic(); result = eol; } free(buf); @@ -315,7 +309,7 @@ read_spki(struct line_file *lfile, struct tal *tal) alloc_size = get_spki_alloc_size(lfile); tal->spki = malloc(alloc_size); if (tal->spki == NULL) - return pr_enomem(); + enomem_panic(); tmp = NULL; error = base64_sanitize(lfile, &tmp); @@ -355,40 +349,33 @@ tal_load(char const *file_name, struct tal **result) if (error) { pr_op_err("Error opening file '%s': %s", file_name, strerror(abs(error))); - goto fail4; + return error; } tal = malloc(sizeof(struct tal)); - if (tal == NULL) { - error = pr_enomem(); - goto fail3; - } + if (tal == NULL) + enomem_panic(); tal->file_name = file_name; - error = uris_init(&tal->uris); - if (error) - goto fail2; + uris_init(&tal->uris); error = read_uris(lfile, &tal->uris); if (error) - goto fail1; + goto fail; error = read_spki(lfile, tal); if (error) - goto fail1; + goto fail; lfile_close(lfile); *result = tal; return 0; -fail1: +fail: uris_destroy(&tal->uris); -fail2: free(tal); -fail3: lfile_close(lfile); -fail4: return error; } @@ -437,7 +424,7 @@ tal_shuffle_uris(struct tal *tal) } } -static int +static void tal_order_uris(struct tal *tal) { struct rpki_uri **ordered; @@ -452,14 +439,14 @@ tal_order_uris(struct tal *tal) tal_shuffle_uris(tal); if (config_get_rsync_priority() == config_get_http_priority()) - return 0; + return; /* Now order according to the priority */ http_first = (config_get_http_priority() > config_get_rsync_priority()); ordered = malloc(tal->uris.size * sizeof(struct rpki_uri *)); if (ordered == NULL) - return pr_enomem(); + enomem_panic(); last_rsync = (http_first ? tal->uris.https_count : 0); last_https = (http_first ? 0 : tal->uris.rsync_count); @@ -475,8 +462,6 @@ tal_order_uris(struct tal *tal) tmp = tal->uris.array; tal->uris.array = ordered; free(tmp); - - return 0; } char const * @@ -671,9 +656,7 @@ do_file_validation(void *thread_arg) if (error) goto end; - error = tal_order_uris(tal); - if (error) - goto destroy_tal; + tal_order_uris(tal); error = foreach_uri(tal, __handle_tal_uri_sync, thread); if (error > 0) { @@ -727,38 +710,22 @@ __do_file_validation(char const *tal_file, void *arg) return error; thread = malloc(sizeof(struct validation_thread)); - if (thread == NULL) { - error = pr_enomem(); - goto free_db_rrdp; - } + if (thread == NULL) + enomem_panic(); thread->tal_file = strdup(tal_file); - if (thread->tal_file == NULL) { - error = pr_enomem(); - goto free_thread; - } + if (thread->tal_file == NULL) + enomem_panic(); thread->arg = t_param->db; thread->exit_status = -EINTR; thread->retry_local = true; thread->sync_files = true; - error = thread_pool_push(t_param->pool, thread->tal_file, - do_file_validation, thread); - if (error) { - pr_op_err("Couldn't push a thread to do files validation"); - goto free_tal_file; - } + thread_pool_push(t_param->pool, thread->tal_file, do_file_validation, + thread); SLIST_INSERT_HEAD(&t_param->threads, thread, next); return 0; - -free_tal_file: - free(thread->tal_file); -free_thread: - free(thread); -free_db_rrdp: - db_rrdp_rem_tal(tal_file); - return error; } int diff --git a/src/object/vcard.c b/src/object/vcard.c index eee6a408..bbaab38d 100644 --- a/src/object/vcard.c +++ b/src/object/vcard.c @@ -93,7 +93,7 @@ analyze_pos(struct utf8_string *string, size_t pos) return SA_LINE_ENDED; /* \r\n */ } -static int +static void double_line_size(struct vcard_line *line) { uint8_t *tmp; @@ -101,28 +101,19 @@ double_line_size(struct vcard_line *line) line->str.size *= 2; tmp = realloc(line->str.val, line->str.size); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); line->str.val = tmp; - - return 0; } -static int +static void add_chara(struct vcard_line *line, uint8_t chara, bool inc_str_len) { - int error; - - if (line->str.len + 1 == line->str.size) { - error = double_line_size(line); - if (error) - return error; - } + if (line->str.len + 1 == line->str.size) + double_line_size(line); line->str.val[line->str.len] = chara; if (inc_str_len) line->str.len++; - - return 0; } /** @@ -136,7 +127,6 @@ line_next(struct vcard_line *line, OCTET_STRING_t *string8) { struct utf8_string string; size_t string_pos; - int error; if (string8->size == line->octet_string_offset) return pr_val_err("vCard ends prematurely. (Expected an END line)"); @@ -149,14 +139,13 @@ line_next(struct vcard_line *line, OCTET_STRING_t *string8) for (string_pos = 0; string_pos < string.len; string_pos++) { switch (analyze_pos(&string, string_pos)) { case SA_COPY_CHARA: - error = add_chara(line, string.val[string_pos], true); - if (error) - return error; + add_chara(line, string.val[string_pos], true); break; case SA_LINE_ENDED: line->octet_string_offset += string_pos + 2; - return add_chara(line, 0, false); + add_chara(line, 0, false); + return 0; case SA_SKIP_THREE_CHARAS: string_pos += 2; @@ -274,7 +263,7 @@ handle_ghostbusters_vcard(OCTET_STRING_t *vcard) line.str.len = 0; line.str.val = malloc(line.str.size); if (line.str.val == NULL) - return pr_enomem(); + enomem_panic(); line.octet_string_offset = 0; error = __handle_ghostbusters_vcard(vcard, &line); diff --git a/src/reqs_errors.c b/src/reqs_errors.c index b91b6f0d..31b721c8 100644 --- a/src/reqs_errors.c +++ b/src/reqs_errors.c @@ -53,16 +53,14 @@ error_uri_create(char const *uri, struct error_uri **err_uri) tmp = malloc(sizeof(struct error_uri)); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); /* Needed by uthash */ memset(tmp, 0, sizeof(struct error_uri)); tmp->uri = strdup(uri); - if (tmp->uri == NULL) { - error = pr_enomem(); - goto release_tmp; - } + if (tmp->uri == NULL) + enomem_panic(); error = get_current_time(&tmp->first_attempt); if (error) @@ -74,9 +72,9 @@ error_uri_create(char const *uri, struct error_uri **err_uri) *err_uri = tmp; return 0; + release_uri: free(tmp->uri); -release_tmp: free(tmp); return error; } diff --git a/src/resource.c b/src/resource.c index 232309be..d5fee05c 100644 --- a/src/resource.c +++ b/src/resource.c @@ -154,7 +154,7 @@ add_prefix4(struct resources *resources, IPAddress_t *addr) if (resources->ip4s == NULL) { resources->ip4s = res4_create(); if (resources->ip4s == NULL) - return pr_enomem(); + enomem_panic(); } error = res4_add_prefix(resources->ip4s, &prefix); @@ -199,7 +199,7 @@ add_prefix6(struct resources *resources, IPAddress_t *addr) if (resources->ip6s == NULL) { resources->ip6s = res6_create(); if (resources->ip6s == NULL) - return pr_enomem(); + enomem_panic(); } error = res6_add_prefix(resources->ip6s, &prefix); @@ -257,7 +257,7 @@ add_range4(struct resources *resources, IPAddressRange_t *input) if (resources->ip4s == NULL) { resources->ip4s = res4_create(); if (resources->ip4s == NULL) - return pr_enomem(); + enomem_panic(); } error = res4_add_range(resources->ip4s, &range); @@ -303,7 +303,7 @@ add_range6(struct resources *resources, IPAddressRange_t *input) if (resources->ip6s == NULL) { resources->ip6s = res6_create(); if (resources->ip6s == NULL) - return pr_enomem(); + enomem_panic(); } error = res6_add_range(resources->ip6s, &range); @@ -459,7 +459,7 @@ add_asn(struct resources *resources, unsigned long min, unsigned long max, if (resources->asns == NULL) { resources->asns = rasn_create(); if (resources->asns == NULL) - return pr_enomem(); + enomem_panic(); } error = rasn_add(resources->asns, min, max); diff --git a/src/rpp.c b/src/rpp.c index cd9ea95c..35d317c6 100644 --- a/src/rpp.c +++ b/src/rpp.c @@ -99,24 +99,24 @@ rpp_refput(struct rpp *pp) } /** Steals ownership of @uri. */ -int +void rpp_add_cert(struct rpp *pp, struct rpki_uri *uri) { - return uris_add(&pp->certs, &uri); + uris_add(&pp->certs, &uri); } /** Steals ownership of @uri. */ -int +void rpp_add_roa(struct rpp *pp, struct rpki_uri *uri) { - return uris_add(&pp->roas, &uri); + uris_add(&pp->roas, &uri); } /** Steals ownership of @uri. */ -int +void rpp_add_ghostbusters(struct rpp *pp, struct rpki_uri *uri) { - return uris_add(&pp->ghostbusters, &uri); + uris_add(&pp->ghostbusters, &uri); } /** Steals ownership of @uri. */ @@ -195,10 +195,8 @@ rpp_crl(struct rpp *pp, STACK_OF(X509_CRL) **result) /* -- Actually initialize pp->crl.stack. -- */ stack = sk_X509_CRL_new_null(); - if (stack == NULL) { - pp->crl.error = pr_enomem(); - return pp->crl.error; - } + if (stack == NULL) + enomem_panic(); pp->crl.error = add_crl_to_stack(pp, stack); if (pp->crl.error) { sk_X509_CRL_pop_free(stack, X509_CRL_free); @@ -217,7 +215,6 @@ __cert_traverse(struct rpp *pp) struct cert_stack *certstack; ssize_t i; struct deferred_cert deferred; - int error; if (pp->certs.len == 0) return 0; @@ -235,9 +232,7 @@ __cert_traverse(struct rpp *pp) */ for (i = pp->certs.len - 1; i >= 0; i--) { deferred.uri = pp->certs.array[i]; - error = deferstack_push(certstack, &deferred); - if (error) - return error; + deferstack_push(certstack, &deferred); } return 0; diff --git a/src/rpp.h b/src/rpp.h index ba75d256..72ddf7a9 100644 --- a/src/rpp.h +++ b/src/rpp.h @@ -9,10 +9,10 @@ struct rpp *rpp_create(void); void rpp_refget(struct rpp *pp); void rpp_refput(struct rpp *pp); -int rpp_add_cert(struct rpp *, struct rpki_uri *); +void rpp_add_cert(struct rpp *, struct rpki_uri *); int rpp_add_crl(struct rpp *, struct rpki_uri *); -int rpp_add_roa(struct rpp *, struct rpki_uri *); -int rpp_add_ghostbusters(struct rpp *, struct rpki_uri *); +void rpp_add_roa(struct rpp *, struct rpki_uri *); +void rpp_add_ghostbusters(struct rpp *, struct rpki_uri *); struct rpki_uri *rpp_get_crl(struct rpp const *); int rpp_crl(struct rpp *, STACK_OF(X509_CRL) **); diff --git a/src/rrdp/db/db_rrdp.c b/src/rrdp/db/db_rrdp.c index bdccbbe9..80f353f9 100644 --- a/src/rrdp/db/db_rrdp.c +++ b/src/rrdp/db/db_rrdp.c @@ -54,7 +54,7 @@ get_workspace_path(char const *base, char **result) hash = malloc(HASH_LEN * sizeof(unsigned char)); if (hash == NULL) - return pr_enomem(); + enomem_panic(); hash_len = 0; error = hash_str("sha1", base, hash, &hash_len); @@ -65,10 +65,8 @@ get_workspace_path(char const *base, char **result) /* Get the first bytes + one slash + NUL char */ tmp = malloc(OUT_LEN + 2); - if (tmp == NULL) { - free(hash); - return pr_enomem(); - } + if (tmp == NULL) + enomem_panic(); ptr = tmp; for (i = 0; i < OUT_LEN / 2; i++) { @@ -87,39 +85,28 @@ static int tal_elem_create(struct tal_elem **elem, char const *name) { struct tal_elem *tmp; - struct db_rrdp_uri *tmp_uris; int error; tmp = malloc(sizeof(struct tal_elem)); if (tmp == NULL) - return pr_enomem(); - - tmp_uris = NULL; - error = db_rrdp_uris_create(&tmp_uris); - if (error) - goto end1; - tmp->uris = tmp_uris; + enomem_panic(); + tmp->uris = db_rrdp_uris_create(); tmp->visited = true; tmp->file_name = strdup(name); - if (tmp->file_name == NULL) { - error = pr_enomem(); - goto end2; - } + if (tmp->file_name == NULL) + enomem_panic(); error = get_workspace_path(name, &tmp->workspace); - if (error) - goto end3; + if (error) { + free(tmp->file_name); + db_rrdp_uris_destroy(tmp->uris); + free(tmp); + return error; + } *elem = tmp; return 0; -end3: - free(tmp->file_name); -end2: - db_rrdp_uris_destroy(tmp->uris); -end1: - free(tmp); - return error; } static void diff --git a/src/rrdp/db/db_rrdp_uris.c b/src/rrdp/db/db_rrdp_uris.c index 38a30182..1bf0ced6 100644 --- a/src/rrdp/db/db_rrdp_uris.c +++ b/src/rrdp/db/db_rrdp_uris.c @@ -27,44 +27,32 @@ struct db_rrdp_uri { char const *current_workspace; }; -static int +static struct uris_table * uris_table_create(char const *uri, char const *session_id, - unsigned long serial, rrdp_req_status_t req_status, - struct uris_table **result) + unsigned long serial, rrdp_req_status_t req_status) { struct uris_table *tmp; - int error; tmp = malloc(sizeof(struct uris_table)); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); /* Needed by uthash */ memset(tmp, 0, sizeof(struct uris_table)); tmp->uri = strdup(uri); - if (tmp->uri == NULL) { - error = pr_enomem(); - goto release_tmp; - } + if (tmp->uri == NULL) + enomem_panic(); tmp->data.session_id = strdup(session_id); - if (tmp->data.session_id == NULL) { - error = pr_enomem(); - goto release_uri; - } + if (tmp->data.session_id == NULL) + enomem_panic(); tmp->data.serial = serial; tmp->last_update = 0; tmp->request_status = req_status; tmp->visited_uris = NULL; - *result = tmp; - return 0; -release_uri: - free(tmp->uri); -release_tmp: - free(tmp); - return error; + return tmp; } static void @@ -125,20 +113,19 @@ get_thread_rrdp_workspace(char const **result) return 0; } -int -db_rrdp_uris_create(struct db_rrdp_uri **uris) +struct db_rrdp_uri * +db_rrdp_uris_create(void) { struct db_rrdp_uri *tmp; tmp = malloc(sizeof(struct db_rrdp_uri)); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); tmp->table = NULL; tmp->current_workspace = NULL; - *uris = tmp; - return 0; + return tmp; } void @@ -207,16 +194,9 @@ db_rrdp_uris_update(char const *uri, char const *session_id, if (error) return error; - db_uri = NULL; - error = uris_table_create(uri, session_id, serial, req_status, &db_uri); - if (error) - return error; - - /* Ownership transfered */ - db_uri->visited_uris = visited_uris; - + db_uri = uris_table_create(uri, session_id, serial, req_status); + db_uri->visited_uris = visited_uris; /* Ownership transfered */ add_rrdp_uri(uris, db_uri); - return 0; } diff --git a/src/rrdp/db/db_rrdp_uris.h b/src/rrdp/db/db_rrdp_uris.h index 63ac8060..940f5df5 100644 --- a/src/rrdp/db/db_rrdp_uris.h +++ b/src/rrdp/db/db_rrdp_uris.h @@ -18,7 +18,7 @@ typedef enum { */ struct db_rrdp_uri; -int db_rrdp_uris_create(struct db_rrdp_uri **); +struct db_rrdp_uri *db_rrdp_uris_create(void); void db_rrdp_uris_destroy(struct db_rrdp_uri *); int db_rrdp_uris_cmp(char const *, char const *, unsigned long, diff --git a/src/rrdp/rrdp_loader.c b/src/rrdp/rrdp_loader.c index b43c48fa..cea7d830 100644 --- a/src/rrdp/rrdp_loader.c +++ b/src/rrdp/rrdp_loader.c @@ -41,9 +41,7 @@ process_snapshot(struct update_notification *notification, bool log_operation, int error; /* Use a new allocated visited_uris struct */ - error = visited_uris_create(&tmp); - if (error) - return error; + tmp = visited_uris_create(); error = rrdp_parse_snapshot(notification, tmp, log_operation); if (error) { @@ -82,9 +80,7 @@ mark_rrdp_uri_request_err(char const *notification_uri) pr_val_debug("RRDP data of '%s' won't be requested again during this cycle due to previous error.", notification_uri); - error = visited_uris_create(&tmp); - if (error) - return error; + tmp = visited_uris_create(); error = db_rrdp_uris_update(notification_uri, "", 0, RRDP_URI_REQ_ERROR, tmp); diff --git a/src/rrdp/rrdp_objects.c b/src/rrdp/rrdp_objects.c index 7898c30f..677987a2 100644 --- a/src/rrdp/rrdp_objects.c +++ b/src/rrdp/rrdp_objects.c @@ -150,19 +150,18 @@ update_notification_destroy(struct update_notification *file) free(file); } -int -snapshot_create(struct snapshot **file) +struct snapshot * +snapshot_create(void) { struct snapshot *tmp; tmp = malloc(sizeof(struct snapshot)); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); global_data_init(&tmp->global_data); - *file = tmp; - return 0; + return tmp; } void @@ -172,19 +171,18 @@ snapshot_destroy(struct snapshot *file) free(file); } -int -delta_create(struct delta **file) +struct delta * +delta_create(void) { struct delta *tmp; tmp = malloc(sizeof(struct delta)); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); global_data_init(&tmp->global_data); - *file = tmp; - return 0; + return tmp; } void @@ -194,21 +192,20 @@ delta_destroy(struct delta *file) free(file); } -int -publish_create(struct publish **file) +struct publish * +publish_create(void) { struct publish *tmp; tmp = malloc(sizeof(struct publish)); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); doc_data_init(&tmp->doc_data); tmp->content = NULL; tmp->content_len = 0; - *file = tmp; - return 0; + return tmp; } void @@ -219,19 +216,18 @@ publish_destroy(struct publish *file) free(file); } -int -withdraw_create(struct withdraw **file) +struct withdraw * +withdraw_create(void) { struct withdraw *tmp; tmp = malloc(sizeof(struct withdraw)); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); doc_data_init(&tmp->doc_data); - *file = tmp; - return 0; + return tmp; } void diff --git a/src/rrdp/rrdp_objects.h b/src/rrdp/rrdp_objects.h index b34970bb..f9a691db 100644 --- a/src/rrdp/rrdp_objects.h +++ b/src/rrdp/rrdp_objects.h @@ -97,16 +97,16 @@ int deltas_head_for_each(struct deltas_head *, unsigned long, unsigned long, delta_head_cb, void *); int deltas_head_sort(struct deltas_head *, unsigned long); -int snapshot_create(struct snapshot **); +struct snapshot *snapshot_create(void); void snapshot_destroy(struct snapshot *); -int delta_create(struct delta **); +struct delta *delta_create(void); void delta_destroy(struct delta *); -int publish_create(struct publish **); +struct publish *publish_create(void); void publish_destroy(struct publish *); -int withdraw_create(struct withdraw **); +struct withdraw *withdraw_create(void); void withdraw_destroy(struct withdraw *); #endif /* SRC_RRDP_RRDP_OBJECTS_H_ */ diff --git a/src/rrdp/rrdp_parser.c b/src/rrdp/rrdp_parser.c index 40216049..44f6b0b7 100644 --- a/src/rrdp/rrdp_parser.c +++ b/src/rrdp/rrdp_parser.c @@ -65,13 +65,11 @@ struct proc_upd_args { bool log_operation; }; -static int +static void add_mft_to_list(struct visited_uris *visited_uris, char const *uri) { - if (strcmp(".mft", strrchr(uri, '.')) != 0) - return 0; - - return visited_uris_add(visited_uris, uri); + if (strcmp(".mft", strrchr(uri, '.')) == 0) + visited_uris_add(visited_uris, uri); } static int @@ -152,7 +150,7 @@ base64_sanitize(char *content, char **out) if (original_size <= BUF_SIZE) { result = strdup(content); if (result == NULL) - return pr_enomem(); + enomem_panic(); *out = result; return 0; } @@ -160,7 +158,7 @@ base64_sanitize(char *content, char **out) new_size = original_size + (original_size / BUF_SIZE); result = malloc(new_size + 1); if (result == NULL) - return pr_enomem(); + enomem_panic(); offset = 0; while (original_size > 0){ @@ -179,10 +177,8 @@ base64_sanitize(char *content, char **out) /* Reallocate to exact size and add nul char */ if (offset != new_size + 1) { tmp = realloc(result, offset + 1); - if (tmp == NULL) { - free(result); - return pr_enomem(); - } + if (tmp == NULL) + enomem_panic(); result = tmp; } @@ -215,10 +211,8 @@ base64_read(char *content, unsigned char **out, size_t *out_len) alloc_size = EVP_DECODE_LENGTH(strlen(content)); result = malloc(alloc_size); - if (result == NULL) { - error = pr_enomem(); - goto release_bio; - } + if (result == NULL) + enomem_panic(); error = base64_decode(encoded, result, true, alloc_size, &result_len); if (error) @@ -232,7 +226,6 @@ base64_read(char *content, unsigned char **out, size_t *out_len) return 0; release_result: free(result); -release_bio: BIO_free(encoded); release_sanitized: free(sanitized); @@ -258,10 +251,8 @@ parse_string(xmlTextReaderPtr reader, char const *attr, char **result) } tmp = malloc(xmlStrlen(xml_value) + 1); - if (tmp == NULL) { - xmlFree(xml_value); - return pr_enomem(); - } + if (tmp == NULL) + enomem_panic(); memcpy(tmp, xml_value, xmlStrlen(xml_value)); tmp[xmlStrlen(xml_value)] = '\0'; @@ -323,10 +314,8 @@ parse_hex_string(xmlTextReaderPtr reader, bool required, char const *attr, tmp_len = xmlStrlen(xml_value) / 2; tmp = malloc(tmp_len); - if (tmp == NULL) { - xmlFree(xml_value); - return pr_enomem(); - } + if (tmp == NULL) + enomem_panic(); memset(tmp, 0, tmp_len); ptr = tmp; @@ -471,9 +460,7 @@ parse_publish(xmlTextReaderPtr reader, bool parse_hash, bool hash_required, char *base64_str; int error; - error = publish_create(&tmp); - if (error) - return error; + tmp = publish_create(); error = parse_doc_data(reader, parse_hash, hash_required, &tmp->doc_data); @@ -532,9 +519,7 @@ parse_withdraw(xmlTextReaderPtr reader, struct withdraw **withdraw) struct rpki_uri *uri; int error; - error = withdraw_create(&tmp); - if (error) - return error; + tmp = withdraw_create(); error = parse_doc_data(reader, true, true, &tmp->doc_data); if (error) @@ -595,12 +580,7 @@ write_from_uri(char const *location, unsigned char *content, size_t content_len, uri_get_local(uri)); } - error = add_mft_to_list(visited_uris, uri_get_global(uri)); - if (error) { - uri_refput(uri); - file_close(out); - return error; - } + add_mft_to_list(visited_uris, uri_get_global(uri)); uri_refput(uri); file_close(out); @@ -702,11 +682,8 @@ parse_notification_delta(xmlTextReaderPtr reader, if (error) return error; - error = deltas_head_add(&update->deltas_list, &delta); - if (error) - doc_data_cleanup(&delta.doc_data); - - return error; + deltas_head_add(&update->deltas_list, &delta); + return 0; } static int @@ -749,7 +726,7 @@ parse_notification(struct rpki_uri *uri, struct update_notification **file) result = update_notification_create(uri_get_global(uri)); if (result == NULL) - return pr_enomem(); + enomem_panic(); error = relax_ng_parse(uri_get_local(uri), xml_read_notification, result); @@ -799,7 +776,6 @@ static int parse_snapshot(struct rpki_uri *uri, struct proc_upd_args *args) { struct rdr_snapshot_ctx ctx; - struct snapshot *snapshot; int error; fnstack_push_uri(uri); @@ -809,19 +785,14 @@ parse_snapshot(struct rpki_uri *uri, struct proc_upd_args *args) if (error) goto pop; - error = snapshot_create(&snapshot); - if (error) - goto pop; - - ctx.snapshot = snapshot; + ctx.snapshot = snapshot_create(); ctx.parent = args->parent; ctx.visited_uris = args->visited_uris; + error = relax_ng_parse(uri_get_local(uri), xml_read_snapshot, &ctx); - /* Error 0 is ok */ - snapshot_destroy(snapshot); -pop: - fnstack_pop(); + snapshot_destroy(ctx.snapshot); +pop: fnstack_pop(); return error; } @@ -865,7 +836,6 @@ parse_delta(struct rpki_uri *uri, struct delta_head *parents_data, struct proc_upd_args *args) { struct rdr_delta_ctx ctx; - struct delta *delta; struct doc_data *expected_data; int error; @@ -877,19 +847,14 @@ parse_delta(struct rpki_uri *uri, struct delta_head *parents_data, if (error) goto pop_fnstack; - error = delta_create(&delta); - if (error) - goto pop_fnstack; - - ctx.delta = delta; + ctx.delta = delta_create(); ctx.parent = args->parent; ctx.visited_uris = args->visited_uris; ctx.expected_serial = parents_data->serial; - error = relax_ng_parse(uri_get_local(uri), xml_read_delta, &ctx); - delta_destroy(delta); - /* Error 0 is ok */ + error = relax_ng_parse(uri_get_local(uri), xml_read_delta, &ctx); + delta_destroy(ctx.delta); pop_fnstack: fnstack_pop(); return error; diff --git a/src/rsync/rsync.c b/src/rsync/rsync.c index be460970..9c901f58 100644 --- a/src/rsync/rsync.c +++ b/src/rsync/rsync.c @@ -26,19 +26,17 @@ SLIST_HEAD(uri_list, uri); /* static char const *const RSYNC_PREFIX = "rsync://"; */ -int -rsync_create(struct uri_list **result) +struct uri_list * +rsync_create(void) { struct uri_list *visited_uris; visited_uris = malloc(sizeof(struct uri_list)); if (visited_uris == NULL) - return pr_enomem(); + enomem_panic(); SLIST_INIT(visited_uris); - - *result = visited_uris; - return 0; + return visited_uris; } void @@ -101,21 +99,19 @@ is_already_downloaded(struct rpki_uri *uri, struct uri_list *visited_uris) return false; } -static int +static void mark_as_downloaded(struct rpki_uri *uri, struct uri_list *visited_uris) { struct uri *node; node = malloc(sizeof(struct uri)); if (node == NULL) - return pr_enomem(); + enomem_panic(); node->uri = uri; uri_refget(uri); SLIST_INSERT_HEAD(visited_uris, node, next); - - return 0; } static int @@ -203,7 +199,7 @@ release_args(char **args, unsigned int size) free(args); } -static int +static void prepare_rsync(struct rpki_uri *uri, bool is_ta, char ***args, size_t *args_len) { struct string_array const *config_args; @@ -218,7 +214,7 @@ prepare_rsync(struct rpki_uri *uri, bool is_ta, char ***args, size_t *args_len) */ copy_args = calloc(config_args->length + 2, sizeof(char *)); if (copy_args == NULL) - return pr_enomem(); + enomem_panic(); copy_args[0] = config_get_rsync_program(); copy_args[config_args->length + 1] = NULL; @@ -234,15 +230,12 @@ prepare_rsync(struct rpki_uri *uri, bool is_ta, char ***args, size_t *args_len) else copy_args[i + 1] = strdup(config_args->array[i]); - if (copy_args[i + 1] == NULL) { - release_args(copy_args, i); - return pr_enomem(); - } + if (copy_args[i + 1] == NULL) + enomem_panic(); } *args = copy_args; *args_len = config_args->length; - return 0; } __dead static void @@ -288,7 +281,7 @@ create_pipes(int fds[2][2]) return 0; } -static int +static void log_buffer(char const *buffer, ssize_t read, int type, bool log_operation) { #define PRE_RSYNC "[RSYNC exec]: " @@ -296,7 +289,7 @@ log_buffer(char const *buffer, ssize_t read, int type, bool log_operation) cpy = malloc(read + 1); if (cpy == NULL) - return pr_enomem(); + enomem_panic(); strncpy(cpy, buffer, read); cpy[read] = '\0'; @@ -319,7 +312,6 @@ log_buffer(char const *buffer, ssize_t read, int type, bool log_operation) cur = tmp + 1; } free(cpy); - return 0; #undef PRE_RSYNC } @@ -344,10 +336,9 @@ read_pipe(int fd_pipe[2][2], int type, bool log_operation) if (count == 0) break; - error = log_buffer(buffer, count, type, log_operation); - if (error) - return error; + log_buffer(buffer, count, type, log_operation); } + close(fd_pipe[type][0]); /* Close read end */ return 0; } @@ -396,9 +387,7 @@ do_rsync(struct rpki_uri *uri, bool is_ta, bool log_operation) /* Prepare everything for the child exec */ args = NULL; args_len = 0; - error = prepare_rsync(uri, is_ta, &args, &args_len); - if (error) - return error; + prepare_rsync(uri, is_ta, &args, &args_len); pr_val_info("rsync: %s", uri_get_global(uri)); if (log_val_enabled(LOG_DEBUG)) { @@ -628,7 +617,7 @@ rsync_download_files(struct rpki_uri *requested_uri, bool is_ta, bool force) case 0: /* Don't store when "force" and if its already downloaded */ if (!(force && is_already_downloaded(rsync_uri, visited_uris))) - error = mark_as_downloaded(rsync_uri, visited_uris); + mark_as_downloaded(rsync_uri, visited_uris); reqs_errors_rem_uri(uri_get_global(rsync_uri)); break; case EREQFAILED: @@ -636,10 +625,8 @@ rsync_download_files(struct rpki_uri *requested_uri, bool is_ta, bool force) error = reqs_errors_add_uri(uri_get_global(rsync_uri)); if (error) break; - error = mark_as_downloaded(rsync_uri, visited_uris); - /* Everything went ok? Return the original error */ - if (!error) - error = EREQFAILED; + mark_as_downloaded(rsync_uri, visited_uris); + error = EREQFAILED; /* Return the original error */ break; } diff --git a/src/rsync/rsync.h b/src/rsync/rsync.h index b5f8e6ed..bb715bd3 100644 --- a/src/rsync/rsync.h +++ b/src/rsync/rsync.h @@ -7,7 +7,7 @@ struct uri_list; int rsync_download_files(struct rpki_uri *, bool, bool); -int rsync_create(struct uri_list **); +struct uri_list *rsync_create(void); void rsync_destroy(struct uri_list *); void reset_downloaded(void); diff --git a/src/rtr/db/db_table.c b/src/rtr/db/db_table.c index 2f6cbe3a..cbfea75b 100644 --- a/src/rtr/db/db_table.c +++ b/src/rtr/db/db_table.c @@ -4,7 +4,7 @@ #include /* AF_INET, AF_INET6 (needed in OpenBSD) */ #include "log.h" -#include "data_structure/uthash_nonfatal.h" +#include "data_structure/uthash.h" struct hashable_roa { const struct vrp data; @@ -96,7 +96,7 @@ add_roa(struct db_table *table, struct hashable_roa const *stack_new) new = malloc(sizeof(struct hashable_roa)); if (new == NULL) - return pr_enomem(); + enomem_panic(); memcpy(new, stack_new, sizeof(*new)); errno = 0; @@ -209,7 +209,7 @@ rtrhandler_handle_router_key(struct db_table *table, key = malloc(sizeof(struct hashable_key)); if (key == NULL) - return pr_enomem(); + enomem_panic(); /* Needed by uthash */ memset(key, 0, sizeof(struct hashable_key)); @@ -327,9 +327,7 @@ compute_deltas(struct db_table *old, struct db_table *new, struct deltas *deltas; int error; - error = deltas_create(&deltas); - if (error) - return error; + deltas = deltas_create(); error = add_roa_deltas(new->roas, old->roas, deltas, FLAG_ANNOUNCEMENT, 'n'); diff --git a/src/rtr/db/delta.c b/src/rtr/db/delta.c index ec412468..d4f1d178 100644 --- a/src/rtr/db/delta.c +++ b/src/rtr/db/delta.c @@ -45,14 +45,14 @@ struct deltas { atomic_uint references; }; -int -deltas_create(struct deltas **_result) +struct deltas * +deltas_create(void) { struct deltas *result; result = malloc(sizeof(struct deltas)); if (result == NULL) - return pr_enomem(); + enomem_panic(); deltas_v4_init(&result->v4.adds); deltas_v4_init(&result->v4.removes); @@ -62,8 +62,7 @@ deltas_create(struct deltas **_result) deltas_rk_init(&result->rk.removes); atomic_init(&result->references, 1); - *_result = result; - return 0; + return result; } void @@ -132,13 +131,15 @@ deltas_add_roa(struct deltas *deltas, struct vrp const *vrp, int op, delta.v4.prefix.addr = vrp->prefix.v4; delta.v4.prefix.len = vrp->prefix_length; delta.v4.max_length = vrp->max_prefix_length; - return deltas_v4_add(get_deltas_array4(deltas, op), &delta.v4); + deltas_v4_add(get_deltas_array4(deltas, op), &delta.v4); + return 0; case AF_INET6: delta.v6.as = vrp->asn; delta.v6.prefix.addr = vrp->prefix.v6; delta.v6.prefix.len = vrp->prefix_length; delta.v6.max_length = vrp->max_prefix_length; - return deltas_v6_add(get_deltas_array6(deltas, op), &delta.v6); + deltas_v6_add(get_deltas_array6(deltas, op), &delta.v6); + return 0; } pr_crit("Unknown protocol: [%u %s/%u-%u %u] %c %u/%u " @@ -165,9 +166,11 @@ deltas_add_router_key(struct deltas *deltas, struct router_key const *key, switch (op) { case FLAG_ANNOUNCEMENT: - return deltas_rk_add(&deltas->rk.adds, &delta); + deltas_rk_add(&deltas->rk.adds, &delta); + return 0; case FLAG_WITHDRAWAL: - return deltas_rk_add(&deltas->rk.removes, &delta); + deltas_rk_add(&deltas->rk.removes, &delta); + return 0; } pr_crit("Unknown delta operation: %d", op); diff --git a/src/rtr/db/delta.h b/src/rtr/db/delta.h index a2f07fa7..20fb7198 100644 --- a/src/rtr/db/delta.h +++ b/src/rtr/db/delta.h @@ -5,7 +5,7 @@ struct deltas; -int deltas_create(struct deltas **); +struct deltas *deltas_create(void); void deltas_refget(struct deltas *); void deltas_refput(struct deltas *); diff --git a/src/rtr/db/deltas_array.c b/src/rtr/db/deltas_array.c index 63bb1dd6..cae242f8 100644 --- a/src/rtr/db/deltas_array.c +++ b/src/rtr/db/deltas_array.c @@ -3,6 +3,7 @@ #include #include #include "config.h" +#include "log.h" struct deltas_array { struct deltas **array; /* It's a circular array. */ @@ -17,14 +18,12 @@ darray_create(void) result = malloc(sizeof(struct deltas_array)); if (result == NULL) - return NULL; + enomem_panic(); result->array = calloc(config_get_deltas_lifetime(), sizeof(struct deltas *)); - if (result->array == NULL) { - free(result); - return NULL; - } + if (result->array == NULL) + enomem_panic(); result->len = 0; result->last = UINT_MAX; diff --git a/src/rtr/db/vrps.c b/src/rtr/db/vrps.c index eeda9df4..43d7482f 100644 --- a/src/rtr/db/vrps.c +++ b/src/rtr/db/vrps.c @@ -116,10 +116,6 @@ vrps_init(void) state.base = NULL; state.deltas = darray_create(); - if (state.deltas == NULL) { - error = pr_enomem(); - goto revert_thread_pool; - } /* * Use the same start serial, the session ID will avoid @@ -153,7 +149,6 @@ vrps_init(void) revert_deltas: darray_destroy(state.deltas); -revert_thread_pool: thread_pool_destroy(pool); return error; } @@ -209,7 +204,7 @@ __perform_standalone_validation(struct db_table **result) db = db_table_create(); if (db == NULL) - return pr_enomem(); + enomem_panic(); error = perform_standalone_validation(pool, db); if (error) { @@ -437,7 +432,7 @@ vrp_ovrd_remove(struct delta_vrp const *delta, void *arg) ptr = malloc(sizeof(struct vrp_node)); if (ptr == NULL) - return pr_enomem(); + enomem_panic(); ptr->delta = *delta; SLIST_INSERT_HEAD(filtered_vrps, ptr, next); @@ -468,7 +463,7 @@ router_key_ovrd_remove(struct delta_router_key const *delta, void *arg) ptr = malloc(sizeof(struct rk_node)); if (ptr == NULL) - return pr_enomem(); + enomem_panic(); ptr->delta = *delta; SLIST_INSERT_HEAD(filtered_keys, ptr, next); diff --git a/src/rtr/pdu.c b/src/rtr/pdu.c index add22f56..eb303efb 100644 --- a/src/rtr/pdu.c +++ b/src/rtr/pdu.c @@ -167,7 +167,7 @@ pdu_load(struct pdu_reader *reader, struct rtr_client *client, request->pdu = malloc(meta->length); if (request->pdu == NULL) /* No error report PDU on allocation failures. */ - return pr_enomem(); + enomem_panic(); error = meta->from_stream(&header, reader, request->pdu); if (error) { diff --git a/src/rtr/pdu_handler.c b/src/rtr/pdu_handler.c index d4110a5b..f822b557 100644 --- a/src/rtr/pdu_handler.c +++ b/src/rtr/pdu_handler.c @@ -127,7 +127,7 @@ handle_serial_query_pdu(int fd, struct rtr_request const *request) /* https://tools.ietf.org/html/rfc6810#section-6.3 */ return send_cache_reset_pdu(fd, args.rtr_version); case -ENOMEM: /* Memory allocation failure */ - return pr_enomem(); + enomem_panic(); case EAGAIN: /* Too many threads */ /* * I think this should be more of a "try again" thing, but diff --git a/src/rtr/pdu_sender.c b/src/rtr/pdu_sender.c index 6b0f5f38..17ea92da 100644 --- a/src/rtr/pdu_sender.c +++ b/src/rtr/pdu_sender.c @@ -294,7 +294,7 @@ send_error_report_pdu(int fd, uint8_t version, uint16_t code, data = malloc(pdu.header.length); if (data == NULL) - return pr_enomem(); + enomem_panic(); len = serialize_error_report_pdu(&pdu, data); if (len != pdu.header.length) diff --git a/src/rtr/primitive_reader.c b/src/rtr/primitive_reader.c index 2979b5ea..116811d9 100644 --- a/src/rtr/primitive_reader.c +++ b/src/rtr/primitive_reader.c @@ -191,7 +191,7 @@ read_string(struct pdu_reader *reader, uint32_t string_len, rtr_char **result) string = malloc(string_len + 1); /* Include NULL chara. */ if (string == NULL) - return pr_enomem(); + enomem_panic(); memcpy(string, reader->buffer, string_len); reader->buffer += string_len; diff --git a/src/rtr/rtr.c b/src/rtr/rtr.c index 8532a3d2..5c0b42bb 100644 --- a/src/rtr/rtr.c +++ b/src/rtr/rtr.c @@ -86,7 +86,7 @@ parse_address(char const *full_address, char **address, char **service) tmp_addr = NULL; tmp_serv = strdup(config_get_server_port()); if (tmp_serv == NULL) - return pr_enomem(); + enomem_panic(); goto done; } @@ -94,13 +94,11 @@ parse_address(char const *full_address, char **address, char **service) if (ptr == NULL) { tmp_addr = strdup(full_address); if (tmp_addr == NULL) - return pr_enomem(); + enomem_panic(); tmp_serv = strdup(config_get_server_port()); - if (tmp_serv == NULL) { - free(tmp_addr); - return pr_enomem(); - } + if (tmp_serv == NULL) + enomem_panic(); goto done; } @@ -112,16 +110,14 @@ parse_address(char const *full_address, char **address, char **service) tmp_addr_len = strlen(full_address) - strlen(ptr); tmp_addr = malloc(tmp_addr_len + 1); if (tmp_addr == NULL) - return pr_enomem(); + enomem_panic(); memcpy(tmp_addr, full_address, tmp_addr_len); tmp_addr[tmp_addr_len] = '\0'; tmp_serv = strdup(ptr + 1); - if (tmp_serv == NULL) { - free(tmp_addr); - return pr_enomem(); - } + if (tmp_serv == NULL) + enomem_panic(); /* Fall through */ done: @@ -303,11 +299,7 @@ create_server_socket(char const *input_addr, char const *hostname, server.fd = fd; /* Ignore failure; this is just a nice-to-have. */ server.addr = (input_addr != NULL) ? strdup(input_addr) : NULL; - error = server_arraylist_add(&servers, &server); - if (error) { - close(fd); - return error; - } + server_arraylist_add(&servers, &server); return 0; /* Happy path */ } @@ -463,10 +455,7 @@ accept_new_client(struct pollfd const *server_fd) client.rtr_version = -1; sockaddr2str(&client_addr, client.addr); - if (client_arraylist_add(&clients, &client) != 0) { - close(client.fd); - return AV_CLIENT_ERROR; - } + client_arraylist_add(&clients, &client); pr_op_info("Client accepted [FD: %d]: %s", client.fd, client.addr); return AV_SUCCESS; @@ -519,29 +508,21 @@ static bool __handle_client_request(struct rtr_client *client) { struct client_request *request; - int error; request = malloc(sizeof(struct client_request)); - if (request == NULL) { - pr_enomem(); - return false; - } + if (request == NULL) + enomem_panic(); request->client = client; - if (!read_until_block(client->fd, request)) - goto cancel; + if (!read_until_block(client->fd, request)) { + free(request); + return false; + } pr_op_debug("Client sent %zu bytes.", request->nread); - error = thread_pool_push(request_handlers, "RTR request", - handle_client_request, request); - if (error) - goto cancel; - + thread_pool_push(request_handlers, "RTR request", handle_client_request, + request); return true; - -cancel: - free(request); - return false; } static void @@ -623,7 +604,7 @@ fddb_poll(void) pollfds = calloc(servers.len + clients.len, sizeof(struct pollfd)); if (pollfds == NULL) { - pr_enomem(); + enomem_panic(); return PV_RETRY; } @@ -647,7 +628,7 @@ fddb_poll(void) pr_op_info("poll() was interrupted by some signal."); goto stop; case ENOMEM: - pr_enomem(); + enomem_panic(); /* Fall through */ case EAGAIN: goto retry; diff --git a/src/slurm/db_slurm.c b/src/slurm/db_slurm.c index 393f09b3..a945e8d3 100644 --- a/src/slurm/db_slurm.c +++ b/src/slurm/db_slurm.c @@ -58,22 +58,21 @@ slurm_bgpsec_wrap_refput(struct slurm_bgpsec_wrap *elem) } } -static int -slurm_lists_create(struct slurm_lists **result) +static struct slurm_lists * +slurm_lists_create(void) { struct slurm_lists *cache; cache = malloc(sizeof(struct slurm_lists)); if (cache == NULL) - return pr_enomem(); + enomem_panic(); al_filter_prefix_init(&cache->filter_pfx_al); al_assertion_prefix_init(&cache->assertion_pfx_al); al_filter_bgpsec_init(&cache->filter_bgps_al); al_assertion_bgpsec_init(&cache->assertion_bgps_al); - *result = cache; - return 0; + return cache; } static void @@ -103,7 +102,7 @@ db_slurm_create(struct slurm_csum_list *csums, struct db_slurm **result) db = malloc(sizeof(struct db_slurm)); if (db == NULL) - return pr_enomem(); + enomem_panic(); error = get_current_time(&db->loaded_date); if (error) { @@ -301,8 +300,9 @@ db_slurm_add_prefix_filter(struct db_slurm *db, struct slurm_prefix *elem) new.element = *elem; new.references = 1; + al_filter_prefix_add(&db->cache->filter_pfx_al, &new); - return al_filter_prefix_add(&db->cache->filter_pfx_al, &new); + return 0; } int @@ -315,8 +315,9 @@ db_slurm_add_prefix_assertion(struct db_slurm *db, struct slurm_prefix *elem) new.element = *elem; new.references = 1; + al_assertion_prefix_add(&db->cache->assertion_pfx_al, &new); - return al_assertion_prefix_add(&db->cache->assertion_pfx_al, &new); + return 0; } static bool @@ -363,8 +364,9 @@ db_slurm_add_bgpsec_filter(struct db_slurm *db, struct slurm_bgpsec *elem) new.element = *elem; new.references = 1; + al_filter_bgpsec_add(&db->cache->filter_bgps_al, &new); - return al_filter_bgpsec_add(&db->cache->filter_bgps_al, &new); + return 0; } int @@ -377,8 +379,9 @@ db_slurm_add_bgpsec_assertion(struct db_slurm *db, struct slurm_bgpsec *elem) new.element = *elem; new.references = 1; + al_assertion_bgpsec_add(&db->cache->assertion_bgps_al, &new); - return al_assertion_bgpsec_add(&db->cache->assertion_bgps_al, &new); + return 0; } bool @@ -534,116 +537,67 @@ db_slurm_log(struct db_slurm *db) pr_op_info("}"); } -int +void db_slurm_start_cache(struct db_slurm *db) { - struct slurm_lists *cache; - int error; - - cache = NULL; - error = slurm_lists_create(&cache); - if (error) - return error; - - db->cache = cache; - - return 0; + db->cache = slurm_lists_create(); } -static int +static void persist_filter_prefix(struct db_slurm *db) { struct slurm_prefix_wrap *cursor; array_index i; - int error; - - ARRAYLIST_FOREACH(&db->cache->filter_pfx_al, cursor, i) { - error = al_filter_prefix_add(&db->lists.filter_pfx_al, cursor); - if (error) - return error; - } - return 0; + ARRAYLIST_FOREACH(&db->cache->filter_pfx_al, cursor, i) + al_filter_prefix_add(&db->lists.filter_pfx_al, cursor); } -static int +static void persist_filter_bgpsec(struct db_slurm *db) { struct slurm_bgpsec_wrap *cursor; array_index i; - int error; ARRAYLIST_FOREACH(&db->cache->filter_bgps_al, cursor, i) { - error = al_filter_bgpsec_add(&db->lists.filter_bgps_al, cursor); - if (error) - return error; + al_filter_bgpsec_add(&db->lists.filter_bgps_al, cursor); slurm_bgpsec_wrap_refget(cursor); } - - return 0; } -static int +static void persist_assertion_prefix(struct db_slurm *db) { struct slurm_prefix_wrap *cursor; array_index i; - int error; - ARRAYLIST_FOREACH(&db->cache->assertion_pfx_al, cursor, i) { - error = al_assertion_prefix_add(&db->lists.assertion_pfx_al, - cursor); - if (error) - return error; - } - - return 0; + ARRAYLIST_FOREACH(&db->cache->assertion_pfx_al, cursor, i) + al_assertion_prefix_add(&db->lists.assertion_pfx_al, cursor); } -static int +static void persist_assertion_bgpsec(struct db_slurm *db) { struct slurm_bgpsec_wrap *cursor; array_index i; - int error; ARRAYLIST_FOREACH(&db->cache->assertion_bgps_al, cursor, i) { - error = al_assertion_bgpsec_add(&db->lists.assertion_bgps_al, - cursor); - if (error) - return error; + al_assertion_bgpsec_add(&db->lists.assertion_bgps_al, cursor); slurm_bgpsec_wrap_refget(cursor); } - - return 0; } -int +/* Copy all data in cache to the main lists */ +void db_slurm_flush_cache(struct db_slurm *db) { - /* Copy all data in cache to the main lists */ - int error; - - error = persist_filter_prefix(db); - if (error) - return error; - - error = persist_filter_bgpsec(db); - if (error) - return error; - - error = persist_assertion_prefix(db); - if (error) - return error; - - error = persist_assertion_bgpsec(db); - if (error) - return error; + persist_filter_prefix(db); + persist_filter_bgpsec(db); + persist_assertion_prefix(db); + persist_assertion_bgpsec(db); slurm_lists_destroy(db->cache); db->cache = NULL; - - return 0; } bool diff --git a/src/slurm/db_slurm.h b/src/slurm/db_slurm.h index 9e693eeb..7eed5495 100644 --- a/src/slurm/db_slurm.h +++ b/src/slurm/db_slurm.h @@ -68,9 +68,9 @@ int db_slurm_foreach_assertion_bgpsec(struct db_slurm *, bgpsec_foreach_cb, void db_slurm_log(struct db_slurm *); /* Start working on the cache */ -int db_slurm_start_cache(struct db_slurm *); +void db_slurm_start_cache(struct db_slurm *); /* Persist all the data stored at cache and erase cache */ -int db_slurm_flush_cache(struct db_slurm *); +void db_slurm_flush_cache(struct db_slurm *); /* Does the SLURM DB has data? */ bool db_slurm_has_data(struct db_slurm *); diff --git a/src/slurm/slurm_loader.c b/src/slurm/slurm_loader.c index 17799da8..525bac33 100644 --- a/src/slurm/slurm_loader.c +++ b/src/slurm/slurm_loader.c @@ -135,7 +135,7 @@ __slurm_load_checksums(char const *location, void *arg) csum = malloc(sizeof(struct slurm_file_csum)); if (csum == NULL) - return pr_enomem(); + enomem_panic(); error = hash_local_file("sha256", location, csum->csum, &csum->csum_len); diff --git a/src/slurm/slurm_parser.c b/src/slurm/slurm_parser.c index 5dc7bb9b..476a2697 100644 --- a/src/slurm/slurm_parser.c +++ b/src/slurm/slurm_parser.c @@ -133,7 +133,7 @@ set_prefix(json_t *object, bool is_assertion, struct slurm_prefix *result, clone = strdup(str_prefix); if (clone == NULL) - return pr_enomem(); + enomem_panic(); token = strtok(clone, "/"); isv4 = strchr(token, ':') == NULL; diff --git a/src/sorted_array.c b/src/sorted_array.c index 2abbcd75..6e535f66 100644 --- a/src/sorted_array.c +++ b/src/sorted_array.c @@ -114,7 +114,7 @@ sarray_add(struct sorted_array *sarray, void *element) if (sarray->count >= sarray->len) { tmp = realloc(sarray->array, 2 * sarray->len * sarray->size); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); sarray->array = tmp; sarray->len *= 2; } diff --git a/src/state.c b/src/state.c index 6a962b6a..7a027904 100644 --- a/src/state.c +++ b/src/state.c @@ -96,7 +96,7 @@ validation_prepare(struct validation **out, struct tal *tal, result = malloc(sizeof(struct validation)); if (!result) - return pr_enomem(); + enomem_panic(); error = state_store(result); if (error) @@ -111,10 +111,8 @@ validation_prepare(struct validation **out, struct tal *tal, } params = X509_VERIFY_PARAM_new(); - if (params == NULL) { - error = pr_enomem(); - goto abort2; - } + if (params == NULL) + enomem_panic(); X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_CRL_CHECK); X509_STORE_set1_param(result->x509_data.store, params); @@ -124,24 +122,17 @@ validation_prepare(struct validation **out, struct tal *tal, if (error) goto abort3; - error = rsync_create(&result->rsync_visited_uris); - if (error) - goto abort4; - + result->rsync_visited_uris = rsync_create(); result->rrdp_uris = db_rrdp_get_uris(tal_get_file_name(tal)); result->rrdp_workspace = db_rrdp_get_workspace(tal_get_file_name(tal)); - result->pubkey_state = PKS_UNTESTED; result->validation_handler = *validation_handler; result->x509_data.params = params; /* Ownership transfered */ *out = result; return 0; -abort4: - certstack_destroy(result->certstack); abort3: X509_VERIFY_PARAM_free(params); -abort2: X509_STORE_free(result->x509_data.store); abort1: free(result); diff --git a/src/str_token.c b/src/str_token.c index 4fac55d8..750dca51 100644 --- a/src/str_token.c +++ b/src/str_token.c @@ -7,28 +7,29 @@ /** * Does not assume that @string is NULL-terminated. */ -static int -string_clone(void const *string, size_t size, char **clone) +static char * +string_clone(void const *string, size_t size) { char *result; result = malloc(size + 1); if (result == NULL) - return pr_enomem(); + enomem_panic(); memcpy(result, string, size); result[size] = '\0'; - *clone = result; - return 0; + return result; } int ia5s2string(ASN1_IA5STRING *ia5, char **result) { - return (ia5->flags & ASN1_STRING_FLAG_BITS_LEFT) - ? pr_val_err("CRL URI IA5String has unused bits.") - : string_clone(ia5->data, ia5->length, result); + if (ia5->flags & ASN1_STRING_FLAG_BITS_LEFT) + return pr_val_err("CRL URI IA5String has unused bits."); + + *result = string_clone(ia5->data, ia5->length); + return 0; } int @@ -43,7 +44,7 @@ BN2string(BIGNUM *bn, char **_result) bio = BIO_new(BIO_s_mem()); if (bio == NULL) - return pr_enomem(); + enomem_panic(); if (BN_print(bio, bn) == 0) { BIO_free(bio); @@ -52,10 +53,8 @@ BN2string(BIGNUM *bn, char **_result) written = BIO_number_written(bio); result = malloc(written + 1); - if (result == NULL) { - BIO_free(bio); - return pr_enomem(); - } + if (result == NULL) + enomem_panic(); BIO_read(bio, result, written); result[written] = '\0'; @@ -120,11 +119,11 @@ token_equals(struct string_tokenizer *t1, struct string_tokenizer *t2) : false; } -int -token_read(struct string_tokenizer *tokenizer, char **token) +char * +token_read(struct string_tokenizer *tokenizer) { return string_clone(tokenizer->str + tokenizer->start, - tokenizer->end - tokenizer->start, token); + tokenizer->end - tokenizer->start); } size_t diff --git a/src/str_token.h b/src/str_token.h index 12da0fe4..a74d272a 100644 --- a/src/str_token.h +++ b/src/str_token.h @@ -33,7 +33,7 @@ void string_tokenizer_init(struct string_tokenizer *, char const *, size_t, unsigned char); bool string_tokenizer_next(struct string_tokenizer *); bool token_equals(struct string_tokenizer *, struct string_tokenizer *); -int token_read(struct string_tokenizer *, char **); +char *token_read(struct string_tokenizer *); size_t token_count(struct string_tokenizer *); #endif /* SRC_STR_TOKEN_H_ */ diff --git a/src/thread/thread_pool.c b/src/thread/thread_pool.c index 1fc3f976..b9cb1b2f 100644 --- a/src/thread/thread_pool.c +++ b/src/thread/thread_pool.c @@ -110,22 +110,20 @@ signal_to_worker(struct thread_pool *pool) "pthread_cond_signal"); } -static int -task_create(char const *name, thread_pool_task_cb cb, void *arg, - struct thread_pool_task **out) +static struct thread_pool_task * +task_create(char const *name, thread_pool_task_cb cb, void *arg) { struct thread_pool_task *task; task = malloc(sizeof(struct thread_pool_task)); if (task == NULL) - return pr_enomem(); + enomem_panic(); task->name = name; task->cb = cb; task->arg = arg; - *out = task; - return 0; + return task; } static void @@ -301,7 +299,7 @@ thread_pool_create(char const *name, unsigned int threads, result = malloc(sizeof(struct thread_pool)); if (result == NULL) - return pr_enomem(); + enomem_panic(); /* Init locking */ error = pthread_mutex_init(&result->lock, NULL); @@ -333,10 +331,8 @@ thread_pool_create(char const *name, unsigned int threads, result->working_count = 0; result->thread_count = 0; result->thread_ids = calloc(threads, sizeof(pthread_t)); - if (result->thread_ids == NULL) { - error = pr_enomem(); - goto free_waiting_cond; - } + if (result->thread_ids == NULL) + enomem_panic(); result->thread_ids_len = threads; error = spawn_threads(result); @@ -348,7 +344,6 @@ thread_pool_create(char const *name, unsigned int threads, free_thread_ids: free(result->thread_ids); -free_waiting_cond: pthread_cond_destroy(&result->worker2parent); free_working_cond: pthread_cond_destroy(&result->parent2worker); @@ -396,17 +391,13 @@ thread_pool_destroy(struct thread_pool *pool) * Push a new task to @pool, the task to be executed is @cb with the argument * @arg. */ -int +void thread_pool_push(struct thread_pool *pool, char const *task_name, thread_pool_task_cb cb, void *arg) { struct thread_pool_task *task; - int error; - task = NULL; - error = task_create(task_name, cb, arg, &task); - if (error) - return error; + task = task_create(task_name, cb, arg); mutex_lock(&pool->lock); task_queue_push(pool, task); @@ -417,7 +408,6 @@ thread_pool_push(struct thread_pool *pool, char const *task_name, * If not, they will claim work once they spawn anyway. */ signal_to_worker(pool); - return 0; } /* There are available threads to work? */ diff --git a/src/thread/thread_pool.h b/src/thread/thread_pool.h index 921cfcf7..5391af18 100644 --- a/src/thread/thread_pool.h +++ b/src/thread/thread_pool.h @@ -14,7 +14,7 @@ int thread_pool_create(char const *, unsigned int, struct thread_pool **); void thread_pool_destroy(struct thread_pool *); typedef void (*thread_pool_task_cb)(void *); -int thread_pool_push(struct thread_pool *, char const *, thread_pool_task_cb, +void thread_pool_push(struct thread_pool *, char const *, thread_pool_task_cb, void *); bool thread_pool_avail_threads(struct thread_pool *); diff --git a/src/thread_var.c b/src/thread_var.c index 4d0200e1..1dbb7422 100644 --- a/src/thread_var.c +++ b/src/thread_var.c @@ -115,13 +115,11 @@ fnstack_init(void) files = malloc(sizeof(struct filename_stack)); if (files == NULL) - return; + enomem_panic(); files->filenames = malloc(32 * sizeof(char *)); - if (files->filenames == NULL) { - free(files); - return; - } + if (files->filenames == NULL) + enomem_panic(); files->len = 0; files->size = 32; @@ -186,13 +184,8 @@ fnstack_push(char const *file) if (files->len >= files->size) { tmp = realloc(files->filenames, 2 * files->size * sizeof(char *)); - if (tmp == NULL) { - /* Oh noes */ - free(files->filenames); - files->filenames = NULL; - return; - } - + if (tmp == NULL) + enomem_panic(); files->filenames = tmp; files->size *= 2; } @@ -247,7 +240,7 @@ working_repo_init(void) repo = malloc(sizeof(struct working_repo)); if (repo == NULL) - return; + enomem_panic(); repo->uri = NULL; repo->level = 0; diff --git a/src/types/uri.c b/src/types/uri.c index 04b1cbe0..a24c5243 100644 --- a/src/types/uri.c +++ b/src/types/uri.c @@ -126,7 +126,7 @@ str2global(char const *str, size_t str_len, struct rpki_uri *uri) uri->global = malloc(str_len + 1); if (uri->global == NULL) - return pr_enomem(); + enomem_panic(); strncpy(uri->global, str, str_len); uri->global[str_len] = '\0'; uri->global_len = str_len; @@ -205,7 +205,7 @@ ia5str2global(struct rpki_uri *uri, char const *mft, IA5String_t *ia5) dir_len = (slash_pos + 1) - mft; joined = malloc(dir_len + ia5->size + 1); if (joined == NULL) - return pr_enomem(); + enomem_panic(); strncpy(joined, mft, dir_len); strncpy(joined + dir_len, (char *) ia5->buf, ia5->size); @@ -277,24 +277,21 @@ validate_gprefix(char const *global, size_t global_len, uint8_t flags, return 0; } -static int -get_local_workspace(char **result) +static char * +get_local_workspace(void) { char const *workspace; char *tmp; workspace = db_rrdp_uris_workspace_get(); - if (workspace == NULL) { - *result = NULL; - return 0; - } + if (workspace == NULL) + return NULL; tmp = strdup(workspace); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); - *result = tmp; - return 0; + return tmp; } /** @@ -320,21 +317,13 @@ g2l(char const *global, size_t global_len, uint8_t flags, char **result, if (error) return error; - workspace = NULL; - if ((flags & URI_USE_RRDP_WORKSPACE) != 0) { - error = get_local_workspace(&workspace); - if (error) - return error; - } + workspace = ((flags & URI_USE_RRDP_WORKSPACE) != 0) + ? get_local_workspace() + : NULL; - error = map_uri_to_local(global, + local = map_uri_to_local(global, type == URI_RSYNC ? PFX_RSYNC : PFX_HTTPS, - workspace, - &local); - if (error) { - free(workspace); - return error; - } + workspace); free(workspace); *result = local; @@ -358,7 +347,7 @@ uri_create(struct rpki_uri **result, uint8_t flags, void const *guri, uri = malloc(sizeof(struct rpki_uri)); if (uri == NULL) - return pr_enomem(); + enomem_panic(); error = str2global(guri, guri_len, uri); if (error) { @@ -426,7 +415,7 @@ uri_create_mft(struct rpki_uri **result, struct rpki_uri *mft, IA5String_t *ia5, uri = malloc(sizeof(struct rpki_uri)); if (uri == NULL) - return pr_enomem(); + enomem_panic(); error = ia5str2global(uri, mft->global, ia5); if (error) { diff --git a/src/visited_uris.c b/src/visited_uris.c index af5c0e1a..ee609359 100644 --- a/src/visited_uris.c +++ b/src/visited_uris.c @@ -22,25 +22,24 @@ struct visited_uris { DEFINE_ARRAY_LIST_STRUCT(uris_roots, char *); DEFINE_ARRAY_LIST_FUNCTIONS(uris_roots, char *, static) -static int -visited_elem_create(struct visited_elem **elem, char const *uri) +static struct visited_elem * +visited_elem_create(char const *uri) { struct visited_elem *tmp; tmp = malloc(sizeof(struct visited_elem)); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); /* Needed by uthash */ memset(tmp, 0, sizeof(struct visited_elem)); tmp->uri = strdup(uri); if (tmp->uri == NULL) { free(tmp); - return pr_enomem(); + enomem_panic(); } - *elem = tmp; - return 0; + return tmp; } static void @@ -50,20 +49,19 @@ visited_elem_destroy(struct visited_elem *elem) free(elem); } -int -visited_uris_create(struct visited_uris **uris) +struct visited_uris * +visited_uris_create(void) { struct visited_uris *tmp; tmp = malloc(sizeof(struct visited_uris)); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); tmp->table = NULL; tmp->refs = 1; - *uris = tmp; - return 0; + return tmp; } static void @@ -100,22 +98,11 @@ elem_find(struct visited_uris *list, char const *uri) return found; } -int +void visited_uris_add(struct visited_uris *uris, char const *uri) { - struct visited_elem *elem; - int error; - - if (elem_find(uris, uri) != NULL) - return 0; /* Exists, don't add again */ - - elem = NULL; - error = visited_elem_create(&elem, uri); - if (error) - return error; - - HASH_ADD_STR(uris->table, uri, elem); - return 0; + if (elem_find(uris, uri) == NULL) + HASH_ADD_STR(uris->table, uri, visited_elem_create(uri)); } int @@ -133,7 +120,7 @@ visited_uris_remove(struct visited_uris *uris, char const *uri) return 0; } -static int +static void visited_uris_to_arr(struct visited_uris *uris, struct uris_roots *roots) { struct visited_elem *elem; @@ -145,13 +132,11 @@ visited_uris_to_arr(struct visited_uris *uris, struct uris_roots *roots) size = last_slash - elem->uri; tmp = malloc(size + 1); if (tmp == NULL) - return pr_enomem(); + enomem_panic(); strncpy(tmp, elem->uri, size); tmp[size] = '\0'; uris_roots_add(roots, &tmp); } - - return 0; } static void @@ -170,21 +155,17 @@ visited_uris_delete_local(struct visited_uris *uris, char const *workspace) int error; uris_roots_init(&roots); - - error = visited_uris_to_arr(uris, &roots); - if (error) - goto err; - + visited_uris_to_arr(uris, &roots); if (roots.len == 0) goto success; error = delete_dir_daemon_start(roots.array, roots.len, workspace); - if (error) - goto err; + if (error) { + uris_roots_cleanup(&roots, uris_root_destroy); + return error; + } + success: uris_roots_cleanup(&roots, uris_root_destroy); return 0; -err: - uris_roots_cleanup(&roots, uris_root_destroy); - return error; } diff --git a/src/visited_uris.h b/src/visited_uris.h index 9557482b..44ff27a4 100644 --- a/src/visited_uris.h +++ b/src/visited_uris.h @@ -3,11 +3,11 @@ struct visited_uris; -int visited_uris_create(struct visited_uris **); +struct visited_uris *visited_uris_create(void); void visited_uris_refput(struct visited_uris *); void visited_uris_refget(struct visited_uris *); -int visited_uris_add(struct visited_uris *, char const *); +void visited_uris_add(struct visited_uris *, char const *); int visited_uris_remove(struct visited_uris *, char const *); int visited_uris_delete_local(struct visited_uris *, char const *); diff --git a/test/rrdp_objects_test.c b/test/rrdp_objects_test.c index d6d41a5b..294f964b 100644 --- a/test/rrdp_objects_test.c +++ b/test/rrdp_objects_test.c @@ -18,7 +18,7 @@ add_serials(struct deltas_head *deltas, ...) va_start(vl, deltas); while ((delta.serial = va_arg(vl, unsigned long)) != END) - ck_assert_int_eq(0, deltas_head_add(deltas, &delta)); + deltas_head_add(deltas, &delta); va_end(vl); } diff --git a/test/rsync_test.c b/test/rsync_test.c index 7b99a961..3bcdcf55 100644 --- a/test/rsync_test.c +++ b/test/rsync_test.c @@ -58,7 +58,7 @@ __mark_as_downloaded(char *uri_str, struct uri_list *visited_uris) { struct rpki_uri *uri; ck_assert_int_eq(0, uri_create_rsync_str(&uri, uri_str, strlen(uri_str))); - ck_assert_int_eq(mark_as_downloaded(uri, visited_uris), 0); + mark_as_downloaded(uri, visited_uris); uri_refput(uri); } @@ -75,8 +75,8 @@ START_TEST(rsync_test_list) { struct uri_list *visited_uris; - visited_uris = NULL; - ck_assert_int_eq(rsync_create(&visited_uris), 0); + visited_uris = rsync_create(); + ck_assert_ptr_nonnull(visited_uris); __mark_as_downloaded("rsync://example.foo/repository/", visited_uris); __mark_as_downloaded("rsync://example.foo/member_repository/", diff --git a/test/rtr/db/deltas_array_test.c b/test/rtr/db/deltas_array_test.c index 2ad2316f..fbfd7a75 100644 --- a/test/rtr/db/deltas_array_test.c +++ b/test/rtr/db/deltas_array_test.c @@ -56,8 +56,10 @@ START_TEST(add_only) darray = darray_create(); ck_assert_ptr_ne(NULL, darray); - for (i = 0; i < TOTAL_CREATED; i++) - ck_assert_int_eq(0, deltas_create(&created[i])); + for (i = 0; i < TOTAL_CREATED; i++) { + created[i] = deltas_create(); + ck_assert_ptr_nonnull(created[i]); + } test_foreach(darray, 0, 0); diff --git a/test/rtr/db/vrps_test.c b/test/rtr/db/vrps_test.c index 51a610e0..db6a81f6 100644 --- a/test/rtr/db/vrps_test.c +++ b/test/rtr/db/vrps_test.c @@ -289,7 +289,8 @@ check_deltas(serial_t from, serial_t to, bool const *expected_deltas) bool actual_deltas[12]; array_index i; - ck_assert_int_eq(0, deltas_create(&deltas)); + deltas = deltas_create(); + ck_assert_ptr_nonnull(deltas); ck_assert_int_eq(0, vrps_foreach_delta_since(from, &actual_serial, vrp_add, rk_add, deltas)); diff --git a/test/tal_test.c b/test/tal_test.c index ac5a4e90..907cbc01 100644 --- a/test/tal_test.c +++ b/test/tal_test.c @@ -57,15 +57,17 @@ close_thread(pthread_t thread, char const *what) /* Nothing to close */ } -int -map_uri_to_local(char const *uri, char const *uri_prefix, char const *workspace, - char **result) +char * +map_uri_to_local(char const *uri, char const *uri_prefix, char const *workspace) { + char *result; + /* These tests focus on global URIs, so set a dummy value */ - *result = strdup("dummy"); - if (*result == NULL) - return -ENOMEM; - return 0; + result = strdup("dummy"); + if (result == NULL) + enomem_panic(); + + return result; } void @@ -188,7 +190,7 @@ START_TEST(tal_order_http_first) config_set_http_priority(60); config_set_rsync_priority(50); - ck_assert_int_eq(tal_order_uris(tal), 0); + tal_order_uris(tal); ck_assert_str_eq(tal->uris.array[0]->global, "https://potato"); ck_assert_str_eq(tal->uris.array[1]->global, @@ -207,7 +209,7 @@ START_TEST(tal_order_http_last) config_set_http_priority(50); config_set_rsync_priority(60); - ck_assert_int_eq(tal_order_uris(tal), 0); + tal_order_uris(tal); ck_assert_str_eq(tal->uris.array[0]->global, "rsync://repository.lacnic.net/rpki/lacnic/rta-lacnic-rpki.cer");