Skip to content

Commit

Permalink
[lib] Use xalloc.c wrapper functions for memory allocation
Browse files Browse the repository at this point in the history
Signed-off-by: David Cantrell <dcantrell@redhat.com>
  • Loading branch information
dcantrell committed May 23, 2024
1 parent 0477c56 commit fc7e158
Show file tree
Hide file tree
Showing 35 changed files with 149 additions and 316 deletions.
2 changes: 1 addition & 1 deletion include/rpminspect.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ bool init_rebaseable(struct rpminspect *);
bool init_politics(struct rpminspect *ri);
bool init_security(struct rpminspect *ri);
bool init_icons(struct rpminspect *ri);
struct rpminspect *calloc_rpminspect(struct rpminspect *);
struct rpminspect *xalloc_rpminspect(struct rpminspect *);
struct rpminspect *init_rpminspect(struct rpminspect *, const char *, const char *);

/* free.c */
Expand Down
8 changes: 3 additions & 5 deletions lib/abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ abi_t *read_abi(const char *vendor_data_dir, const char *product_release)

/* package is not found, add it to the table */
if (entry == NULL) {
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);

entry = xalloc(sizeof(*entry));
entry->pkg = strdup(pkg->data);
assert(entry->pkg != NULL);

Expand Down Expand Up @@ -188,11 +186,11 @@ string_list_t *get_abidiff_suppressions(const struct rpminspect *ri, const char
TAILQ_FOREACH(file, peer->after_files, items) {
if (!strcmp(file->localpath, suppression_file)) {
if (list == NULL) {
list = calloc(1, sizeof(*list));
list = xalloc(sizeof(*list));
TAILQ_INIT(list);
}

entry = calloc(1, sizeof(*entry));
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "%s %s", ABI_SUPPRESSIONS, file->fullpath);
assert(entry->data != NULL);
TAILQ_INSERT_TAIL(list, entry, items);
Expand Down
3 changes: 1 addition & 2 deletions lib/abspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ char *abspath(const char *path)
assert(tokens != NULL);

/* our new path elements */
newpath = calloc(1, sizeof(*newpath));
assert(newpath != NULL);
newpath = xalloc(sizeof(*newpath));
TAILQ_INIT(newpath);

/* handle each part of the path */
Expand Down
5 changes: 2 additions & 3 deletions lib/builds.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ static int download_build(struct rpminspect *ri, const struct koji_build *build)
}

/* Initialize a string list. */
filter = calloc(1, sizeof(*filter));
assert(filter != NULL);
filter = xalloc(sizeof(*filter));
TAILQ_INIT(filter);

if (p->strarray_foreach(ctx, "filter", "rpms", filter_cb, filter)) {
Expand Down Expand Up @@ -588,7 +587,7 @@ static int download_task(struct rpminspect *ri, struct koji_task *task)
}

len = strlen(dst);
dst = realloc(dst, len + strlen(pkg) + 2);
dst = xrealloc(dst, len + strlen(pkg) + 2);
tail = dst + len;
tail = stpcpy(tail, "/");
(void) stpcpy(tail, pkg);
Expand Down
7 changes: 1 addition & 6 deletions lib/checksums.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,7 @@ char *compute_checksum(const char *filename, mode_t *st_mode, int type)
}

/* this is our human readable digest, caller must free */
ret = calloc(len + 1, sizeof(char *));

if (ret == NULL) {
warn("*** calloc");
return NULL;
}
ret = xcalloc(len + 1, sizeof(char *));

for (i = 0; i < len; ++i) {
sprintf(&ret[i*2], "%02x", (unsigned int) digest[i]);
Expand Down
23 changes: 6 additions & 17 deletions lib/delta.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ static int fill_mmfile(mmfile_t *mf, const char *file)

/* extra byte for the \0 */
size = sb.st_size;
buf = calloc(1, size + 1);

if (buf == NULL) {
warn("*** malloc");
return 1;
}

buf = xalloc(size + 1);
mf->ptr = buf;
mf->ptr[size] = '\0';
mf->size = size;
Expand Down Expand Up @@ -105,19 +99,15 @@ static int delta_out(void *priv, mmbuffer_t *mb, int nbuf)
}

/* capture the line */
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));

if ((mb[i].size > 1) && mb[i].ptr != NULL) {
if (prefix) {
entry->data = calloc(1, strlen(prefix) + mb[i].size + 1);
assert(entry->data != NULL);

entry->data = xalloc(strlen(prefix) + mb[i].size + 1);
end = stpcpy(entry->data, prefix);
end = strncpy(end, mb[i].ptr, mb[i].size);
} else {
entry->data = calloc(1, mb[i].size + 1);
assert(entry->data != NULL);
entry->data = xalloc(mb[i].size + 1);

entry->data = strncpy(entry->data, mb[i].ptr, mb[i].size);
}
Expand All @@ -127,7 +117,7 @@ static int delta_out(void *priv, mmbuffer_t *mb, int nbuf)

assert(entry->data != NULL);
entry->data[strcspn(entry->data, "\n")] = '\0';
entry->data = realloc(entry->data, strlen(entry->data) + 1);
entry->data = xrealloc(entry->data, strlen(entry->data) + 1);
TAILQ_INSERT_TAIL(list, entry, items);
prefix = NULL;
}
Expand Down Expand Up @@ -165,8 +155,7 @@ char *get_file_delta(const char *a, const char *b)
memset(&xecfg, 0, sizeof(xecfg));
memset(&ecb, 0, sizeof(ecb));

list = calloc(1, sizeof(*list));
assert(list != NULL);
list = xalloc(sizeof(*list));
TAILQ_INIT(list);

xpp.flags = 0;
Expand Down
9 changes: 2 additions & 7 deletions lib/deprules.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ static deprule_list_t *gather_deprules_by_type(deprule_list_t *rules, Header hdr

if (headerGet(hdr, rtag, req, flags) && headerGet(hdr, otag, op, flags) && headerGet(hdr, vtag, ver, flags)) {
if (deprules == NULL) {
deprules = calloc(1, sizeof(*deprules));
assert(deprules != NULL);
deprules = xalloc(sizeof(*deprules));
TAILQ_INIT(deprules);
}

Expand Down Expand Up @@ -146,14 +145,10 @@ static deprule_list_t *gather_deprules_by_type(deprule_list_t *rules, Header hdr

v = rpmtdGetString(ver);

deprule_entry = calloc(1, sizeof(*deprule_entry));
assert(deprule_entry != NULL);

deprule_entry = xalloc(sizeof(*deprule_entry));
deprule_entry->type = type;

deprule_entry->requirement = strdup(r);
assert(deprule_entry->requirement != NULL);

deprule_entry->op = get_dep_operator(*(rpmtdGetUint32(op)));

if (!strcmp(v, "")) {
Expand Down
60 changes: 20 additions & 40 deletions lib/diags.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ string_list_t *gather_diags(struct rpminspect *ri, const char *progname, const c
assert(progver != NULL);

/* initialize a new list */
list = calloc(1, sizeof(*list));
assert(list != NULL);
list = xalloc(sizeof(*list));
TAILQ_INIT(list);

/* start by adding info about ourself */
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "%s version %s", progname, progver);
TAILQ_INSERT_TAIL(list, entry, items);

Expand All @@ -65,41 +63,35 @@ string_list_t *gather_diags(struct rpminspect *ri, const char *progname, const c
*/

/* zlib */
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "zlib version %s", zlibVersion());
TAILQ_INSERT_TAIL(list, entry, items);

#ifdef _HAVE_MAGIC_VERSION
/* libmagic */
/* older versions of libmagic lack this function */
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "libmagic version %d", magic_version());
TAILQ_INSERT_TAIL(list, entry, items);
#endif

/* libclamav */
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "libclamav version %s", cl_retver());
TAILQ_INSERT_TAIL(list, entry, items);

/* librpm */
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "librpm version %s", RPMVERSION);
TAILQ_INSERT_TAIL(list, entry, items);

/* libxml */
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "libxml version %s", xmlParserVersion);
TAILQ_INSERT_TAIL(list, entry, items);

/* json-c */
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "json-c version %s", json_c_version());
TAILQ_INSERT_TAIL(list, entry, items);

Expand All @@ -117,8 +109,7 @@ string_list_t *gather_diags(struct rpminspect *ri, const char *progname, const c
tmp = list_to_string(details, " "); /* rejoin remaining details */
list_free(details, free);

entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "libcurl version %s (%s)", ver, tmp);
TAILQ_INSERT_TAIL(list, entry, items);

Expand All @@ -140,47 +131,41 @@ string_list_t *gather_diags(struct rpminspect *ri, const char *progname, const c
tmp = list_to_string(details, " ");
list_free(details, free);

entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "libarchive version %s (%s)", ver, tmp);
TAILQ_INSERT_TAIL(list, entry, items);

free(ver);
free(tmp);
#elif _HAVE_ARCHIVE_VERSION_STRING
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "%s", archive_version_string());
TAILQ_INSERT_TAIL(list, entry, items);
#endif

/* libyaml */
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "libyaml version %s", yaml_get_version_string());
TAILQ_INSERT_TAIL(list, entry, items);

#ifndef NO_OPENSSL_VERSION_FUNCTION
/* openssl */
tmp = strreplace(OpenSSL_version(OPENSSL_VERSION), "OpenSSL ", "OpenSSL version ");
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "%s", tmp);
TAILQ_INSERT_TAIL(list, entry, items);
free(tmp);
#endif

/* xmlrpc-c */
xmlrpc_client_version(&major, &minor, &update);
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "xmlrpc-c version %u.%u.%u", major, minor, update);
TAILQ_INSERT_TAIL(list, entry, items);

#ifdef _WITH_LIBANNOCHECK
/* libannocheck */
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "libannocheck version %u", libannocheck_get_version());
TAILQ_INSERT_TAIL(list, entry, items);
#endif
Expand Down Expand Up @@ -214,8 +199,7 @@ string_list_t *gather_diags(struct rpminspect *ri, const char *progname, const c
assert(ver != NULL);
}

entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "%s", ver);
TAILQ_INSERT_TAIL(list, entry, items);

Expand Down Expand Up @@ -247,8 +231,7 @@ string_list_t *gather_diags(struct rpminspect *ri, const char *progname, const c
assert(ver != NULL);
}

entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "%s", ver);
TAILQ_INSERT_TAIL(list, entry, items);

Expand Down Expand Up @@ -280,8 +263,7 @@ string_list_t *gather_diags(struct rpminspect *ri, const char *progname, const c
assert(ver != NULL);
}

entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "%s", ver);
TAILQ_INSERT_TAIL(list, entry, items);

Expand Down Expand Up @@ -312,8 +294,7 @@ string_list_t *gather_diags(struct rpminspect *ri, const char *progname, const c
assert(ver != NULL);
}

entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "%s", ver);
TAILQ_INSERT_TAIL(list, entry, items);

Expand Down Expand Up @@ -344,8 +325,7 @@ string_list_t *gather_diags(struct rpminspect *ri, const char *progname, const c
assert(ver != NULL);
}

entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry = xalloc(sizeof(*entry));
xasprintf(&entry->data, "udevadm version %s", ver);
TAILQ_INSERT_TAIL(list, entry, items);

Expand Down
12 changes: 4 additions & 8 deletions lib/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ rpmfile_t *extract_rpm(struct rpminspect *ri, const char *pkg, Header hdr, const
goto cleanup;
}

path_entry = calloc(1, sizeof(*path_entry));
assert(path_entry != NULL);
path_entry = xalloc(sizeof(*path_entry));
path_entry->path = strdup(rpm_path);
path_entry->index = i;
HASH_ADD_KEYPTR(hh, path_table, path_entry->path, strlen(path_entry->path), path_entry);
Expand Down Expand Up @@ -252,8 +251,7 @@ rpmfile_t *extract_rpm(struct rpminspect *ri, const char *pkg, Header hdr, const
}

/* Allocate space for the return value */
file_list = calloc(1, sizeof(rpmfile_t));
assert(file_list != NULL);
file_list = xalloc(sizeof(rpmfile_t));
TAILQ_INIT(file_list);

while ((archive_result = archive_read_next_header(archive, &entry)) != ARCHIVE_EOF) {
Expand Down Expand Up @@ -282,8 +280,7 @@ rpmfile_t *extract_rpm(struct rpminspect *ri, const char *pkg, Header hdr, const
}

/* Create a new rpmfile_entry_t for this file */
file_entry = calloc(1, sizeof(rpmfile_entry_t));
assert(file_entry != NULL);
file_entry = xalloc(sizeof(rpmfile_entry_t));

file_entry->rpm_header = hdr;
file_entry->idx = path_entry->index;
Expand Down Expand Up @@ -445,8 +442,7 @@ static struct file_data *files_to_table(rpmfile_t *list)
assert(iter);

TAILQ_FOREACH(iter, list, items) {
fentry = calloc(1, sizeof(*fentry));
assert(fentry != NULL);
fentry = xalloc(sizeof(*fentry));
fentry->path = iter->localpath;
fentry->rpmfile = iter;
HASH_ADD_KEYPTR(hh, table, fentry->path, strlen(fentry->path), fentry);
Expand Down
Loading

0 comments on commit fc7e158

Please sign in to comment.