]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Wrap alloc functions, to remove boilerplate
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Fri, 23 Jun 2023 22:49:24 +0000 (16:49 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Fri, 23 Jun 2023 22:49:24 +0000 (16:49 -0600)
66 files changed:
src/Makefile.am
src/alloc.c [new file with mode: 0644]
src/alloc.h [new file with mode: 0644]
src/asn1/oid.c
src/asn1/signed_data.c
src/cert_stack.c
src/common.c
src/config.c
src/config/str.c
src/config/string_array.c
src/crypto/base64.c
src/crypto/hash.c
src/crypto/hash.h
src/delete_dir_daemon.c
src/file.c
src/http/http.c
src/init.c
src/json_handler.c
src/line_file.c
src/object/bgpsec.c
src/object/certificate.c
src/object/manifest.c
src/object/name.c
src/object/tal.c
src/object/vcard.c
src/reqs_errors.c
src/resource.c
src/rpp.c
src/rrdp/db/db_rrdp.c
src/rrdp/db/db_rrdp_uris.c
src/rrdp/rrdp_objects.c
src/rrdp/rrdp_parser.c
src/rsync/rsync.c
src/rtr/db/db_table.c
src/rtr/db/delta.c
src/rtr/db/deltas_array.c
src/rtr/db/vrps.c
src/rtr/err_pdu.c
src/rtr/pdu.c
src/rtr/pdu_sender.c
src/rtr/primitive_reader.c
src/rtr/rtr.c
src/slurm/db_slurm.c
src/slurm/slurm_loader.c
src/slurm/slurm_parser.c
src/sorted_array.c
src/state.c
src/str_token.c
src/thread/thread_pool.c
src/thread_var.c
src/types/uri.c
src/visited_uris.c
test/line_file_test.c
test/rrdp_objects_test.c
test/rsync_test.c
test/rtr/db/db_table_test.c
test/rtr/db/deltas_array_test.c
test/rtr/db/vrps_test.c
test/rtr/pdu_handler_test.c
test/rtr/pdu_test.c
test/rtr/primitive_reader_test.c
test/tal_test.c
test/thread_pool_test.c
test/types/address_test.c
test/types/uri_test.c
test/vcard_test.c

index ad6c8f78e1f38999a49f0470f845885324e256cb..98d5f3d16d6d4d708b1be8f89283c7f281c5c84f 100644 (file)
@@ -7,6 +7,7 @@ bin_PROGRAMS = fort
 fort_SOURCES  = main.c
 
 fort_SOURCES += algorithm.h algorithm.c
+fort_SOURCES += alloc.h alloc.c
 fort_SOURCES += certificate_refs.h certificate_refs.c
 fort_SOURCES += cert_stack.h cert_stack.c
 fort_SOURCES += common.c common.h
diff --git a/src/alloc.c b/src/alloc.c
new file mode 100644 (file)
index 0000000..fab2537
--- /dev/null
@@ -0,0 +1,76 @@
+#include "alloc.h"
+
+#include <string.h>
+#include "log.h"
+
+void *
+pmalloc(size_t size)
+{
+       void *result;
+
+       result = malloc(size);
+       if (result == NULL)
+               enomem_panic();
+
+       return result;
+}
+
+void *
+pzalloc(size_t size)
+{
+       void *result;
+
+       result = pmalloc(size);
+       memset(result, 0, size);
+
+       return result;
+}
+
+void *
+pcalloc(size_t nmemb, size_t size)
+{
+       void *result;
+
+       result = calloc(nmemb, size);
+       if (result == NULL)
+               enomem_panic();
+
+       return result;
+}
+
+void *
+prealloc(void *ptr, size_t size)
+{
+       void *result;
+
+       result = realloc(ptr, size);
+       if (result == NULL)
+               enomem_panic();
+
+       return result;
+}
+
+void *
+pmclone(void const *src, size_t size)
+{
+       void *result;
+
+       result = pmalloc(size);
+       if (result == NULL)
+               enomem_panic();
+       memcpy(result, src, size);
+
+       return result;
+}
+
+char *
+pstrdup(const char *s)
+{
+       char *result;
+
+       result = strdup(s);
+       if (result == NULL)
+               enomem_panic();
+
+       return result;
+}
diff --git a/src/alloc.h b/src/alloc.h
new file mode 100644 (file)
index 0000000..c53ab30
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef SRC_ALLOC_H_
+#define SRC_ALLOC_H_
+
+#include <stdlib.h>
+
+/* malloc(), but panic on allocation failure. */
+void *pmalloc(size_t size);
+/* malloc(), but panic on allocation failure, zeroize memory on success. */
+void *pzalloc(size_t size);
+/* calloc(), but panic on allocation failure. */
+void *pcalloc(size_t nmemb, size_t size);
+/* realloc(), but panic on allocation failure. */
+void *prealloc(void *ptr, size_t size);
+/* Clone @src on the heap, panic on allocation failure. */
+void *pmclone(void const *src, size_t size);
+
+/* strdup(), but panic on allocation failure. */
+char *pstrdup(char const *s);
+
+#endif /* SRC_ALLOC_H_ */
index 03cb85941c384254ca4cd8bc52d842480655a9b8..dc013a5c7951ed93517a7c513c6b7990f4d70fa4 100644 (file)
@@ -1,6 +1,7 @@
 #include "oid.h"
 
 #include <errno.h>
+#include "alloc.h"
 #include "common.h"
 #include "log.h"
 #include "asn1/decode.h"
@@ -25,11 +26,8 @@ oid2arcs(OBJECT_IDENTIFIER_t *oid, struct oid_arcs *result)
        static const size_t MAX_ARCS = 9;
        ssize_t count;
        ssize_t count2;
-       asn_oid_arc_t *tmp;
 
-       result->arcs = malloc(MAX_ARCS * sizeof(asn_oid_arc_t));
-       if (result->arcs == NULL)
-               enomem_panic();
+       result->arcs = pmalloc(MAX_ARCS * sizeof(asn_oid_arc_t));
 
        count = OBJECT_IDENTIFIER_get_arcs(oid, result->arcs, MAX_ARCS);
        if (count < 0) {
@@ -42,10 +40,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)
-                       enomem_panic();
-               result->arcs = tmp;
+               result->arcs = prealloc(result->arcs,
+                   count * sizeof(asn_oid_arc_t));
 
                count2 = OBJECT_IDENTIFIER_get_arcs(oid, result->arcs, count);
                if (count != count2) {
index 1f7dade35d91c0355c009731c2b84e02bd17d0a3..83b5934e16fb81a650a5ec102a1d3bcd51afbe74 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 
 #include "algorithm.h"
+#include "alloc.h"
 #include "config.h"
 #include "log.h"
 #include "oid.h"
@@ -414,9 +415,7 @@ signed_data_decode_pkcs7(ANY_t *coded, struct SignedData **result)
        if (error)
                return error;
 
-       sdata = calloc(1, sizeof(struct SignedData));
-       if (sdata == NULL)
-               enomem_panic();
+       sdata = pcalloc(1, sizeof(struct SignedData));
 
        /* Parse content as OCTET STRING */
        error = asn1_decode_any(sdata_pkcs7->encapContentInfo.eContent,
index 0d9246e1c68f5d4ec20d01903672ce6246f0f637..c04857a26b16b7fa0c7498ec2b1d7d23803bd58c 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <sys/queue.h>
 
+#include "alloc.h"
 #include "resource.h"
 #include "str_token.h"
 #include "thread_var.h"
@@ -103,9 +104,7 @@ certstack_create(struct cert_stack **result)
 {
        struct cert_stack *stack;
 
-       stack = malloc(sizeof(struct cert_stack));
-       if (stack == NULL)
-               enomem_panic();
+       stack = pmalloc(sizeof(struct cert_stack));
 
        stack->x509s = sk_X509_new_null();
        if (stack->x509s == NULL) {
@@ -187,9 +186,7 @@ deferstack_push(struct cert_stack *stack, struct deferred_cert *deferred)
 {
        struct defer_node *node;
 
-       node = malloc(sizeof(struct defer_node));
-       if (node == NULL)
-               enomem_panic();
+       node = pmalloc(sizeof(struct defer_node));
 
        node->type = DNT_CERT;
        node->deferred = *deferred;
@@ -314,11 +311,9 @@ create_separator(void)
 {
        struct defer_node *result;
 
-       result = malloc(sizeof(struct defer_node));
-       if (result == NULL)
-               enomem_panic();
-
+       result = pmalloc(sizeof(struct defer_node));
        result->type = DNT_SEPARATOR;
+
        return result;
 }
 
@@ -332,9 +327,7 @@ x509stack_push(struct cert_stack *stack, struct rpki_uri *uri, X509 *x509,
        int ok;
        int error;
 
-       meta = malloc(sizeof(struct metadata_node));
-       if (meta == NULL)
-               enomem_panic();
+       meta = pmalloc(sizeof(struct metadata_node));
 
        meta->uri = uri;
        uri_refget(uri);
@@ -426,17 +419,12 @@ static char *
 get_current_file_name(void)
 {
        char const *file_name;
-       char *result;
 
        file_name = fnstack_peek();
        if (file_name == NULL)
                pr_crit("The file name stack is empty.");
 
-       result = strdup(file_name);
-       if (result == NULL)
-               enomem_panic();
-
-       return result;
+       return pstrdup(file_name);
 }
 
 /**
index 3f2acefdd008f46e90b3dffc8745b629b09f47fd..cb457da14b437c1c5a932897de8a3b5a82fbf0ed 100644 (file)
@@ -2,13 +2,14 @@
 
 #include <dirent.h> /* readdir(), closedir() */
 #include <limits.h> /* realpath() */
-#include <stdlib.h> /* malloc(), free(), realloc(), realpath() */
+#include <stdlib.h> /* free(), realpath() */
 #include <stdio.h> /* remove() */
-#include <string.h> /* strdup(), strrchr(), strcmp(), strcat(), etc */
+#include <string.h> /* strrchr(), strcmp(), strcat(), etc */
 #include <unistd.h> /* stat(), rmdir() */
 #include <sys/stat.h> /* stat(), mkdir() */
 #include <sys/types.h> /* stat(), closedir(), mkdir() */
 
+#include "alloc.h"
 #include "config.h"
 #include "log.h"
 
@@ -104,13 +105,8 @@ process_file(char const *dir_name, char const *file_name, char const *file_ext,
        (*fcount)++; /* Increment the found count */
 
        /* Get the full file path */
-       tmp = strdup(dir_name);
-       if (tmp == NULL)
-               enomem_panic();
-
-       tmp = realloc(tmp, strlen(tmp) + 1 + strlen(file_name) + 1);
-       if (tmp == NULL)
-               enomem_panic();
+       tmp = pstrdup(dir_name);
+       tmp = prealloc(tmp, strlen(tmp) + 1 + strlen(file_name) + 1);
 
        strcat(tmp, "/");
        strcat(tmp, file_name);
@@ -297,9 +293,7 @@ create_dir_recursive(char const *path)
        if (exist)
                return 0;
 
-       localuri = strdup(path);
-       if (localuri == NULL)
-               enomem_panic();
+       localuri = pstrdup(path);
 
        for (i = 1; localuri[i] != '\0'; i++) {
                if (localuri[i] == '/') {
@@ -361,9 +355,7 @@ delete_dir_recursive_bottom_up(char const *path)
        if (error)
                return error;
 
-       config_repo = strdup(config_get_local_repository());
-       if (config_repo == NULL)
-               enomem_panic();
+       config_repo = pstrdup(config_get_local_repository());
 
        /* Stop dir removal when the work_dir has this length */
        config_len = strlen(config_repo);
@@ -371,9 +363,7 @@ delete_dir_recursive_bottom_up(char const *path)
                config_len--;
        free(config_repo);
 
-       work_loc = strdup(path);
-       if (work_loc == NULL)
-               enomem_panic();
+       work_loc = pstrdup(path);
 
        do {
                tmp = strrchr(work_loc, '/');
@@ -459,9 +449,7 @@ 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);
-       if (local == NULL)
-               enomem_panic();
+       local = pmalloc(repository_len + extra_slash + workspace_len + uri_len + 1);
 
        offset = 0;
        strcpy(local + offset, repository);
index 0766d6de6f908a3ac193542b68bf56d152d10060..743f3d62562db9641b43945ba4a4c3032a5b502f 100644 (file)
@@ -8,6 +8,7 @@
 #include <sys/socket.h>
 #include <syslog.h>
 
+#include "alloc.h"
 #include "common.h"
 #include "configure_ac.h"
 #include "daemon.h"
@@ -912,12 +913,8 @@ 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)
-               enomem_panic();
-       short_opts = malloc(total_short_options + 1);
-       if (short_opts == NULL)
-               enomem_panic();
+       long_opts = pcalloc(total_long_options + 1, sizeof(struct option));
+       short_opts = pmalloc(total_short_options + 1);
 
        *_long_opts = long_opts;
        *_short_opts = short_opts;
@@ -987,11 +984,7 @@ set_default_values(void)
         */
 
        string_array_init(&rpki_config.server.address, NULL, 0);
-
-       rpki_config.server.port = strdup("323");
-       if (rpki_config.server.port == NULL)
-               enomem_panic();
-
+       rpki_config.server.port = pstrdup("323");
        rpki_config.server.backlog = SOMAXCONN;
        rpki_config.server.interval.validation = 3600;
        rpki_config.server.interval.refresh = 3600;
@@ -1000,15 +993,11 @@ set_default_values(void)
        rpki_config.server.deltas_lifetime = 2;
 
        rpki_config.tal = NULL;
-       rpki_config.slurm = NULL;
-
-       rpki_config.local_repository = strdup("/tmp/fort/repository");
-       if (rpki_config.local_repository == NULL)
-               enomem_panic();
-
+       rpki_config.local_repository = pstrdup("/tmp/fort/repository");
        rpki_config.sync_strategy = RSYNC_ROOT_EXCEPT_TA;
        rpki_config.shuffle_tal_uris = false;
        rpki_config.maximum_certificate_depth = 32;
+       rpki_config.slurm = NULL;
        rpki_config.mode = SERVER;
        rpki_config.work_offline = false;
        rpki_config.daemon = false;
@@ -1018,23 +1007,18 @@ set_default_values(void)
        rpki_config.rsync.strategy = RSYNC_ROOT_EXCEPT_TA;
        rpki_config.rsync.retry.count = 1;
        rpki_config.rsync.retry.interval = 4;
-       rpki_config.rsync.program = strdup("rsync");
-       if (rpki_config.rsync.program == NULL)
-               enomem_panic();
-
-       string_array_init(&rpki_config.rsync.args.recursive,
-           recursive_rsync_args, ARRAY_LEN(recursive_rsync_args));
+       rpki_config.rsync.program = pstrdup("rsync");
        string_array_init(&rpki_config.rsync.args.flat,
            flat_rsync_args, ARRAY_LEN(flat_rsync_args));
+       string_array_init(&rpki_config.rsync.args.recursive,
+           recursive_rsync_args, ARRAY_LEN(recursive_rsync_args));
 
        /* By default, has a higher priority than rsync */
        rpki_config.http.enabled = true;
        rpki_config.http.priority = 60;
        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)
-               enomem_panic();
+       rpki_config.http.user_agent = pstrdup(PACKAGE_NAME "/" PACKAGE_VERSION);
        rpki_config.http.connect_timeout = 30;
        rpki_config.http.transfer_timeout = 0;
        rpki_config.http.low_speed_limit = 100000;
@@ -1051,28 +1035,21 @@ set_default_values(void)
        rpki_config.rrdp.retry.count = rpki_config.http.retry.count;
        rpki_config.rrdp.retry.interval = rpki_config.http.retry.interval;
 
+       rpki_config.log.enabled = true;
+       rpki_config.log.tag = NULL;
        rpki_config.log.color = false;
        rpki_config.log.filename_format = FNF_GLOBAL;
        rpki_config.log.level = LOG_WARNING;
        rpki_config.log.output = CONSOLE;
-
-       rpki_config.log.enabled = true;
-       rpki_config.log.output = CONSOLE;
-       rpki_config.log.level = LOG_WARNING;
-       rpki_config.log.color = false;
-       rpki_config.log.filename_format = FNF_GLOBAL;
        rpki_config.log.facility = LOG_DAEMON;
-       rpki_config.log.tag = NULL;
 
        rpki_config.validation_log.enabled = false;
-       rpki_config.validation_log.output = CONSOLE;
-       rpki_config.validation_log.level = LOG_WARNING;
+       rpki_config.validation_log.tag = pstrdup("Validation");
        rpki_config.validation_log.color = false;
        rpki_config.validation_log.filename_format = FNF_GLOBAL;
+       rpki_config.validation_log.level = LOG_WARNING;
+       rpki_config.validation_log.output = CONSOLE;
        rpki_config.validation_log.facility = LOG_DAEMON;
-       rpki_config.validation_log.tag = strdup("Validation");
-       if (rpki_config.validation_log.tag == NULL)
-               enomem_panic();
 
        rpki_config.output.roa = NULL;
        rpki_config.output.bgpsec = NULL;
@@ -1080,13 +1057,10 @@ set_default_values(void)
 
        rpki_config.asn1_decode_max_stack = 4096; /* 4kB */
        rpki_config.stale_repository_period = 43200; /* 12 hours */
-
        rpki_config.init_tals = false;
        rpki_config.init_tal_locations = 0;
 
-       /* Common scenario is to connect 1 router or a couple of them */
        rpki_config.thread_pool.server.max = 20;
-       /* Usually 5 TALs, let a few more available */
        rpki_config.thread_pool.validation.max = 5;
 }
 
index f1f5cbfa8a36d927f51e395f892d883116beaafb..99e4227ec175f80ff05f8c508969da5b24091702 100644 (file)
@@ -3,6 +3,8 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <string.h>
+
+#include "alloc.h"
 #include "log.h"
 
 #define DEREFERENCE(void_value) (*((char **) void_value))
@@ -33,10 +35,7 @@ string_parse_argv(struct option_field const *field, char const *str,
        /* Remove the previous value (usually the default). */
        __string_free(result);
 
-       DEREFERENCE(result) = strdup(str);
-       if (DEREFERENCE(result) == NULL)
-               enomem_panic();
-
+       DEREFERENCE(result) = pstrdup(str);
        return 0;
 }
 
index 49bd1fac34d5e6c86db192f110a03925dc90b3ec..f61484a9250c34ea160c641ce1e47fe922922da9 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "alloc.h"
 #include "log.h"
 #include "str_token.h"
 #include "config/str.h"
@@ -22,17 +23,9 @@ string_array_init(struct string_array *array, char const *const *values,
                return;
        }
 
-       array->array = calloc(len, sizeof(char *));
-       if (array->array == NULL)
-               enomem_panic();
-
-       for (i = 0; i < len; i++) {
-               array->array[i] = strdup(values[i]);
-               if (array->array[i] == NULL) {
-                       string_array_cleanup(array);
-                       enomem_panic();
-               }
-       }
+       array->array = pcalloc(len, sizeof(char *));
+       for (i = 0; i < len; i++)
+               array->array[i] = pstrdup(values[i]);
 }
 
 void
@@ -104,9 +97,7 @@ string_array_parse_json(struct option_field const *opt, json_t *json,
        /* Remove the previous value (usually the default). */
        __string_array_free(result);
 
-       result->array = calloc(len, sizeof(char *));
-       if (result->array == NULL)
-               enomem_panic();
+       result->array = pcalloc(len, sizeof(char *));
        result->length = len;
 
        for (i = 0; i < len; i++) {
@@ -115,9 +106,7 @@ string_array_parse_json(struct option_field const *opt, json_t *json,
                if (error)
                        goto fail;
 
-               result->array[i] = strdup(tmp);
-               if (result->array[i] == NULL)
-                       enomem_panic();
+               result->array[i] = pstrdup(tmp);
        }
 
        return 0;
@@ -147,9 +136,7 @@ string_array_parse_argv(struct option_field const *opt, char const *str,
                return pr_op_err("'%s' can have %u elements max; currently it has %lu elements.",
                    opt->name, opt->max, len);
 
-       result->array = calloc(len, sizeof(char *));
-       if (result->array == NULL)
-               enomem_panic();
+       result->array = pcalloc(len, sizeof(char *));
        result->length = len;
 
        for (i = 0; string_tokenizer_next(&tokenizer); i++)
index b2e50590b0320eef83e37928f1426615b2afbefb..083fea495e59499be37eed071207eace2e7f2c8e 100644 (file)
@@ -5,6 +5,7 @@
 #include <openssl/buffer.h>
 #include <errno.h>
 #include <string.h>
+#include "alloc.h"
 #include "log.h"
 
 /**
@@ -150,9 +151,7 @@ base64url_decode(char const *str_encoded, unsigned char **result,
        encoded_len = strlen(str_encoded);
        pad = (encoded_len % 4) > 0 ? 4 - (encoded_len % 4) : 0;
 
-       str_copy = malloc(encoded_len + pad + 1);
-       if (str_copy == NULL)
-               enomem_panic();
+       str_copy = pmalloc(encoded_len + pad + 1);
        /* Set all with pad char, then replace with the original string */
        memset(str_copy, '=', encoded_len + pad);
        memcpy(str_copy, str_encoded, encoded_len);
@@ -173,11 +172,7 @@ 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)
-               enomem_panic();
-       memset(*result, 0, alloc_size);
-       (*result)[alloc_size] = '\0';
+       *result = pzalloc(alloc_size + 1);
 
        error = base64_decode(encoded, *result, false, alloc_size, &dec_len);
        if (error)
@@ -216,10 +211,7 @@ to_base64url(char *base, size_t base_len)
                len = pad - base;
        } while(0);
 
-       tmp = malloc(len + 1);
-       if (tmp == NULL)
-               enomem_panic();
-
+       tmp = pmalloc(len + 1);
        memcpy(tmp, base, len);
        tmp[len] = '\0';
 
index fa3079ba639ed7beb7eadf6b072cf6ecb6631ded..0537b4bcbf4596f12a836c6b67bf84f5100e2c3b 100644 (file)
@@ -5,6 +5,7 @@
 #include <sys/stat.h>
 #include <sys/types.h> /* For blksize_t */
 
+#include "alloc.h"
 #include "common.h"
 #include "file.h"
 #include "log.h"
@@ -34,27 +35,24 @@ hash_matches(unsigned char const *expected, size_t expected_len,
 }
 
 static int
-hash_file(char const *algorithm, struct rpki_uri *uri, unsigned char *result,
-    unsigned int *result_len)
+hash_file(struct rpki_uri *uri, unsigned char *result, unsigned int *result_len)
 {
-       return hash_local_file(algorithm, uri_get_local(uri), result,
-               result_len);
+       return hash_local_file(uri_get_local(uri), result, result_len);
 }
 
 int
-hash_local_file(char const *algorithm, char const *uri, unsigned char *result,
+hash_local_file(char const *uri, unsigned char *result,
     unsigned int *result_len)
 {
        EVP_MD const *md;
        FILE *file;
        struct stat stat;
        unsigned char *buffer;
-       blksize_t buffer_len;
        size_t consumed;
        EVP_MD_CTX *ctx;
        int error;
 
-       error = get_md(algorithm, &md);
+       error = get_md("sha256", &md);
        if (error)
                return error;
 
@@ -62,10 +60,7 @@ hash_local_file(char const *algorithm, char const *uri, unsigned char *result,
        if (error)
                return error;
 
-       buffer_len = stat.st_blksize;
-       buffer = malloc(buffer_len);
-       if (buffer == NULL)
-               enomem_panic();
+       buffer = pmalloc(stat.st_blksize);
 
        ctx = EVP_MD_CTX_new();
        if (ctx == NULL)
@@ -77,7 +72,7 @@ hash_local_file(char const *algorithm, char const *uri, unsigned char *result,
        }
 
        do {
-               consumed = fread(buffer, 1, buffer_len, file);
+               consumed = fread(buffer, 1, stat.st_blksize, file);
                error = ferror(file);
                if (error) {
                        pr_val_err("File reading error. Error message (apparently): %s",
@@ -114,8 +109,7 @@ end:
  *     an incidence to ignore this).
  */
 int
-hash_validate_mft_file(char const *algorithm, struct rpki_uri *uri,
-    BIT_STRING_t const *expected)
+hash_validate_mft_file(struct rpki_uri *uri, BIT_STRING_t const *expected)
 {
        unsigned char actual[EVP_MAX_MD_SIZE];
        unsigned int actual_len;
@@ -125,7 +119,7 @@ hash_validate_mft_file(char const *algorithm, struct rpki_uri *uri,
                return pr_val_err("Hash string has unused bits.");
 
        do {
-               error = hash_file(algorithm, uri, actual, &actual_len);
+               error = hash_file(uri, actual, &actual_len);
                if (!error)
                        break;
 
@@ -155,14 +149,14 @@ hash_validate_mft_file(char const *algorithm, struct rpki_uri *uri,
  * @expected_len. Returns 0 if no errors happened and the hashes match.
  */
 int
-hash_validate_file(char const *algorithm, struct rpki_uri *uri,
-    unsigned char const *expected, size_t expected_len)
+hash_validate_file(struct rpki_uri *uri, unsigned char const *expected,
+    size_t expected_len)
 {
        unsigned char actual[EVP_MAX_MD_SIZE];
        unsigned int actual_len;
        int error;
 
-       error = hash_file(algorithm, uri, actual, &actual_len);
+       error = hash_file(uri, actual, &actual_len);
        if (error)
                return error;
 
@@ -241,17 +235,6 @@ int
 hash_str(char const *algorithm, char const *str, unsigned char *result,
     unsigned int *result_len)
 {
-       unsigned char *src;
-       int error;
-
-       src = malloc(strlen(str));
-       if (src == NULL)
-               enomem_panic();
-
-       memcpy(src, str, strlen(str));
-
-       error = hash_buffer(algorithm, src, strlen(str), result, result_len);
-
-       free(src);
-       return error;
+       return hash_buffer(algorithm, (unsigned char const *) str, strlen(str),
+           result, result_len);
 }
index 31ae305dd56cf957c66dace1d6cd85505c84a8a2..744b2431e1b21cec06ff0f8a6a681d8cf6474047 100644 (file)
@@ -6,17 +6,14 @@
 #include "types/uri.h"
 #include "asn1/asn1c/BIT_STRING.h"
 
-int hash_validate_mft_file(char const *, struct rpki_uri *uri,
-    BIT_STRING_t const *);
-int hash_validate_file(char const *, struct rpki_uri *, unsigned char const *,
-    size_t);
+int hash_validate_mft_file(struct rpki_uri *uri, BIT_STRING_t const *);
+int hash_validate_file(struct rpki_uri *, unsigned char const *, size_t);
 int hash_validate(char const *, unsigned char const *, size_t,
     unsigned char const *, size_t);
 int hash_validate_octet_string(char const *, OCTET_STRING_t const*,
     OCTET_STRING_t const *);
 
-int hash_local_file(char const *, char const *, unsigned char *,
-    unsigned int *);
+int hash_local_file(char const *, unsigned char *, unsigned int *);
 
 int hash_str(char const *, char const *, unsigned char *, unsigned int *);
 
index f1579385c573db46411adcbb04f7a6d87f574523..eb11d7128ad073d7fc0ce6ade91caa0ed16745f8 100644 (file)
@@ -8,6 +8,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include "alloc.h"
 #include "common.h"
 #include "internal_pool.h"
 #include "log.h"
@@ -166,9 +167,7 @@ get_local_path(char const *rcvd, char const *workspace, char **result)
        if (strrchr(local_path, '/') == local_path + strlen(local_path) - 1)
                tmp_size--;
 
-       tmp = malloc(tmp_size + 1);
-       if (tmp == NULL)
-               enomem_panic();
+       tmp = pmalloc(tmp_size + 1);
        strncpy(tmp, local_path, tmp_size);
        tmp[tmp_size] = '\0';
 
@@ -192,9 +191,7 @@ rename_local_path(char const *rcvd, char **result)
        rcvd_size = strlen(rcvd);
        /* original size + one underscore + hex random val (8 chars) */
        tmp_size = rcvd_size + 1 + (sizeof(RAND_MAX) * 2);
-       tmp = malloc(tmp_size + 1);
-       if (tmp == NULL)
-               enomem_panic();
+       tmp = pmalloc(tmp_size + 1);
 
        /* Rename the path with a random suffix */
        random_init();
@@ -246,14 +243,9 @@ rem_dirs_create(size_t arr_len)
 {
        struct rem_dirs *tmp;
 
-       tmp = malloc(sizeof(struct rem_dirs));
-       if (tmp == NULL)
-               enomem_panic();
-
-       tmp->arr = calloc(arr_len, sizeof(char *));
-       if (tmp->arr == NULL)
-               enomem_panic();
+       tmp = pmalloc(sizeof(struct rem_dirs));
 
+       tmp->arr = pcalloc(arr_len, sizeof(char *));
        tmp->arr_len = arr_len;
        tmp->arr_set = 0;
 
index 3a36d6e77993d2c316948cdd79120feb18c2d52f..8548bc912bb18d3d7688379b3a59ca146f06b4fd 100644 (file)
@@ -2,6 +2,8 @@
 
 #include <errno.h>
 #include <stdlib.h>
+
+#include "alloc.h"
 #include "log.h"
 
 static int
@@ -70,9 +72,7 @@ file_load(char const *file_name, struct file_contents *fc)
                return error;
 
        fc->buffer_size = stat.st_size;
-       fc->buffer = malloc(fc->buffer_size);
-       if (fc->buffer == NULL)
-               enomem_panic();
+       fc->buffer = pmalloc(fc->buffer_size);
 
        fread_result = fread(fc->buffer, 1, fc->buffer_size, file);
        if (fread_result < fc->buffer_size) {
index 48a003cfd2ab4cf60e2b2de9ff2081d22dc9d0b6..34298df306724b0ffecf6ac5757a747f4c736fbc 100644 (file)
@@ -4,6 +4,8 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <curl/curl.h>
+
+#include "alloc.h"
 #include "common.h"
 #include "config.h"
 #include "file.h"
@@ -362,9 +364,7 @@ __http_download_file(struct rpki_uri *uri, long *response_code, long ims_value,
 
        original_file = uri_get_local(uri);
 
-       tmp_file = malloc(strlen(original_file) + strlen(tmp_suffix) + 1);
-       if (tmp_file == NULL)
-               enomem_panic();
+       tmp_file = pmalloc(strlen(original_file) + strlen(tmp_suffix) + 1);
        strcpy(tmp_file, original_file);
        strcat(tmp_file, tmp_suffix);
 
@@ -516,18 +516,11 @@ http_direct_download(char const *remote, char const *dest)
        FILE *out;
        long response_code;
        long cond_met;
-       char *tmp_file, *tmp;
+       char *tmp_file;
        int error;
 
-       tmp_file = strdup(dest);
-       if (tmp_file == NULL)
-               enomem_panic();
-
-       tmp = realloc(tmp_file, strlen(tmp_file) + strlen(tmp_suffix) + 1);
-       if (tmp == NULL)
-               enomem_panic();
-
-       tmp_file = tmp;
+       tmp_file = pstrdup(dest);
+       tmp_file = prealloc(tmp_file, strlen(tmp_file) + strlen(tmp_suffix) + 1);
        strcat(tmp_file, tmp_suffix);
 
        error = file_write(tmp_file, &out);
index 021ed1fc3595a1c5d504075afc8627cce36080e2..ff88a7ea9a2f3b6f3b48a062e438aa8aea1e512b 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdbool.h>
 #include <unistd.h>
 
+#include "alloc.h"
 #include "log.h"
 #include "http/http.h"
 
@@ -37,9 +38,7 @@ fetch_url(char const *url)
 
        extra_slash = (dest_dir[dest_dir_len - 1] == '/') ? 0 : 1;
 
-       dest = malloc(dest_dir_len + extra_slash + strlen(dest_file) + 1);
-       if (dest == NULL)
-               enomem_panic();
+       dest = pmalloc(dest_dir_len + extra_slash + strlen(dest_file) + 1);
 
        offset = 0;
        strcpy(dest + offset, dest_dir);
index b6454e5f0cae91c9b3885778a39b133c92ab7455..5643dd251867bbcf3a7783bd986441cd7bf6a8f3 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 #include <string.h>
 
+#include "alloc.h"
 #include "config.h"
 #include "log.h"
 #include "config/types.h"
@@ -18,9 +19,7 @@ find_json(struct json_t *root, char const *full_name)
        struct json_t *node;
 
        /* strtok_r() needs a non-const string */
-       strtok.opt_name = strdup(full_name);
-       if (strtok.opt_name == NULL)
-               enomem_panic();
+       strtok.opt_name = pstrdup(full_name);
 
        node = root;
        strtok.token = strtok_r(strtok.opt_name, ".", &strtok.saveptr);
index fe29b1689f03ac75cc38ae12ccfa4fc36a5af772..0ce983ceb8553daa539aa497f9a7f69e7a27f38f 100644 (file)
@@ -4,6 +4,7 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include "alloc.h"
 #include "file.h"
 #include "log.h"
 
@@ -22,9 +23,7 @@ lfile_open(const char *file_name, struct line_file **result)
        struct line_file *lfile;
        int error;
 
-       lfile = malloc(sizeof(struct line_file));
-       if (lfile == NULL)
-               enomem_panic();
+       lfile = pmalloc(sizeof(struct line_file));
 
        lfile->file = fopen(file_name, "r");
        if (lfile->file == NULL) {
index e6274140517a38e2de47e6994f02fdca7eca0424..638b3bb3f90235c607ba55b94eef986284d31fe4 100644 (file)
@@ -1,5 +1,6 @@
 #include "bgpsec.h"
 
+#include "alloc.h"
 #include "log.h"
 #include "validation_handler.h"
 
@@ -34,9 +35,7 @@ handle_bgpsec(X509 *cert, unsigned char const *ski, struct resources *resources)
        if (pub_key == NULL)
                return val_crypto_err("X509_get_X509_PUBKEY() returned NULL at BGPsec");
 
-       cert_spk = malloc(RK_SPKI_LEN);
-       if (cert_spk == NULL)
-               enomem_panic();
+       cert_spk = pmalloc(RK_SPKI_LEN);
 
        /* Use a temporal pointer, since i2d_X509_PUBKEY moves it */
        tmp = cert_spk;
index 56c3d30d6ebac7421d04340196042b6f347cf661..a75ac7fbbfc4c2587c7b96aff6826b1946d2279c 100644 (file)
@@ -14,6 +14,7 @@
 #include <sys/socket.h>
 
 #include "algorithm.h"
+#include "alloc.h"
 #include "config.h"
 #include "extension.h"
 #include "log.h"
@@ -2259,10 +2260,7 @@ get_rsync_server_uri(struct rpki_uri *src, char **result, size_t *result_len)
                }
        }
 
-       tmp = malloc(i + 1);
-       if (tmp == NULL)
-               enomem_panic();
-
+       tmp = pmalloc(i + 1);
        strncpy(tmp, global, i);
        tmp[i] = '\0';
 
index 56da892c24863a6eb4d09fc5956ff55690f438c5..ecf5d16620eedc236da779b971949abfb4d7c900 100644 (file)
@@ -189,7 +189,7 @@ build_rpp(struct Manifest *mft, struct rpki_uri *mft_uri, bool rrdp_workspace,
                 * - Positive value: file doesn't exist and keep validating
                 *   manifest.
                 */
-               error = hash_validate_mft_file("sha256", uri, &fah->hash);
+               error = hash_validate_mft_file(uri, &fah->hash);
                if (error < 0) {
                        uri_refput(uri);
                        goto fail;
index 7c6cb5302929cadd1ca5cc3519e3f2b65c64a39e..90609a3643cf8b403198b83c8a92e1ed9c05c152 100644 (file)
@@ -5,6 +5,7 @@
 #include <string.h>
 #include <syslog.h>
 
+#include "alloc.h"
 #include "log.h"
 #include "thread_var.h"
 
@@ -30,10 +31,7 @@ name2string(X509_NAME_ENTRY *name, char **_result)
        if (data == NULL)
                return val_crypto_err("X509_NAME_ENTRY_get_data() returned NULL");
 
-       result = malloc(data->length + 1);
-       if (result == NULL)
-               enomem_panic();
-
+       result = pmalloc(data->length + 1);
        memcpy(result, data->data, data->length);
        result[data->length] = '\0';
 
@@ -51,9 +49,7 @@ x509_name_decode(X509_NAME *name, char const *what,
        int nid;
        int error;
 
-       result = malloc(sizeof(struct rfc5280_name));
-       if (result == NULL)
-               enomem_panic();
+       result = pmalloc(sizeof(struct rfc5280_name));
 
        result->commonName = NULL;
        result->serialNumber = NULL;
index a2997ab71704f074e20b05e748e99b06907f832e..d7aa7f060dca69e09695aac921d817680bed7279 100644 (file)
@@ -12,6 +12,7 @@
 #include <sys/stat.h>
 #include <openssl/evp.h>
 
+#include "alloc.h"
 #include "cert_stack.h"
 #include "common.h"
 #include "config.h"
@@ -79,9 +80,7 @@ uris_init(struct uris *uris)
        uris->rsync_count = 0;
        uris->https_count = 0;
        uris->size = 4; /* Most TALs only define one. */
-       uris->array = malloc(uris->size * sizeof(struct rpki_uri *));
-       if (uris->array == NULL)
-               enomem_panic();
+       uris->array = pmalloc(uris->size * sizeof(struct rpki_uri *));
 }
 
 static void
@@ -96,7 +95,6 @@ uris_destroy(struct uris *uris)
 static int
 uris_add(struct uris *uris, char *uri)
 {
-       struct rpki_uri **tmp;
        struct rpki_uri *new;
        int error;
 
@@ -113,11 +111,8 @@ uris_add(struct uris *uris, char *uri)
 
        if (uris->count + 1 >= uris->size) {
                uris->size *= 2;
-               tmp = realloc(uris->array,
+               uris->array = realloc(uris->array,
                    uris->size * sizeof(struct rpki_uri *));
-               if (tmp == NULL)
-                       enomem_panic();
-               uris->array = tmp;
        }
 
        uris->array[uris->count++] = new;
@@ -232,13 +227,8 @@ base64_sanitize(struct line_file *lfile, char **out)
         */
        original_size = get_spki_orig_size(lfile);
        new_size = original_size + (original_size / BUF_SIZE);
-       result = malloc(new_size + 1);
-       if (result == NULL)
-               enomem_panic();
-
-       buf = malloc(BUF_SIZE);
-       if (buf == NULL)
-               enomem_panic();
+       result = pmalloc(new_size + 1);
+       buf = pmalloc(BUF_SIZE);
 
        fd = lfile_fd(lfile);
        offset = 0;
@@ -280,12 +270,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)
-                       enomem_panic();
-               result = eol;
-       }
+       if (offset != new_size)
+               result = prealloc(result, offset + 1);
        free(buf);
        result[offset] = '\0';
 
@@ -307,9 +293,7 @@ read_spki(struct line_file *lfile, struct tal *tal)
        int error;
 
        alloc_size = get_spki_alloc_size(lfile);
-       tal->spki = malloc(alloc_size);
-       if (tal->spki == NULL)
-               enomem_panic();
+       tal->spki = pmalloc(alloc_size);
 
        tmp = NULL;
        error = base64_sanitize(lfile, &tmp);
@@ -352,18 +336,13 @@ tal_load(char const *file_name, struct tal **result)
                return error;
        }
 
-       tal = malloc(sizeof(struct tal));
-       if (tal == NULL)
-               enomem_panic();
+       tal = pmalloc(sizeof(struct tal));
 
        tal->file_name = file_name;
-
        uris_init(&tal->uris);
-
        error = read_uris(lfile, &tal->uris);
        if (error)
                goto fail;
-
        error = read_spki(lfile, tal);
        if (error)
                goto fail;
@@ -444,9 +423,7 @@ tal_order_uris(struct tal *tal)
        /* 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)
-               enomem_panic();
+       ordered = pmalloc(tal->uris.size * sizeof(struct rpki_uri *));
 
        last_rsync = (http_first ? tal->uris.https_count : 0);
        last_https = (http_first ? 0 : tal->uris.rsync_count);
@@ -709,13 +686,9 @@ __do_file_validation(char const *tal_file, void *arg)
        if (error)
                return error;
 
-       thread = malloc(sizeof(struct validation_thread));
-       if (thread == NULL)
-               enomem_panic();
+       thread = pmalloc(sizeof(struct validation_thread));
 
-       thread->tal_file = strdup(tal_file);
-       if (thread->tal_file == NULL)
-               enomem_panic();
+       thread->tal_file = pstrdup(tal_file);
        thread->arg = t_param->db;
        thread->exit_status = -EINTR;
        thread->retry_local = true;
index bbaab38d44f090c9b7aef27ae3ce623509ecfd6d..9d08edb72c1d703bed17ef9b4019f87824c7fa17 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 #include <stdbool.h>
 
+#include "alloc.h"
 #include "log.h"
 
 /*
@@ -96,13 +97,8 @@ analyze_pos(struct utf8_string *string, size_t pos)
 static void
 double_line_size(struct vcard_line *line)
 {
-       uint8_t *tmp;
-
        line->str.size *= 2;
-       tmp = realloc(line->str.val, line->str.size);
-       if (tmp == NULL)
-               enomem_panic();
-       line->str.val = tmp;
+       line->str.val = prealloc(line->str.val, line->str.size);
 }
 
 static void
@@ -261,9 +257,7 @@ handle_ghostbusters_vcard(OCTET_STRING_t *vcard)
 
        line.str.size = 81; /* Okay default, assuming there is no folding. */
        line.str.len = 0;
-       line.str.val = malloc(line.str.size);
-       if (line.str.val == NULL)
-               enomem_panic();
+       line.str.val = pmalloc(line.str.size);
        line.octet_string_offset = 0;
 
        error = __handle_ghostbusters_vcard(vcard, &line);
index 31b721c8e0ac9a71410076fa81d3c6e243c5854c..64e50a8f6c75cfe3f10448b32b587ace00a15ada 100644 (file)
@@ -5,6 +5,7 @@
 #include <time.h>
 
 #include "data_structure/uthash.h"
+#include "alloc.h"
 #include "common.h"
 #include "config.h"
 #include "log.h"
@@ -51,21 +52,12 @@ error_uri_create(char const *uri, struct error_uri **err_uri)
        struct error_uri *tmp;
        int error;
 
-       tmp = malloc(sizeof(struct error_uri));
-       if (tmp == NULL)
-               enomem_panic();
-
-       /* Needed by uthash */
-       memset(tmp, 0, sizeof(struct error_uri));
-
-       tmp->uri = strdup(uri);
-       if (tmp->uri == NULL)
-               enomem_panic();
+       tmp = pzalloc(sizeof(struct error_uri)); /* Zero needed by uthash */
 
+       tmp->uri = pstrdup(uri);
        error = get_current_time(&tmp->first_attempt);
        if (error)
                goto release_uri;
-
        tmp->log_summary = false;
        tmp->uri_related = NULL;
        tmp->ref_by = NULL;
index d5fee05cb3289f1fe54152d2629711f122feab99..3a881ed4caad92c26a459dad50bc65bc7fd87f52 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 #include <stdint.h> /* UINT32_MAX */
 
+#include "alloc.h"
 #include "log.h"
 #include "sorted_array.h"
 #include "thread_var.h"
@@ -34,9 +35,7 @@ resources_create(bool force_inherit)
 {
        struct resources *result;
 
-       result = malloc(sizeof(struct resources));
-       if (result == NULL)
-               return NULL;
+       result = pmalloc(sizeof(struct resources));
 
        result->ip4s = NULL;
        result->ip6s = NULL;
index 35d317c6b2527a8155160a2d96084ccc18368dbf..e3c83e6fa73a8158eefca472d49b281c9d128a6b 100644 (file)
--- a/src/rpp.c
+++ b/src/rpp.c
@@ -1,6 +1,6 @@
 #include "rpp.h"
 
-#include <stdlib.h>
+#include "alloc.h"
 #include "cert_stack.h"
 #include "log.h"
 #include "thread_var.h"
@@ -55,9 +55,7 @@ rpp_create(void)
 {
        struct rpp *result;
 
-       result = malloc(sizeof(struct rpp));
-       if (result == NULL)
-               return NULL;
+       result = pmalloc(sizeof(struct rpp));
 
        uris_init(&result->certs);
        result->crl.uri = NULL;
index 80f353f982abe9af2bceb7d6edea852e244a26b3..ffb4e41264c4b0923fd4cbbc07bf077a85228031 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <string.h>
+#include "alloc.h"
 #include "crypto/hash.h"
 #include "common.h"
 #include "log.h"
@@ -52,9 +53,7 @@ get_workspace_path(char const *base, char **result)
        char *ptr;
        int error;
 
-       hash = malloc(HASH_LEN * sizeof(unsigned char));
-       if (hash == NULL)
-               enomem_panic();
+       hash = pmalloc(HASH_LEN * sizeof(unsigned char));
 
        hash_len = 0;
        error = hash_str("sha1", base, hash, &hash_len);
@@ -64,9 +63,7 @@ get_workspace_path(char const *base, char **result)
        }
 
        /* Get the first bytes + one slash + NUL char */
-       tmp = malloc(OUT_LEN + 2);
-       if (tmp == NULL)
-               enomem_panic();
+       tmp = pmalloc(OUT_LEN + 2);
 
        ptr = tmp;
        for (i = 0; i < OUT_LEN / 2; i++) {
@@ -87,15 +84,11 @@ tal_elem_create(struct tal_elem **elem, char const *name)
        struct tal_elem *tmp;
        int error;
 
-       tmp = malloc(sizeof(struct tal_elem));
-       if (tmp == NULL)
-               enomem_panic();
+       tmp = pmalloc(sizeof(struct tal_elem));
 
        tmp->uris = db_rrdp_uris_create();
        tmp->visited = true;
-       tmp->file_name = strdup(name);
-       if (tmp->file_name == NULL)
-               enomem_panic();
+       tmp->file_name = pstrdup(name);
 
        error = get_workspace_path(name, &tmp->workspace);
        if (error) {
index 1bf0ced6dfeef74481166302b8e17562de9fd260..07ee0f2ad16708cd1abf1c75d0244c8632c19695 100644 (file)
@@ -4,6 +4,7 @@
 #include <string.h>
 #include <time.h>
 #include "data_structure/uthash.h"
+#include "alloc.h"
 #include "common.h"
 #include "log.h"
 #include "thread_var.h"
@@ -33,20 +34,10 @@ uris_table_create(char const *uri, char const *session_id,
 {
        struct uris_table *tmp;
 
-       tmp = malloc(sizeof(struct uris_table));
-       if (tmp == NULL)
-               enomem_panic();
-       /* Needed by uthash */
-       memset(tmp, 0, sizeof(struct uris_table));
-
-       tmp->uri = strdup(uri);
-       if (tmp->uri == NULL)
-               enomem_panic();
-
-       tmp->data.session_id = strdup(session_id);
-       if (tmp->data.session_id == NULL)
-               enomem_panic();
+       tmp = pzalloc(sizeof(struct uris_table)); /* Zero needed by uthash */
 
+       tmp->uri = pstrdup(uri);
+       tmp->data.session_id = pstrdup(session_id);
        tmp->data.serial = serial;
        tmp->last_update = 0;
        tmp->request_status = req_status;
@@ -118,10 +109,7 @@ db_rrdp_uris_create(void)
 {
        struct db_rrdp_uri *tmp;
 
-       tmp = malloc(sizeof(struct db_rrdp_uri));
-       if (tmp == NULL)
-               enomem_panic();
-
+       tmp = pmalloc(sizeof(struct db_rrdp_uri));
        tmp->table = NULL;
        tmp->current_workspace = NULL;
 
index 677987a2f2600191083b17108c954673e0485969..0c7ee998c8f9157cc056efec1d12a0e25119b505 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include "alloc.h"
 #include "log.h"
 
 DEFINE_ARRAY_LIST_FUNCTIONS(deltas_head, struct delta_head, )
@@ -118,18 +119,12 @@ update_notification_create(char const *uri)
 {
        struct update_notification *result;
 
-       result = malloc(sizeof(struct update_notification));
-       if (result == NULL)
-               return NULL;
+       result = pmalloc(sizeof(struct update_notification));
 
        global_data_init(&result->global_data);
        doc_data_init(&result->snapshot);
        deltas_head_init(&result->deltas_list);
-       result->uri = strdup(uri);
-       if (result->uri == NULL) {
-               free(result);
-               return NULL;
-       }
+       result->uri = pstrdup(uri);
 
        return result;
 }
@@ -155,10 +150,7 @@ snapshot_create(void)
 {
        struct snapshot *tmp;
 
-       tmp = malloc(sizeof(struct snapshot));
-       if (tmp == NULL)
-               enomem_panic();
-
+       tmp = pmalloc(sizeof(struct snapshot));
        global_data_init(&tmp->global_data);
 
        return tmp;
@@ -176,10 +168,7 @@ delta_create(void)
 {
        struct delta *tmp;
 
-       tmp = malloc(sizeof(struct delta));
-       if (tmp == NULL)
-               enomem_panic();
-
+       tmp = pmalloc(sizeof(struct delta));
        global_data_init(&tmp->global_data);
 
        return tmp;
@@ -197,10 +186,7 @@ publish_create(void)
 {
        struct publish *tmp;
 
-       tmp = malloc(sizeof(struct publish));
-       if (tmp == NULL)
-               enomem_panic();
-
+       tmp = pmalloc(sizeof(struct publish));
        doc_data_init(&tmp->doc_data);
        tmp->content = NULL;
        tmp->content_len = 0;
@@ -221,10 +207,7 @@ withdraw_create(void)
 {
        struct withdraw *tmp;
 
-       tmp = malloc(sizeof(struct withdraw));
-       if (tmp == NULL)
-               enomem_panic();
-
+       tmp = pmalloc(sizeof(struct withdraw));
        doc_data_init(&tmp->doc_data);
 
        return tmp;
index 44f6b0b75da540ab7ce6b0b23e894e7fcb81e732..2020b1a582e4e78037249063cc6b9c2d15523056 100644 (file)
@@ -14,6 +14,7 @@
 #include "crypto/hash.h"
 #include "http/http.h"
 #include "xml/relax_ng.h"
+#include "alloc.h"
 #include "common.h"
 #include "file.h"
 #include "log.h"
@@ -148,17 +149,12 @@ base64_sanitize(char *content, char **out)
                return error;
 
        if (original_size <= BUF_SIZE) {
-               result = strdup(content);
-               if (result == NULL)
-                       enomem_panic();
-               *out = result;
+               *out = pstrdup(content);
                return 0;
        }
 
        new_size = original_size + (original_size / BUF_SIZE);
-       result = malloc(new_size + 1);
-       if (result == NULL)
-               enomem_panic();
+       result = pmalloc(new_size + 1);
 
        offset = 0;
        while (original_size > 0){
@@ -175,12 +171,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)
-                       enomem_panic();
-               result = tmp;
-       }
+       if (offset != new_size + 1)
+               result = prealloc(result, offset + 1);
 
        result[offset] = '\0';
        *out = result;
@@ -210,9 +202,7 @@ 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)
-               enomem_panic();
+       result = pmalloc(alloc_size);
 
        error = base64_decode(encoded, result, true, alloc_size, &result_len);
        if (error)
@@ -250,10 +240,7 @@ parse_string(xmlTextReaderPtr reader, char const *attr, char **result)
                            attr, xmlTextReaderConstLocalName(reader));
        }
 
-       tmp = malloc(xmlStrlen(xml_value) + 1);
-       if (tmp == NULL)
-               enomem_panic();
-
+       tmp = pmalloc(xmlStrlen(xml_value) + 1);
        memcpy(tmp, xml_value, xmlStrlen(xml_value));
        tmp[xmlStrlen(xml_value)] = '\0';
        xmlFree(xml_value);
@@ -313,10 +300,7 @@ parse_hex_string(xmlTextReaderPtr reader, bool required, char const *attr,
        }
 
        tmp_len = xmlStrlen(xml_value) / 2;
-       tmp = malloc(tmp_len);
-       if (tmp == NULL)
-               enomem_panic();
-       memset(tmp, 0, tmp_len);
+       tmp = pzalloc(tmp_len);
 
        ptr = tmp;
        xml_cur = (char *) xml_value;
@@ -491,7 +475,7 @@ parse_publish(xmlTextReaderPtr reader, bool parse_hash, bool hash_required,
                if (error)
                        goto release_base64;
 
-               error = hash_validate_file("sha256", uri, tmp->doc_data.hash,
+               error = hash_validate_file(uri, tmp->doc_data.hash,
                    tmp->doc_data.hash_len);
                uri_refput(uri);
                if (error != 0) {
@@ -531,8 +515,8 @@ parse_withdraw(xmlTextReaderPtr reader, struct withdraw **withdraw)
        if (error)
                goto release_tmp;
 
-       error = hash_validate_file("sha256", uri,
-           tmp->doc_data.hash, tmp->doc_data.hash_len);
+       error = hash_validate_file(uri, tmp->doc_data.hash,
+           tmp->doc_data.hash_len);
        if (error)
                goto release_uri;
 
@@ -780,7 +764,7 @@ parse_snapshot(struct rpki_uri *uri, struct proc_upd_args *args)
 
        fnstack_push_uri(uri);
        /* Hash validation */
-       error = hash_validate_file("sha256", uri, args->parent->snapshot.hash,
+       error = hash_validate_file(uri, args->parent->snapshot.hash,
            args->parent->snapshot.hash_len);
        if (error)
                goto pop;
@@ -842,7 +826,7 @@ parse_delta(struct rpki_uri *uri, struct delta_head *parents_data,
        expected_data = &parents_data->doc_data;
 
        fnstack_push_uri(uri);
-       error = hash_validate_file("sha256", uri, expected_data->hash,
+       error = hash_validate_file(uri, expected_data->hash,
            expected_data->hash_len);
        if (error)
                goto pop_fnstack;
index 9c901f58bdced0c0d2a02136c6e4754a5f8a3b51..a24323ee31d20aff909e4961a2792f5553965074 100644 (file)
@@ -9,6 +9,7 @@
 #include <sys/stat.h>
 #include <sys/wait.h>
 
+#include "alloc.h"
 #include "common.h"
 #include "config.h"
 #include "log.h"
@@ -31,11 +32,9 @@ rsync_create(void)
 {
        struct uri_list *visited_uris;
 
-       visited_uris = malloc(sizeof(struct uri_list));
-       if (visited_uris == NULL)
-               enomem_panic();
-
+       visited_uris = pmalloc(sizeof(struct uri_list));
        SLIST_INIT(visited_uris);
+
        return visited_uris;
 }
 
@@ -104,10 +103,7 @@ mark_as_downloaded(struct rpki_uri *uri, struct uri_list *visited_uris)
 {
        struct uri *node;
 
-       node = malloc(sizeof(struct uri));
-       if (node == NULL)
-               enomem_panic();
-
+       node = pmalloc(sizeof(struct uri));
        node->uri = uri;
        uri_refget(uri);
 
@@ -212,9 +208,7 @@ prepare_rsync(struct rpki_uri *uri, bool is_ta, char ***args, size_t *args_len)
         * and we need to add the program name (for some reason) and NULL
         * elements, and replace $REMOTE and $LOCAL.
         */
-       copy_args = calloc(config_args->length + 2, sizeof(char *));
-       if (copy_args == NULL)
-               enomem_panic();
+       copy_args = pcalloc(config_args->length + 2, sizeof(char *));
 
        copy_args[0] = config_get_rsync_program();
        copy_args[config_args->length + 1] = NULL;
@@ -224,14 +218,11 @@ prepare_rsync(struct rpki_uri *uri, bool is_ta, char ***args, size_t *args_len)
 
        for (i = 0; i < config_args->length; i++) {
                if (strcmp(config_args->array[i], "$REMOTE") == 0)
-                       copy_args[i + 1] = strdup(uri_get_global(uri));
+                       copy_args[i + 1] = pstrdup(uri_get_global(uri));
                else if (strcmp(config_args->array[i], "$LOCAL") == 0)
-                       copy_args[i + 1] = strdup(uri_get_local(uri));
+                       copy_args[i + 1] = pstrdup(uri_get_local(uri));
                else
-                       copy_args[i + 1] = strdup(config_args->array[i]);
-
-               if (copy_args[i + 1] == NULL)
-                       enomem_panic();
+                       copy_args[i + 1] = pstrdup(config_args->array[i]);
        }
 
        *args = copy_args;
@@ -287,9 +278,7 @@ log_buffer(char const *buffer, ssize_t read, int type, bool log_operation)
 #define PRE_RSYNC "[RSYNC exec]: "
        char *cpy, *cur, *tmp;
 
-       cpy = malloc(read + 1);
-       if (cpy == NULL)
-               enomem_panic();
+       cpy = pmalloc(read + 1);
 
        strncpy(cpy, buffer, read);
        cpy[read] = '\0';
index cbfea75bbced1448c87f765e21c003a72fa81215..13f6d43e28fdad435384afbec01395afc3ec64d1 100644 (file)
@@ -3,6 +3,7 @@
 #include <sys/types.h> /* AF_INET, AF_INET6 (needed in OpenBSD) */
 #include <sys/socket.h> /* AF_INET, AF_INET6 (needed in OpenBSD) */
 
+#include "alloc.h"
 #include "log.h"
 #include "data_structure/uthash.h"
 
@@ -26,12 +27,10 @@ db_table_create(void)
 {
        struct db_table *table;
 
-       table = malloc(sizeof(struct db_table));
-       if (table == NULL)
-               return NULL;
-
+       table = pmalloc(sizeof(struct db_table));
        table->roas = NULL;
        table->router_keys = NULL;
+
        return table;
 }
 
@@ -94,10 +93,7 @@ add_roa(struct db_table *table, struct hashable_roa const *stack_new)
        struct hashable_roa *old;
        int error;
 
-       new = malloc(sizeof(struct hashable_roa));
-       if (new == NULL)
-               enomem_panic();
-       memcpy(new, stack_new, sizeof(*new));
+       new = pmclone(stack_new, sizeof(struct hashable_roa));
 
        errno = 0;
        HASH_REPLACE(hh, table->roas, data, sizeof(new->data), new, old);
@@ -207,11 +203,7 @@ rtrhandler_handle_router_key(struct db_table *table,
        struct hashable_key *key;
        int error;
 
-       key = malloc(sizeof(struct hashable_key));
-       if (key == NULL)
-               enomem_panic();
-       /* Needed by uthash */
-       memset(key, 0, sizeof(struct hashable_key));
+       key = pzalloc(sizeof(struct hashable_key)); /* Zero needed by uthash */
 
        router_key_init(&key->data, ski, as, spk);
 
index d4f1d1782ca420dd02c842ccbff4ee87c6cbc016..84c734852d621f3cdb5d8484775e3d57beb7a7f8 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdatomic.h>
 #include <sys/types.h> /* AF_INET, AF_INET6 (needed in OpenBSD) */
 #include <sys/socket.h> /* AF_INET, AF_INET6 (needed in OpenBSD) */
+#include "alloc.h"
 #include "types/address.h"
 #include "data_structure/array_list.h"
 
@@ -50,9 +51,7 @@ deltas_create(void)
 {
        struct deltas *result;
 
-       result = malloc(sizeof(struct deltas));
-       if (result == NULL)
-               enomem_panic();
+       result = pmalloc(sizeof(struct deltas));
 
        deltas_v4_init(&result->v4.adds);
        deltas_v4_init(&result->v4.removes);
index cae242f832fb8767a44c99c4a940a26ff1953562..d733215a0b13c4dc02276b5a830fcc3a428a4b8b 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <limits.h>
 #include <errno.h>
+#include "alloc.h"
 #include "config.h"
 #include "log.h"
 
@@ -16,17 +17,13 @@ darray_create(void)
 {
        struct deltas_array *result;
 
-       result = malloc(sizeof(struct deltas_array));
-       if (result == NULL)
-               enomem_panic();
+       result = pmalloc(sizeof(struct deltas_array));
 
-       result->array = calloc(config_get_deltas_lifetime(),
+       result->array = pcalloc(config_get_deltas_lifetime(),
            sizeof(struct deltas *));
-       if (result->array == NULL)
-               enomem_panic();
-
        result->len = 0;
        result->last = UINT_MAX;
+
        return result;
 }
 
index 43d7482fb973b04f9d4b3823154ccfa38a8f0f9a..90c61d02d052dcd914216ef17f7afcb1b499aba5 100644 (file)
@@ -6,6 +6,7 @@
 #include <time.h>
 #include <sys/queue.h>
 
+#include "alloc.h"
 #include "common.h"
 #include "output_printer.h"
 #include "validation_handler.h"
@@ -430,12 +431,10 @@ vrp_ovrd_remove(struct delta_vrp const *delta, void *arg)
                        return 0;
                }
 
-       ptr = malloc(sizeof(struct vrp_node));
-       if (ptr == NULL)
-               enomem_panic();
-
+       ptr = pmalloc(sizeof(struct vrp_node));
        ptr->delta = *delta;
        SLIST_INSERT_HEAD(filtered_vrps, ptr, next);
+
        return 0;
 }
 
@@ -461,12 +460,10 @@ router_key_ovrd_remove(struct delta_router_key const *delta, void *arg)
                }
        }
 
-       ptr = malloc(sizeof(struct rk_node));
-       if (ptr == NULL)
-               enomem_panic();
-
+       ptr = pmalloc(sizeof(struct rk_node));
        ptr->delta = *delta;
        SLIST_INSERT_HEAD(filtered_keys, ptr, next);
+
        return 0;
 }
 
index 7df38d2218a2b812c8cdc0c6a3b853cc1db2a841..0ae225bd8a12189cb12ffa91c352bfdf46676ac0 100644 (file)
@@ -1,6 +1,7 @@
 #include "err_pdu.h"
 
 #include <unistd.h>
+#include "alloc.h"
 #include "pdu_sender.h"
 #include "log.h"
 
@@ -30,7 +31,7 @@ err_pdu_send(int fd, uint8_t version, rtr_error_code_t code,
         */
 
        /* Need a clone to remove the const. */
-       message = (message_const != NULL) ? strdup(message_const) : NULL;
+       message = (message_const != NULL) ? pstrdup(message_const) : NULL;
        send_error_report_pdu(fd, version, code, request, message);
        free(message);
 
index eb303efb8726daecc7aa79c2d75d33f2616bf7e4..7524e478269c9b348b74637f53889313159f5c17 100644 (file)
@@ -5,6 +5,7 @@
 #include <string.h>
 #include <syslog.h>
 
+#include "alloc.h"
 #include "common.h"
 #include "log.h"
 #include "types/address.h"
@@ -164,10 +165,7 @@ pdu_load(struct pdu_reader *reader, struct rtr_client *client,
                return RESPOND_ERROR(err_pdu_send_unsupported_pdu_type(
                    client->fd, client->rtr_version, request));
 
-       request->pdu = malloc(meta->length);
-       if (request->pdu == NULL)
-               /* No error report PDU on allocation failures. */
-               enomem_panic();
+       request->pdu = pmalloc(meta->length);
 
        error = meta->from_stream(&header, reader, request->pdu);
        if (error) {
index 17ea92da83906fd2fd176599d9a4cfc06ab18827..e722b763fa91cd684544424c94599d1326966b1d 100644 (file)
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <arpa/inet.h> /* INET_ADDRSTRLEN */
 
+#include "alloc.h"
 #include "common.h"
 #include "config.h"
 #include "log.h"
@@ -292,9 +293,7 @@ send_error_report_pdu(int fd, uint8_t version, uint16_t code,
            + 4 /* Length of Error Text field */
            + pdu.error_message_length;
 
-       data = malloc(pdu.header.length);
-       if (data == NULL)
-               enomem_panic();
+       data = pmalloc(pdu.header.length);
 
        len = serialize_error_report_pdu(&pdu, data);
        if (len != pdu.header.length)
index 116811d9d8297c71c85e326d3ccf63a27cc5298f..d71af2959ffe23edcb6870db741176a054d37332 100644 (file)
@@ -8,6 +8,7 @@
 #include <unistd.h>
 #include <netinet/in.h>
 
+#include "alloc.h"
 #include "log.h"
 
 static int get_octets(unsigned char);
@@ -189,9 +190,7 @@ read_string(struct pdu_reader *reader, uint32_t string_len, rtr_char **result)
         * to be relatively small now.
         */
 
-       string = malloc(string_len + 1); /* Include NULL chara. */
-       if (string == NULL)
-               enomem_panic();
+       string = pmalloc(string_len + 1); /* Include NULL chara. */
 
        memcpy(string, reader->buffer, string_len);
        reader->buffer += string_len;
index 5c0b42bbe8d5027f45b1fffc71adcce525dac531..7784ad2f0c4258af7889beebc0f19c726d80117d 100644 (file)
@@ -12,6 +12,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 
+#include "alloc.h"
 #include "config.h"
 #include "types/address.h"
 #include "data_structure/array_list.h"
@@ -84,22 +85,14 @@ parse_address(char const *full_address, char **address, char **service)
 
        if (full_address == NULL) {
                tmp_addr = NULL;
-               tmp_serv = strdup(config_get_server_port());
-               if (tmp_serv == NULL)
-                       enomem_panic();
+               tmp_serv = pstrdup(config_get_server_port());
                goto done;
        }
 
        ptr = strrchr(full_address, '#');
        if (ptr == NULL) {
-               tmp_addr = strdup(full_address);
-               if (tmp_addr == NULL)
-                       enomem_panic();
-
-               tmp_serv = strdup(config_get_server_port());
-               if (tmp_serv == NULL)
-                       enomem_panic();
-
+               tmp_addr = pstrdup(full_address);
+               tmp_serv = pstrdup(config_get_server_port());
                goto done;
        }
 
@@ -108,17 +101,12 @@ parse_address(char const *full_address, char **address, char **service)
                    full_address);
 
        tmp_addr_len = strlen(full_address) - strlen(ptr);
-       tmp_addr = malloc(tmp_addr_len + 1);
-       if (tmp_addr == NULL)
-               enomem_panic();
+       tmp_addr = pmalloc(tmp_addr_len + 1);
 
        memcpy(tmp_addr, full_address, tmp_addr_len);
        tmp_addr[tmp_addr_len] = '\0';
 
-       tmp_serv = strdup(ptr + 1);
-       if (tmp_serv == NULL)
-               enomem_panic();
-
+       tmp_serv = pstrdup(ptr + 1);
        /* Fall through */
 done:
        *address = tmp_addr;
@@ -298,7 +286,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;
+               server.addr = (input_addr != NULL) ? pstrdup(input_addr) : NULL;
                server_arraylist_add(&servers, &server);
 
                return 0; /* Happy path */
@@ -509,9 +497,7 @@ __handle_client_request(struct rtr_client *client)
 {
        struct client_request *request;
 
-       request = malloc(sizeof(struct client_request));
-       if (request == NULL)
-               enomem_panic();
+       request = pmalloc(sizeof(struct client_request));
 
        request->client = client;
        if (!read_until_block(client->fd, request)) {
@@ -602,11 +588,7 @@ fddb_poll(void)
        unsigned int i;
        int error;
 
-       pollfds = calloc(servers.len + clients.len, sizeof(struct pollfd));
-       if (pollfds == NULL) {
-               enomem_panic();
-               return PV_RETRY;
-       }
+       pollfds = pcalloc(servers.len + clients.len, sizeof(struct pollfd));
 
        ARRAYLIST_FOREACH(&servers, server, i)
                init_pollfd(&pollfds[i], server->fd);
index a945e8d3d0ed526f6e0367ae3bce27c41cf1c426..fb4869fcfc6dc2a87c5eb7b7cc8122c745297224 100644 (file)
@@ -4,6 +4,7 @@
 #include <time.h>
 #include <arpa/inet.h>
 
+#include "alloc.h"
 #include "common.h"
 #include "crypto/base64.h"
 #include "data_structure/array_list.h"
@@ -63,9 +64,7 @@ slurm_lists_create(void)
 {
        struct slurm_lists *cache;
 
-       cache = malloc(sizeof(struct slurm_lists));
-       if (cache == NULL)
-               enomem_panic();
+       cache = pmalloc(sizeof(struct slurm_lists));
 
        al_filter_prefix_init(&cache->filter_pfx_al);
        al_assertion_prefix_init(&cache->assertion_pfx_al);
@@ -100,9 +99,7 @@ db_slurm_create(struct slurm_csum_list *csums, struct db_slurm **result)
        struct db_slurm *db;
        int error;
 
-       db = malloc(sizeof(struct db_slurm));
-       if (db == NULL)
-               enomem_panic();
+       db = pmalloc(sizeof(struct db_slurm));
 
        error = get_current_time(&db->loaded_date);
        if (error) {
index 525bac335bd5c2979b45616819d70e7152affe28..32bbf252ac9a79a30210251bd7dd4f94bd127623 100644 (file)
@@ -5,6 +5,7 @@
 #include <sys/types.h> /* AF_INET, AF_INET6 (needed in OpenBSD) */
 #include <sys/socket.h> /* AF_INET, AF_INET6 (needed in OpenBSD) */
 
+#include "alloc.h"
 #include "log.h"
 #include "config.h"
 #include "common.h"
@@ -131,15 +132,10 @@ __slurm_load_checksums(char const *location, void *arg)
 {
        struct slurm_csum_list *list;
        struct slurm_file_csum *csum;
-       int error;
 
-       csum = malloc(sizeof(struct slurm_file_csum));
-       if (csum == NULL)
-               enomem_panic();
+       csum = pmalloc(sizeof(struct slurm_file_csum));
 
-       error = hash_local_file("sha256", location, csum->csum,
-           &csum->csum_len);
-       if (error) {
+       if (hash_local_file(location, csum->csum, &csum->csum_len) != 0) {
                free(csum);
                return pr_op_err("Calculating slurm hash");
        }
index 476a2697d7cd5d31edbee619c98588f85c2e1d5f..5951bdcee7eb77d553a2b14c674ce48b6d6e4d28 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "crypto/base64.h"
 #include "algorithm.h"
+#include "alloc.h"
 #include "log.h"
 #include "json_parser.h"
 #include "types/address.h"
@@ -131,9 +132,7 @@ set_prefix(json_t *object, bool is_assertion, struct slurm_prefix *result,
        } else if (error)
                return error;
 
-       clone = strdup(str_prefix);
-       if (clone == NULL)
-               enomem_panic();
+       clone = pstrdup(str_prefix);
 
        token = strtok(clone, "/");
        isv4 = strchr(token, ':') == NULL;
index 6e535f66203795dbf5375abae5e5855e70dd4096..add39180299948e44d5a4547b56494a3a2cc8d50 100644 (file)
@@ -3,6 +3,8 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+
+#include "alloc.h"
 #include "log.h"
 
 struct sorted_array {
@@ -24,15 +26,9 @@ sarray_create(size_t elem_size, sarray_cmp cmp)
 {
        struct sorted_array *result;
 
-       result = malloc(sizeof(struct sorted_array));
-       if (result == NULL)
-               return NULL;
+       result = pmalloc(sizeof(struct sorted_array));
 
-       result->array = calloc(8, elem_size);
-       if (result->array == NULL) {
-               free(result);
-               return NULL;
-       }
+       result->array = pcalloc(8, elem_size);
        result->count = 0;
        result->len = 8;
        result->size = elem_size;
@@ -105,17 +101,14 @@ int
 sarray_add(struct sorted_array *sarray, void *element)
 {
        int error;
-       void *tmp;
 
        error = compare(sarray, element);
        if (error)
                return error;
 
        if (sarray->count >= sarray->len) {
-               tmp = realloc(sarray->array, 2 * sarray->len * sarray->size);
-               if (tmp == NULL)
-                       enomem_panic();
-               sarray->array = tmp;
+               sarray->array = realloc(sarray->array,
+                   2 * sarray->len * sarray->size);
                sarray->len *= 2;
        }
 
index 7a0279044f7bc084917df478684313807824f255..9628a55587079088910abb61d2d5f94955fd5159 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <errno.h>
 #include "rrdp/db/db_rrdp.h"
+#include "alloc.h"
 #include "log.h"
 #include "thread_var.h"
 
@@ -94,9 +95,7 @@ validation_prepare(struct validation **out, struct tal *tal,
        X509_VERIFY_PARAM *params;
        int error;
 
-       result = malloc(sizeof(struct validation));
-       if (!result)
-               enomem_panic();
+       result = pmalloc(sizeof(struct validation));
 
        error = state_store(result);
        if (error)
index 750dca51bcd81642bb0b77f37d2f3dbd3a337f09..0ecad62cea632d4d713cd8cae6e1d40d009b011d 100644 (file)
@@ -2,6 +2,8 @@
 
 #include <errno.h>
 #include <string.h>
+
+#include "alloc.h"
 #include "log.h"
 
 /**
@@ -12,9 +14,7 @@ string_clone(void const *string, size_t size)
 {
        char *result;
 
-       result = malloc(size + 1);
-       if (result == NULL)
-               enomem_panic();
+       result = pmalloc(size + 1);
 
        memcpy(result, string, size);
        result[size] = '\0';
@@ -52,9 +52,7 @@ BN2string(BIGNUM *bn, char **_result)
        }
 
        written = BIO_number_written(bio);
-       result = malloc(written + 1);
-       if (result == NULL)
-               enomem_panic();
+       result = pmalloc(written + 1);
 
        BIO_read(bio, result, written);
        result[written] = '\0';
index b9cb1b2fb03b05a4eb49ef0d0b8e6d18ce477e63..8f56aca783b2ecfaa416a102933b322140f5fedd 100644 (file)
@@ -4,6 +4,7 @@
 #include <pthread.h>
 #include <stdlib.h>
 #include <string.h>
+#include "alloc.h"
 #include "common.h"
 #include "log.h"
 
@@ -115,9 +116,7 @@ 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)
-               enomem_panic();
+       task = pmalloc(sizeof(struct thread_pool_task));
 
        task->name = name;
        task->cb = cb;
@@ -297,9 +296,7 @@ thread_pool_create(char const *name, unsigned int threads,
        struct thread_pool *result;
        int error;
 
-       result = malloc(sizeof(struct thread_pool));
-       if (result == NULL)
-               enomem_panic();
+       result = pmalloc(sizeof(struct thread_pool));
 
        /* Init locking */
        error = pthread_mutex_init(&result->lock, NULL);
@@ -330,9 +327,7 @@ thread_pool_create(char const *name, unsigned int threads,
        result->stop = false;
        result->working_count = 0;
        result->thread_count = 0;
-       result->thread_ids = calloc(threads, sizeof(pthread_t));
-       if (result->thread_ids == NULL)
-               enomem_panic();
+       result->thread_ids = pcalloc(threads, sizeof(pthread_t));
        result->thread_ids_len = threads;
 
        error = spawn_threads(result);
index 1dbb74221b65780bba8c89e6cf334075981e6902..4dcbe5fb05f5e73a2e26f2da65f298e51e476739 100644 (file)
@@ -8,6 +8,7 @@
 #include <arpa/inet.h>
 #include <sys/socket.h>
 
+#include "alloc.h"
 #include "config.h"
 
 static pthread_key_t state_key;
@@ -113,14 +114,9 @@ fnstack_init(void)
        struct filename_stack *files;
        int error;
 
-       files = malloc(sizeof(struct filename_stack));
-       if (files == NULL)
-               enomem_panic();
-
-       files->filenames = malloc(32 * sizeof(char *));
-       if (files->filenames == NULL)
-               enomem_panic();
+       files = pmalloc(sizeof(struct filename_stack));
 
+       files->filenames = pmalloc(32 * sizeof(char *));
        files->len = 0;
        files->size = 32;
 
@@ -176,17 +172,14 @@ void
 fnstack_push(char const *file)
 {
        struct filename_stack *files;
-       char const **tmp;
 
        files = pthread_getspecific(filenames_key);
        if (files == NULL || files->filenames == NULL)
                return;
 
        if (files->len >= files->size) {
-               tmp = realloc(files->filenames, 2 * files->size * sizeof(char *));
-               if (tmp == NULL)
-                       enomem_panic();
-               files->filenames = tmp;
+               files->filenames = prealloc(files->filenames,
+                   2 * files->size * sizeof(char *));
                files->size *= 2;
        }
 
@@ -238,9 +231,7 @@ working_repo_init(void)
        struct working_repo *repo;
        int error;
 
-       repo = malloc(sizeof(struct working_repo));
-       if (repo == NULL)
-               enomem_panic();
+       repo = pmalloc(sizeof(struct working_repo));
 
        repo->uri = NULL;
        repo->level = 0;
index a24c524351ef1219e86bd6100eb52676ff72b763..07c65dc987bd17b621bf419e1c7a46a9b50e42c8 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 #include <strings.h>
 #include "rrdp/db/db_rrdp_uris.h"
+#include "alloc.h"
 #include "common.h"
 #include "config.h"
 #include "log.h"
@@ -124,9 +125,7 @@ str2global(char const *str, size_t str_len, struct rpki_uri *uri)
                        return error;
        }
 
-       uri->global = malloc(str_len + 1);
-       if (uri->global == NULL)
-               enomem_panic();
+       uri->global = pmalloc(str_len + 1);
        strncpy(uri->global, str, str_len);
        uri->global[str_len] = '\0';
        uri->global_len = str_len;
@@ -203,9 +202,7 @@ ia5str2global(struct rpki_uri *uri, char const *mft, IA5String_t *ia5)
                return pr_val_err("Manifest URL '%s' contains no slashes.", mft);
 
        dir_len = (slash_pos + 1) - mft;
-       joined = malloc(dir_len + ia5->size + 1);
-       if (joined == NULL)
-               enomem_panic();
+       joined = pmalloc(dir_len + ia5->size + 1);
 
        strncpy(joined, mft, dir_len);
        strncpy(joined + dir_len, (char *) ia5->buf, ia5->size);
@@ -281,17 +278,12 @@ static char *
 get_local_workspace(void)
 {
        char const *workspace;
-       char *tmp;
 
        workspace = db_rrdp_uris_workspace_get();
        if (workspace == NULL)
                return NULL;
 
-       tmp = strdup(workspace);
-       if (tmp == NULL)
-               enomem_panic();
-
-       return tmp;
+       return pstrdup(workspace);
 }
 
 /**
@@ -345,9 +337,7 @@ uri_create(struct rpki_uri **result, uint8_t flags, void const *guri,
        struct rpki_uri *uri;
        int error;
 
-       uri = malloc(sizeof(struct rpki_uri));
-       if (uri == NULL)
-               enomem_panic();
+       uri = pmalloc(sizeof(struct rpki_uri));
 
        error = str2global(guri, guri_len, uri);
        if (error) {
@@ -413,9 +403,7 @@ uri_create_mft(struct rpki_uri **result, struct rpki_uri *mft, IA5String_t *ia5,
        uint8_t flags;
        int error;
 
-       uri = malloc(sizeof(struct rpki_uri));
-       if (uri == NULL)
-               enomem_panic();
+       uri = pmalloc(sizeof(struct rpki_uri));
 
        error = ia5str2global(uri, mft->global, ia5);
        if (error) {
index ee60935932218b446c2a06262ab3a6070837b11c..e935e6e2e0adba48b1f7514973e1c9d3fc6e213b 100644 (file)
@@ -3,6 +3,7 @@
 #include <sys/queue.h>
 #include <stddef.h>
 #include <string.h>
+#include "alloc.h"
 #include "log.h"
 #include "delete_dir_daemon.h"
 #include "data_structure/array_list.h"
@@ -27,17 +28,8 @@ visited_elem_create(char const *uri)
 {
        struct visited_elem *tmp;
 
-       tmp = malloc(sizeof(struct visited_elem));
-       if (tmp == NULL)
-               enomem_panic();
-       /* Needed by uthash */
-       memset(tmp, 0, sizeof(struct visited_elem));
-
-       tmp->uri = strdup(uri);
-       if (tmp->uri == NULL) {
-               free(tmp);
-               enomem_panic();
-       }
+       tmp = pzalloc(sizeof(struct visited_elem)); /* Zero needed by uthash */
+       tmp->uri = pstrdup(uri);
 
        return tmp;
 }
@@ -54,9 +46,7 @@ visited_uris_create(void)
 {
        struct visited_uris *tmp;
 
-       tmp = malloc(sizeof(struct visited_uris));
-       if (tmp == NULL)
-               enomem_panic();
+       tmp = pmalloc(sizeof(struct visited_uris));
 
        tmp->table = NULL;
        tmp->refs = 1;
@@ -130,9 +120,7 @@ visited_uris_to_arr(struct visited_uris *uris, struct uris_roots *roots)
        for (elem = uris->table; elem != NULL; elem = elem->hh.next) {
                last_slash = strrchr(elem->uri, '/');
                size = last_slash - elem->uri;
-               tmp = malloc(size + 1);
-               if (tmp == NULL)
-                       enomem_panic();
+               tmp = pmalloc(size + 1);
                strncpy(tmp, elem->uri, size);
                tmp[size] = '\0';
                uris_roots_add(roots, &tmp);
index 4a37b6148c264c55ea8059f4def99513b401ad11..591412e4a5d43b62b253a9f0408869834e9e0c36 100644 (file)
@@ -2,6 +2,7 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include "alloc.c"
 #include "file.c"
 #include "impersonator.c"
 #include "line_file.c"
index 294f964b8b17abfde4f1cdef124fbf386175b7f2..d99c36c233131bd86d2eac63d27c013bdd8ca056 100644 (file)
@@ -2,6 +2,7 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include "alloc.c"
 #include "impersonator.c"
 #include "log.c"
 #include "rrdp/rrdp_objects.c"
index 3bcdcf55ac10196fbc1878084133099e717c1c23..e46d88bab9676546bade975b5646a027bff06b9f 100644 (file)
@@ -2,6 +2,7 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include "alloc.c"
 #include "common.c"
 #include "log.c"
 #include "impersonator.c"
index 2122f9b799fbf9338adae831d2eeef370b3bab78..6a202835ecf65024220eea4eb4643c532ab9b61a 100644 (file)
@@ -1,6 +1,7 @@
 #include <check.h>
 #include <stdlib.h>
 
+#include "alloc.c"
 #include "common.c"
 #include "log.c"
 #include "impersonator.c"
index fbfd7a7521362e92acda26fcf840bd64b686586c..21fa6c1d596dc4e478efd99fdeceaab87b34e736 100644 (file)
@@ -1,6 +1,7 @@
 #include <check.h>
 #include <stdlib.h>
 
+#include "alloc.c"
 #include "log.c"
 #include "impersonator.c"
 #include "types/address.c"
index db6a81f6f1cb35bc187e2e68f3e9a6fe7c8bdd23..2172c44a1a3172449174360dc6c4e33203876416 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "crypto/base64.c"
 #include "algorithm.c"
+#include "alloc.c"
 #include "common.c"
 #include "file.c"
 #include "impersonator.c"
index 29694df7164b5ae2e23362129e8b4b43baf8d6aa..40dbe658d8e5888a7c0efb8d9ea03590f6fc90c8 100644 (file)
@@ -4,6 +4,7 @@
 #include <sys/queue.h>
 
 #include "algorithm.c"
+#include "alloc.c"
 #include "common.c"
 #include "file.c"
 #include "impersonator.c"
index 284cab26b13d0708d7562bc76a0785ba2b74e69b..c7347504cf0966e45477f81b70aa0de8ef59cb62 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include "alloc.c"
 #include "common.c"
 #include "log.c"
 #include "impersonator.c"
index 779c12e513e47755320dc4b723cb0ac4604e0b85..1f7a9dad411d9712354a3d471610190e5e5c2ecd 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "alloc.c"
 #include "impersonator.c"
 #include "log.c"
 #include "rtr/primitive_reader.c"
index 907cbc0162da313a492d2d5a0621a11774cf1186..0351787299876b4a98768ba3cba8b4744d79bbe0 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include "common.h"
 
+#include "alloc.c"
 #include "file.c"
 #include "impersonator.c"
 #include "line_file.c"
index 781382649b3eb26aebb01ca5be0761b47b94be5d..5ef0da581763d0b67af22409098880a963a597f6 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#include "alloc.c"
 #include "common.c"
 #include "log.c"
 #include "impersonator.c"
index 7636386e2296a271a4db26dd6ab568644ce910ec..90e70c8add4c3dc93917b2502e7fe5af7fbc8ff7 100644 (file)
@@ -2,6 +2,7 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include "alloc.c"
 #include "common.c"
 #include "log.c"
 #include "impersonator.c"
index 6604d2e6b08b32750767660d827b94ea01612b44..139d766931212c85541f3072c0c33176b72c5970 100644 (file)
@@ -2,6 +2,7 @@
 #include <errno.h>
 #include <stdint.h>
 
+#include "alloc.c"
 #include "common.c"
 #include "log.c"
 #include "impersonator.c"
index 8ccf741563f5dcc244bde19c52bf28626c8ca829..0f03b9a960bb79679a0cd3314ab1b881545d3e7a 100644 (file)
@@ -1,5 +1,6 @@
 #include <check.h>
 
+#include "alloc.c"
 #include "log.c"
 #include "impersonator.c"
 #include "object/vcard.c"