tree-wide: replace reallocarray() with GREEDY_REALLOC()

This commit is contained in:
Yu Watanabe
2024-10-08 19:56:09 +09:00
parent 5f9f38b8df
commit 223a67e572
18 changed files with 81 additions and 177 deletions
+2 -4
View File
@@ -2423,15 +2423,13 @@ static int bus_append_service_property(sd_bus_message *m, const char *field, con
if (r >= 0) {
assert(r >= 0 && r < 256);
status = reallocarray(status, n_status + 1, sizeof(int));
if (!status)
if (!GREEDY_REALLOC(status, n_status + 1))
return log_oom();
status[n_status++] = r;
} else if ((r = signal_from_string(word)) >= 0) {
signal = reallocarray(signal, n_signal + 1, sizeof(int));
if (!signal)
if (!GREEDY_REALLOC(signal, n_signal + 1))
return log_oom();
signal[n_signal++] = r;
+9 -15
View File
@@ -76,18 +76,15 @@ static int trie_children_cmp(const struct trie_child_entry *a, const struct trie
}
static int node_add_child(struct trie *trie, struct trie_node *node, struct trie_node *node_child, uint8_t c) {
struct trie_child_entry *child;
/* extend array, add new entry, sort for bisection */
child = reallocarray(node->children, node->children_count + 1, sizeof(struct trie_child_entry));
if (!child)
if (!GREEDY_REALLOC(node->children, node->children_count + 1))
return -ENOMEM;
node->children = child;
trie->children_count++;
node->children[node->children_count].c = c;
node->children[node->children_count].child = node_child;
node->children_count++;
node->children[node->children_count++] = (struct trie_child_entry) {
.c = c,
.child = node_child,
};
typesafe_qsort(node->children, node->children_count, trie_children_cmp);
trie->nodes_count++;
@@ -136,7 +133,6 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
const char *key, const char *value,
const char *filename, uint16_t file_priority, uint32_t line_number, bool compat) {
ssize_t k, v, fn = 0;
struct trie_value_entry *val;
k = strbuf_add_string(trie->strings, key);
if (k < 0)
@@ -155,7 +151,7 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
struct trie_value_entry search = {
.key_off = k,
.value_off = v,
};
}, *val;
val = typesafe_bsearch_r(&search, node->values, node->values_count, trie_values_cmp, trie);
if (val) {
@@ -170,19 +166,17 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
}
/* extend array, add new entry, sort for bisection */
val = reallocarray(node->values, node->values_count + 1, sizeof(struct trie_value_entry));
if (!val)
if (!GREEDY_REALLOC(node->values, node->values_count + 1))
return -ENOMEM;
trie->values_count++;
node->values = val;
node->values[node->values_count] = (struct trie_value_entry) {
node->values[node->values_count++] = (struct trie_value_entry) {
.key_off = k,
.value_off = v,
.filename_off = fn,
.file_priority = file_priority,
.line_number = line_number,
};
node->values_count++;
typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
return 0;
}
+2 -5
View File
@@ -295,7 +295,6 @@ InstallChangeType install_changes_add(
const char *source) {
_cleanup_free_ char *p = NULL, *s = NULL;
InstallChange *c;
int r;
assert(!changes == !n_changes);
@@ -310,10 +309,8 @@ InstallChangeType install_changes_add(
if (!changes)
return type;
c = reallocarray(*changes, *n_changes + 1, sizeof(InstallChange));
if (!c)
if (!GREEDY_REALLOC(*changes, *n_changes + 1))
return -ENOMEM;
*changes = c;
r = path_simplify_alloc(path, &p);
if (r < 0)
@@ -323,7 +320,7 @@ InstallChangeType install_changes_add(
if (r < 0)
return r;
c[(*n_changes)++] = (InstallChange) {
(*changes)[(*n_changes)++] = (InstallChange) {
.type = type,
.path = TAKE_PTR(p),
.source = TAKE_PTR(s),
+7 -23
View File
@@ -771,8 +771,6 @@ static int dispatch_pkcs11_key(const char *name, sd_json_variant *variant, sd_js
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
JSON_VARIANT_ARRAY_FOREACH(e, variant) {
Pkcs11EncryptedKey *array, *k;
static const sd_json_dispatch_field pkcs11_key_dispatch_table[] = {
{ "uri", SD_JSON_VARIANT_STRING, dispatch_pkcs11_uri, offsetof(Pkcs11EncryptedKey, uri), SD_JSON_MANDATORY },
{ "data", SD_JSON_VARIANT_STRING, dispatch_pkcs11_key_data, 0, SD_JSON_MANDATORY },
@@ -783,12 +781,10 @@ static int dispatch_pkcs11_key(const char *name, sd_json_variant *variant, sd_js
if (!sd_json_variant_is_object(e))
return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
array = reallocarray(h->pkcs11_encrypted_key, h->n_pkcs11_encrypted_key + 1, sizeof(Pkcs11EncryptedKey));
if (!array)
if (!GREEDY_REALLOC(h->pkcs11_encrypted_key, h->n_pkcs11_encrypted_key + 1))
return log_oom();
h->pkcs11_encrypted_key = array;
k = h->pkcs11_encrypted_key + h->n_pkcs11_encrypted_key;
Pkcs11EncryptedKey *k = h->pkcs11_encrypted_key + h->n_pkcs11_encrypted_key;
*k = (Pkcs11EncryptedKey) {};
r = sd_json_dispatch(e, pkcs11_key_dispatch_table, flags, k);
@@ -834,20 +830,16 @@ static int dispatch_fido2_hmac_credential_array(const char *name, sd_json_varian
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
JSON_VARIANT_ARRAY_FOREACH(e, variant) {
Fido2HmacCredential *array;
size_t l;
void *b;
array = reallocarray(h->fido2_hmac_credential, h->n_fido2_hmac_credential + 1, sizeof(Fido2HmacCredential));
if (!array)
if (!GREEDY_REALLOC(h->fido2_hmac_credential, h->n_fido2_hmac_credential + 1))
return log_oom();
r = sd_json_variant_unbase64(e, &b, &l);
if (r < 0)
return json_log(variant, flags, r, "Failed to decode FIDO2 credential ID: %m");
h->fido2_hmac_credential = array;
h->fido2_hmac_credential[h->n_fido2_hmac_credential++] = (Fido2HmacCredential) {
.id = b,
.size = l,
@@ -889,8 +881,6 @@ static int dispatch_fido2_hmac_salt(const char *name, sd_json_variant *variant,
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
JSON_VARIANT_ARRAY_FOREACH(e, variant) {
Fido2HmacSalt *array, *k;
static const sd_json_dispatch_field fido2_hmac_salt_dispatch_table[] = {
{ "credential", SD_JSON_VARIANT_STRING, dispatch_fido2_hmac_credential, offsetof(Fido2HmacSalt, credential), SD_JSON_MANDATORY },
{ "salt", SD_JSON_VARIANT_STRING, dispatch_fido2_hmac_salt_value, 0, SD_JSON_MANDATORY },
@@ -904,12 +894,10 @@ static int dispatch_fido2_hmac_salt(const char *name, sd_json_variant *variant,
if (!sd_json_variant_is_object(e))
return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
array = reallocarray(h->fido2_hmac_salt, h->n_fido2_hmac_salt + 1, sizeof(Fido2HmacSalt));
if (!array)
if (!GREEDY_REALLOC(h->fido2_hmac_salt, h->n_fido2_hmac_salt + 1))
return log_oom();
h->fido2_hmac_salt = array;
k = h->fido2_hmac_salt + h->n_fido2_hmac_salt;
Fido2HmacSalt *k = h->fido2_hmac_salt + h->n_fido2_hmac_salt;
*k = (Fido2HmacSalt) {
.uv = -1,
.up = -1,
@@ -937,8 +925,6 @@ static int dispatch_recovery_key(const char *name, sd_json_variant *variant, sd_
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
JSON_VARIANT_ARRAY_FOREACH(e, variant) {
RecoveryKey *array, *k;
static const sd_json_dispatch_field recovery_key_dispatch_table[] = {
{ "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, 0, SD_JSON_MANDATORY },
{ "hashedPassword", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(RecoveryKey, hashed_password), SD_JSON_MANDATORY },
@@ -948,12 +934,10 @@ static int dispatch_recovery_key(const char *name, sd_json_variant *variant, sd_
if (!sd_json_variant_is_object(e))
return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
array = reallocarray(h->recovery_key, h->n_recovery_key + 1, sizeof(RecoveryKey));
if (!array)
if (!GREEDY_REALLOC(h->recovery_key, h->n_recovery_key + 1))
return log_oom();
h->recovery_key = array;
k = h->recovery_key + h->n_recovery_key;
RecoveryKey *k = h->recovery_key + h->n_recovery_key;
*k = (RecoveryKey) {};
r = sd_json_dispatch(e, recovery_key_dispatch_table, flags, k);