eBPF-based security platform
treewide: drop the NULL tests before the frees that take NULL
Daniel Borkmannd8c6f9f86668
diff --git a/builtin-bpm.c b/builtin-bpm.cindex e8fcb883..44bafb63 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -614,10 +614,8 @@ static int print_package_upatches(const char *source, bool all)if (ret == -ENOENT)pr_err("bpm info upatches: package %s does not include Beefile",source);- if (beefile)- xfree(beefile, beefile_len + 1);- if (ctx.list)- xfree(ctx.list, ctx.cap);+ xfree(beefile, beefile_len + 1);+ xfree(ctx.list, ctx.cap);if (archive == temporary)unlink(temporary);return ret;@@ -682,10 +680,8 @@ static int print_package_upatch_data(const char *source, const char *member,pr_err("bpm info data: cannot decode .rodata in %s", member);out:upatch_close(&u);- if (beefile)- xfree(beefile, beefile_len + 1);- if (bytes)- xfree(bytes, upatch_len + 1);+ xfree(beefile, beefile_len + 1);+ xfree(bytes, upatch_len + 1);if (archive == temporary)unlink(temporary);return ret;diff --git a/builtin-genkey.c b/builtin-genkey.cindex 5991497e..8d55d358 100644--- a/builtin-genkey.c+++ b/builtin-genkey.c@@ -301,8 +301,7 @@ static int write_cert_der(X509 *c, const char *path)}ret = 0;out:- if (bio)- BIO_free(bio);+ BIO_free(bio);if (fd >= 0)close(fd);if (ret != 0)@@ -504,9 +503,7 @@ int cmd_genkey(int argc, const char **argv)GENKEY_ALG, keys_dir);ret = 0;out:- if (cert)- X509_free(cert);- if (pkey)- EVP_PKEY_free(pkey);+ X509_free(cert);+ EVP_PKEY_free(pkey);return ret;}diff --git a/builtin-keys.c b/builtin-keys.cindex 157496f2..799e863e 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -384,8 +384,7 @@ static int keys_list_keyring(key_serial_t ring, const struct keys_anchor *a,"frozen" : "NOT frozen");printf("\n");}- if (links)- xfree(links, n * sizeof(*links));+ xfree(links, n * sizeof(*links));return 0;}@@ -673,8 +672,7 @@ static int keys_add(int argc, const char **argv)"the initrd (shirudo-gen-initrd)", fp_hex);printf("%s\n", fp_hex);out:- if (sig)- xfree(sig, sig_sz);+ xfree(sig, sig_sz);xfree(der, der_sz);return ret ? 1 : 0;}diff --git a/builtin-status.c b/builtin-status.cindex 69ad48c1..0365adac 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -425,8 +425,7 @@ static size_t build_upatches_table(struct dirent **ups, int n_ups,map_summary_free(list, nm);r->age_sec = upatch_age_sec(r->loaded_at);}- if (vbtf)- btf__free(vbtf);+ btf__free(vbtf);*out = rows;return (size_t)n_ups;}@@ -563,10 +562,8 @@ static void keyring_view_links(struct status_keyring *k,static void keyring_view_free(struct status_keyring *k){- if (k->links)- xfree(k->links, k->n_links * sizeof(*k->links));- if (k->state)- xfree(k->state, k->n_links * sizeof(*k->state));+ xfree(k->links, k->n_links * sizeof(*k->links));+ xfree(k->state, k->n_links * sizeof(*k->state));}/* Its subject, key and dates, and the link whose key it is. */@@ -662,8 +659,7 @@ static void keyring_view_dm_verity(struct status_keyring *k,k->n_certs = 1;keyring_view_cert(k, c, der, len);}- if (der)- xfree(der, len);+ xfree(der, len);}static void keyring_cond_input(struct conditions_input *in,@@ -1624,7 +1620,6 @@ out:for (i = 0; i < n_ups; i++)libc_free(ups[i]);libc_free(ups);- if (json_buf)- xfree(json_buf, json_cap);+ xfree(json_buf, json_cap);return ret;}diff --git a/builtin-upatch.c b/builtin-upatch.cindex 7216a8f4..8dc481c3 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -646,8 +646,7 @@ out:for (j = 0; j < na; j++)libc_free(active[j]);libc_free(active);- if (jbuf)- xfree(jbuf, 65536);+ xfree(jbuf, 65536);return ret;}@@ -3499,10 +3498,8 @@ struct upatch_get_ctx {static void upatch_get_ctx_free(struct upatch_get_ctx *c){- if (c->ents)- xfree(c->ents, (size_t)c->n_recs * sizeof(*c->ents));- if (c->btf)- btf__free(c->btf);+ xfree(c->ents, (size_t)c->n_recs * sizeof(*c->ents));+ btf__free(c->btf);c->ents = NULL;c->btf = NULL;}@@ -3547,8 +3544,7 @@ static int upatch_get_collect(const char *want, struct upatch_get_ctx *c)vbtf = btf__load_vmlinux_btf();upatch_resolve_location(vbtf, want, c->location,sizeof(c->location));- if (vbtf)- btf__free(vbtf);+ btf__free(vbtf);prog_fd = bpf_prog_get_fd_by_id(linfo.prog_id);if (prog_fd < 0)diff --git a/testing/helper/test-otel-map.c b/testing/helper/test-otel-map.cindex be3b8505..cb889ac2 100644--- a/testing/helper/test-otel-map.c+++ b/testing/helper/test-otel-map.c@@ -255,8 +255,7 @@ static int print_payload(bool metric, bool counter, bool with_location)&len);if (!ret && buf)fwrite(buf, 1, len, stdout);- if (buf)- xfree(buf, JSON_MAX_BYTES);+ xfree(buf, JSON_MAX_BYTES);if (decoder_path[0])unlink(decoder_path);fixture_destroy(&f);@@ -352,8 +351,7 @@ static int print_record_size(void)&decoded, &decoded_len);if (!ret)printf("%zu\n", decoded_len);- if (decoded)- xfree(decoded, decoded_len + 1);+ xfree(decoded, decoded_len + 1);if (decoder_path[0])unlink(decoder_path);fixture_destroy(&f);diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex 69730609..041ea6e5 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -631,10 +631,8 @@ int bpm_build_parse_beefile(const __u8 *body, size_t len,spec->build_file = xmalloc(len + 1);scratch = xmalloc(len + 1);if (!spec->build_file || !scratch) {- if (scratch)- xfree(scratch, len + 1);- if (spec->build_file)- xfree(spec->build_file, len + 1);+ xfree(scratch, len + 1);+ xfree(spec->build_file, len + 1);spec->build_file = NULL;return -ENOMEM;}@@ -653,16 +651,13 @@ void bpm_build_spec_free(struct bpm_build_spec *spec)if (!spec)return;for (size_t i = 0; i < spec->n_upatches; i++) {- if (spec->upatches[i].tree.selector)- xfree(spec->upatches[i].tree.selector,- sizeof(*spec->upatches[i].tree.selector));- if (spec->upatches[i].rodata)- xfree(spec->upatches[i].rodata,- BPM_BUILD_MAX_RODATA *- sizeof(*spec->upatches[i].rodata));- }- if (spec->build_file)- xfree(spec->build_file, spec->build_file_len + 1);+ xfree(spec->upatches[i].tree.selector,+ sizeof(*spec->upatches[i].tree.selector));+ xfree(spec->upatches[i].rodata,+ BPM_BUILD_MAX_RODATA *+ sizeof(*spec->upatches[i].rodata));+ }+ xfree(spec->build_file, spec->build_file_len + 1);xfree(spec, sizeof(*spec));}@@ -847,8 +842,7 @@ static int build_rodata_apply(const struct bpm_build_spec *spec,if (!ret && rename(patched, load))ret = -errno;out:- if (value)- xfree(value, value_len + 1);+ xfree(value, value_len + 1);unlink(generated);if (ret) {unlink(patched);@@ -1250,8 +1244,7 @@ static int build_environment(const struct bpm_build_spec *spec,if (!body || uname(&un) ||iso8601(now, timestamp, sizeof(timestamp))) {- if (body)- xfree(body, BPM_BUILD_MAX_JSON);+ xfree(body, BPM_BUILD_MAX_JSON);return -EIO;}self_len = readlink("/proc/self/exe", self, sizeof(self) - 1);@@ -1659,8 +1652,7 @@ static int build_bpm_build(int argc, char **argv)out_cert:cert_free(&cert);out:- if (body)- xfree(body, body_len + 1);+ xfree(body, body_len + 1);bpm_build_spec_free(spec);return ret;usage:@@ -1728,8 +1720,7 @@ static int build_copy_package_member(const char *package, const char *member,out:if (fd >= 0)close(fd);- if (body)- xfree(body, len + 1);+ xfree(body, len + 1);return ret;}@@ -1793,8 +1784,7 @@ out:close(fd);if (ret)unlink(path);- if (body)- xfree(body, body_len + 1);+ xfree(body, body_len + 1);bpm_buffer_free(&manifest);return ret;}@@ -2087,8 +2077,7 @@ out:ret = -errno;build_stage_close(spec);}- if (body)- xfree(body, body_len + 1);+ xfree(body, body_len + 1);bpm_build_spec_free(spec);goto out_base;key_error:@@ -2097,8 +2086,7 @@ key_error:alloc_error:ret = -ENOMEM;out_base:- if (base_info.cert)- xfree(base_info.cert, base_info.cert_len + 1);+ xfree(base_info.cert, base_info.cert_len + 1);if (downloaded_package)unlink(base_package);return ret;diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.cindex aad881db..5b54cd86 100644--- a/tools/shirudo-build/bpm-publish.c+++ b/tools/shirudo-build/bpm-publish.c@@ -89,8 +89,7 @@ static int sign_detached(const void *payload, size_t len,static void sig_free(__u8 *sig){- if (sig)- xfree(sig, BPM_SIGN_MAX);+ xfree(sig, BPM_SIGN_MAX);}static int index_entry_compare(const void *left, const void *right)@@ -142,8 +141,7 @@ static int fetch_index(struct bpm_oci_client *c, const char *cert,}ret = bpm_index_parse(payload.data, payload.len, entries, n);out:- if (layers)- xfree(layers, BPM_MAX_ENTRIES * sizeof(*layers));+ xfree(layers, BPM_MAX_ENTRIES * sizeof(*layers));bpm_buffer_free(&manifest);bpm_buffer_free(&payload);bpm_buffer_free(&sig);@@ -168,7 +166,7 @@ static int push_index(struct bpm_oci_client *c, const char *repository,struct json_emit e = {};char timestamp[32], digest[72];char empty_config[] = "{}";- size_t cap, sig_len = 0;+ size_t cap = 0, sig_len = 0;time_t now = time(NULL);__u8 *sig = NULL;int n = 0, ret;@@ -282,11 +280,9 @@ static int push_index(struct bpm_oci_client *c, const char *repository,(const __u8 *)manifest, e.len, digest,sizeof(digest));out:- if (manifest)- xfree(manifest, BPM_MANIFEST_CAP);+ xfree(manifest, BPM_MANIFEST_CAP);sig_free(sig);- if (body)- xfree(body, cap);+ xfree(body, cap);xfree(entries, BPM_MAX_ENTRIES * sizeof(*entries));return ret;}@@ -463,12 +459,9 @@ static int push_package(const char *package_path, const char *raw_repository,*reference_out = xstrdup(reference);ret = *reference_out ? 0 : -ENOMEM;out:- if (key_manifest_body)- xfree(key_manifest_body, BPM_MANIFEST_CAP);- if (manifest)- xfree(manifest, BPM_MANIFEST_CAP);- if (info.cert)- xfree(info.cert, info.cert_len + 1);+ xfree(key_manifest_body, BPM_MANIFEST_CAP);+ xfree(manifest, BPM_MANIFEST_CAP);+ xfree(info.cert, info.cert_len + 1);xfree(archive, archive_len + 1);return ret;}@@ -835,19 +828,14 @@ static int cmd_bpm_tree_append(int argc, char **argv)printf("oci://%s/%s:%s %s\n", c.registry, repository, reference,digest);out:- if (manifest)- xfree(manifest, BPM_MANIFEST_CAP);+ xfree(manifest, BPM_MANIFEST_CAP);sig_free(sig);- if (payload)- xfree(payload, payload_len);- if (op)- xfree(op, sizeof(*op));+ xfree(payload, payload_len);+ xfree(op, sizeof(*op));tree_publication_free(&pub);bpm_build_spec_free(spec);- if (beefile)- xfree(beefile, len + 1);- if (info.cert)- xfree(info.cert, info.cert_len + 1);+ xfree(beefile, len + 1);+ xfree(info.cert, info.cert_len + 1);return ret;}diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex 4a1a8ded..2e42a6b0 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -329,8 +329,7 @@ out:close(fd);if (ret && wrote)unlink(output);- if (sig_buf)- xfree(sig_buf, MAX_SIG_SIZE);+ xfree(sig_buf, MAX_SIG_SIZE);upatch_close(&mod);upatch_close(&src);return ret;@@ -419,8 +418,7 @@ out:close(fd);if (created)unlink(modified);- if (candidate)- xfree(candidate, src.map_size);+ xfree(candidate, src.map_size);upatch_close(&src);return ret;}@@ -1719,8 +1717,7 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,out:if (fd >= 0)close(fd);- if (loader_data_buf)- xfree(loader_data_buf, loader_data_len);+ xfree(loader_data_buf, loader_data_len);free_decoder_set(&decoders);xfree(signed_data, signed_data_len);xfree(decoder_blob, decoder_blob_len);diff --git a/tools/shirudo-build/vouch.c b/tools/shirudo-build/vouch.cindex e12fd8ef..2a9c9672 100644--- a/tools/shirudo-build/vouch.c+++ b/tools/shirudo-build/vouch.c@@ -212,12 +212,9 @@ int build_keys_vouch(int argc, char **argv)own_hex, output);printf("%s\n", fp_hex);out:- if (io.signature)- xfree(io.signature, VOUCH_SIG_MAX);- if (payload)- xfree(payload, payload_sz);- if (own)- xfree(own, own_sz);+ xfree(io.signature, VOUCH_SIG_MAX);+ xfree(payload, payload_sz);+ xfree(own, own_sz);xfree(der, der_sz);return ret ? 1 : 0;}diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex 97bcc284..f38e585e 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -108,8 +108,7 @@ void bpm_oci_client_clear(struct bpm_oci_client *c)void bpm_buffer_free(struct bpm_buffer *b){- if (b->data)- xfree(b->data, b->cap);+ xfree(b->data, b->cap);*b = (struct bpm_buffer){};}diff --git a/util/bpm_package.c b/util/bpm_package.cindex 2ac93f87..2b75b6d4 100644--- a/util/bpm_package.c+++ b/util/bpm_package.c@@ -836,8 +836,7 @@ int bpm_tree_fetch_publication(struct bpm_oci_client *c,}ret = tree_parse((const char *)payload.data, payload.len, pub);out:- if (layers)- xfree(layers, BPM_MAX_ENTRIES * sizeof(*layers));+ xfree(layers, BPM_MAX_ENTRIES * sizeof(*layers));bpm_buffer_free(&manifest);bpm_buffer_free(&payload);bpm_buffer_free(&sig);diff --git a/util/bpm_tree.c b/util/bpm_tree.cindex 55c1dfe0..56828109 100644--- a/util/bpm_tree.c+++ b/util/bpm_tree.c@@ -383,9 +383,8 @@ void tree_publication_free(struct tree_publication *pub){if (!pub)return;- if (pub->operations)- xfree(pub->operations,- (size_t)pub->operations_capacity * sizeof(*pub->operations));+ xfree(pub->operations,+ (size_t)pub->operations_capacity * sizeof(*pub->operations));*pub = (struct tree_publication){};}diff --git a/util/keyring.c b/util/keyring.cindex 4b5360a8..a28c3520 100644--- a/util/keyring.c+++ b/util/keyring.c@@ -296,12 +296,9 @@ int keyring_holds_exactly(key_serial_t keyring,}ret = 1;out:- if (serials)- xfree(serials, cap);- if (want)- xfree(want, n * sizeof(*want));- if (matched)- xfree(matched, n * sizeof(*matched));+ xfree(serials, cap);+ xfree(want, n * sizeof(*want));+ xfree(matched, n * sizeof(*matched));return ret;}diff --git a/util/keys.c b/util/keys.cindex a2480d0a..ac1a72f3 100644--- a/util/keys.c+++ b/util/keys.c@@ -450,8 +450,7 @@ static void candidates_free(struct keys_candidate *cand, size_t n)size_t i;for (i = 0; i < n; i++) {- if (cand[i].der)- xfree(cand[i].der, cand[i].der_sz);+ xfree(cand[i].der, cand[i].der_sz);}xfree(cand, KEYS_SCAN_MAX * sizeof(*cand));}diff --git a/util/tls_openssl.c b/util/tls_openssl.cindex 4132c699..b0603e57 100644--- a/util/tls_openssl.c+++ b/util/tls_openssl.c@@ -89,12 +89,9 @@ static bool tls_classical_host_allowed(CURL *curl)allowlist = end + 1;}out:- if (host)- curl_free(host);- if (scheme)- curl_free(scheme);- if (url)- curl_url_cleanup(url);+ curl_free(host);+ curl_free(scheme);+ curl_url_cleanup(url);return allowed;}diff --git a/util/upatch_decode.c b/util/upatch_decode.cindex ff2cd5b0..388680e6 100644--- a/util/upatch_decode.c+++ b/util/upatch_decode.c@@ -1266,8 +1266,7 @@ static int run_lua_decoder(const struct upatch_decoder_view *d,jo.cap = 0;out_close:- if (jo.buf)- xfree(jo.buf, jo.cap);+ xfree(jo.buf, jo.cap);lua_sethook(L, NULL, 0, 0);current_lua_budget = NULL;lua_close(L);diff --git a/util/upatch_maps.c b/util/upatch_maps.cindex 15f23f63..0ec8e9e0 100644--- a/util/upatch_maps.c+++ b/util/upatch_maps.c@@ -56,8 +56,7 @@ static void resolve_map_location(struct map_view *v)vbtf = NULL;upatch_resolve_location(vbtf, v->upatch,v->location, sizeof(v->location));- if (vbtf)- btf__free(vbtf);+ btf__free(vbtf);}#define SERIAL_MAX_BYTES \@@ -1423,8 +1422,7 @@ int upatch_maps_list_all(struct map_summary **out,void map_summary_free(struct map_summary *list, size_t n){- if (list)- xfree(list, n * sizeof(*list));+ xfree(list, n * sizeof(*list));}void upatch_maps_print_summary(const struct map_summary *list,diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.cindex 87e88ff1..145bca9c 100644--- a/util/upatch_maps_otel.c+++ b/util/upatch_maps_otel.c@@ -1579,8 +1579,7 @@ static int otel_post(struct otel_state *st, const char *json, size_t json_len)retry_wait(attempt, response.retry_after,response.retry_after_valid);}- if (compressed)- xfree(compressed, compressed_cap);+ xfree(compressed, compressed_cap);return ret;}@@ -1775,8 +1774,7 @@ int otel_send_log_json(const struct otel_opts *opts,&payload, &payload_len, &alloc_len);if (!ret)ret = otel_post(state, payload, payload_len);- if (payload)- xfree(payload, alloc_len);+ xfree(payload, alloc_len);otel_close(state);return ret;}@@ -2536,8 +2534,7 @@ static int otel_s3_emit(void *state, const struct map_view *v)goto out;ret = otel_post(st->otel, payload, payload_len);out:- if (payload)- xfree(payload, S3_REFERENCE_MAX);+ xfree(payload, S3_REFERENCE_MAX);if (snapshot.file)fclose(snapshot.file);return ret;diff --git a/util/upatch_rodata.c b/util/upatch_rodata.cindex ea41b8da..fa24ab7f 100644--- a/util/upatch_rodata.c+++ b/util/upatch_rodata.c@@ -937,7 +937,6 @@ int upatch_rodata_print(const struct upatch *u, const char *path,ret = json ? print_rodata_json(u, path, &view) :print_rodata_text(u, path, &view);out:- if (view.btf)- btf__free(view.btf);+ btf__free(view.btf);return ret;}