shared: make check-pointer-deref clean

Add the needed assert changes to make the code clean
for the new check-pointer-deref script.
This commit is contained in:
Michael Vogt
2026-03-31 18:30:40 +02:00
parent b50d7345a2
commit 029335ea74
23 changed files with 74 additions and 3 deletions
-1
View File
@@ -2978,7 +2978,6 @@ if spatch.found()
# Remove directories from this list as they are cleaned up.
coccinelle_exclude = [
'src/basic/',
'src/shared/',
# libc/ has no assert() or systemd-headers so leave it
'src/libc/',
# test/ has some deliberate wonky pointers, just leave excluded
+3
View File
@@ -134,6 +134,9 @@ static void remove_cgroup(Hashmap *cgroups, struct CGroupInfo *cg) {
}
static int cgroup_info_compare_func(struct CGroupInfo * const *a, struct CGroupInfo * const *b) {
assert(a);
assert(b);
return strcmp((*a)->cgroup_path, (*b)->cgroup_path);
}
+16 -2
View File
@@ -39,6 +39,7 @@ static CalendarComponent* chain_free(CalendarComponent *c) {
DEFINE_TRIVIAL_CLEANUP_FUNC(CalendarComponent*, chain_free);
CalendarSpec* calendar_spec_free(CalendarSpec *c) {
POINTER_MAY_BE_NULL(c);
if (!c)
return NULL;
@@ -57,6 +58,9 @@ CalendarSpec* calendar_spec_free(CalendarSpec *c) {
static int component_compare(CalendarComponent * const *a, CalendarComponent * const *b) {
int r;
assert(a);
assert(b);
r = CMP((*a)->start, (*b)->start);
if (r != 0)
return r;
@@ -487,6 +491,10 @@ static int parse_one_number(const char *p, const char **e, unsigned long *ret) {
char *ee = NULL;
unsigned long value;
assert(p);
assert(e);
assert(ret);
errno = 0;
value = strtoul(p, &ee, 10);
if (errno > 0)
@@ -504,6 +512,9 @@ static int parse_component_decimal(const char **p, bool usec, int *res) {
const char *e = NULL;
int r;
assert(p);
assert(res);
if (!ascii_isdigit(**p))
return -EINVAL;
@@ -569,6 +580,8 @@ static int calendarspec_from_time_t(CalendarSpec *c, time_t time) {
struct tm tm;
int r;
assert(c);
if ((usec_t) time > USEC_INFINITY / USEC_PER_SEC)
return -ERANGE;
@@ -616,9 +629,8 @@ static int calendarspec_from_time_t(CalendarSpec *c, time_t time) {
static int prepend_component(const char **p, bool usec, unsigned nesting, CalendarComponent **c) {
int r, start, stop = -1, repeat = 0;
CalendarComponent *cc;
const char *e = *p;
const char *e = *ASSERT_PTR(p);
assert(p);
assert(c);
if (nesting > CALENDARSPEC_COMPONENTS_MAX)
@@ -1218,6 +1230,8 @@ static bool matches_weekday(int weekdays_bits, const struct tm *tm, bool utc) {
struct tm t;
int k;
assert(tm);
if (weekdays_bits < 0 || weekdays_bits >= BITS_WEEKDAYS)
return true;
+2
View File
@@ -918,6 +918,8 @@ static int _hashmap_by_section_find_unused_line(
unsigned n = 0;
void *entry;
assert(ret);
HASHMAP_BASE_FOREACH_KEY(entry, cs, entries_by_section) {
if (filename && !streq(cs->filename, filename))
continue;
+4
View File
@@ -129,6 +129,8 @@ int open_credentials_dir(void) {
int get_system_credentials_dir(const char **ret) {
int r;
assert(ret);
/* Note that for system credentials the environment variable we honour is just for debugging purpose
* (unlike for the per-service credential path env var where it's key part of the protocol). */
r = get_credentials_dir_internal("SYSTEMD_SYSTEM_CREDENTIALS_DIRECTORY", ret);
@@ -142,6 +144,8 @@ int get_system_credentials_dir(const char **ret) {
int get_encrypted_system_credentials_dir(const char **ret) {
int r;
assert(ret);
r = get_credentials_dir_internal("SYSTEMD_ENCRYPTED_SYSTEM_CREDENTIALS_DIRECTORY", ret);
if (r >= 0 || r != -ENXIO)
return r;
+3
View File
@@ -1468,6 +1468,7 @@ int dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start) {
int r;
assert(p);
assert(ret);
r = dns_packet_read(p, sizeof(uint8_t), &d, start);
if (r < 0)
@@ -1498,6 +1499,7 @@ int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start) {
int r;
assert(p);
assert(ret);
r = dns_packet_read(p, sizeof(uint32_t), &d, start);
if (r < 0)
@@ -2404,6 +2406,7 @@ static bool opt_is_good(DnsResourceRecord *rr, bool *rfc6975) {
* a reply). */
assert(rr);
assert(rfc6975);
assert(rr->key->type == DNS_TYPE_OPT);
/* Check that the version is 0 */
+2
View File
@@ -1543,6 +1543,8 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
int r;
assert(a);
assert(b);
assert(t);
assert(t->sort_map);
+4
View File
@@ -142,6 +142,8 @@ int tar_strip_suffixes(const char *name, char **ret) {
const char *e;
char *s;
assert(ret);
e = endswith(name, ".tar");
if (!e)
e = endswith(name, ".tar.xz");
@@ -183,6 +185,8 @@ int raw_strip_suffixes(const char *name, char **ret) {
_cleanup_free_ char *q = NULL;
assert(ret);
q = strdup(name);
if (!q)
return -ENOMEM;
+2
View File
@@ -49,6 +49,8 @@ static int specifier_instance(char specifier, const void *data, const char *root
char *instance;
int r;
assert(ret);
r = unit_name_to_instance(i->name, &instance);
if (r < 0)
return r;
+5
View File
@@ -646,6 +646,7 @@ static int mark_symlink_for_removal(
char *n;
int r;
assert(remove_symlinks_to);
assert(p);
r = set_ensure_allocated(remove_symlinks_to, &path_hash_ops_free);
@@ -1037,6 +1038,7 @@ static int find_symlinks_in_scope(
assert(lp);
assert(info);
assert(state);
/* As we iterate over the list of search paths in lp->search_path, we may encounter "same name"
* symlinks. The ones which are "below" (i.e. have lower priority) than the unit file itself are
@@ -1842,6 +1844,8 @@ static int install_info_discover_and_check(
int r;
POINTER_MAY_BE_NULL(ret);
r = install_info_discover(ctx, lp, name_or_path, flags, ret, changes, n_changes);
if (r < 0)
return r;
@@ -3510,6 +3514,7 @@ static int query_presets(const char *name, const UnitFilePresets *presets, char
assert(name);
assert(presets);
POINTER_MAY_BE_NULL(instance_name_list);
if (!unit_name_is_valid(name, UNIT_NAME_ANY))
return -EINVAL;
+2
View File
@@ -50,6 +50,8 @@ static int get_line(JournalImporter *imp, char **line, size_t *size) {
char *c = NULL;
assert(imp);
assert(line);
assert(size);
assert(imp->state == IMPORTER_STATE_LINE);
assert(imp->offset <= imp->filled);
assert(imp->filled <= MALLOC_SIZEOF_SAFE(imp->buf));
+4
View File
@@ -53,6 +53,9 @@ bool has_local_address(const struct local_address *addresses, size_t n_addresses
static void suppress_duplicates(struct local_address *list, size_t *n_list) {
size_t old_size, new_size;
POINTER_MAY_BE_NULL(list);
assert(n_list);
/* Removes duplicate entries, assumes the list of addresses is already sorted. Updates in-place. */
if (*n_list < 2) /* list with less than two entries can't have duplicates */
@@ -88,6 +91,7 @@ static int add_local_address_full(
assert(ifindex > 0);
assert(IN_SET(family, AF_INET, AF_INET6));
assert(address);
POINTER_MAY_BE_NULL(prefsrc);
if (!GREEDY_REALLOC(*list, *n_list + 1))
return -ENOMEM;
+2
View File
@@ -33,6 +33,8 @@
#include "time-util.h"
static void cleanup_clear_loop_close(int *fd) {
assert(fd);
if (*fd < 0)
return;
+2
View File
@@ -40,6 +40,8 @@ int net_get_type_string(sd_device *device, uint16_t iftype, char **ret) {
const char *t;
char *p;
assert(ret);
if (device &&
sd_device_get_devtype(device, &t) >= 0 &&
!isempty(t))
+2
View File
@@ -79,6 +79,8 @@ int parse_path_argument(const char *path, bool suppress_root, char **arg) {
char *p;
int r;
assert(arg);
/*
* This function is intended to be used in command line parsers, to handle paths that are passed
* in. It makes the path absolute, and reduces it to NULL if omitted or root (the latter optionally).
+5
View File
@@ -874,6 +874,8 @@ int pkcs11_token_find_related_object(
CK_OBJECT_HANDLE objects[2];
CK_RV rv;
assert(ret_object);
rv = m->C_GetAttributeValue(session, prototype, attributes, ELEMENTSOF(attributes));
if (!IN_SET(rv, CKR_OK, CKR_ATTRIBUTE_TYPE_INVALID))
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve length of attributes: %s", sym_p11_kit_strerror(rv));
@@ -1072,6 +1074,9 @@ static int pkcs11_token_decrypt_data_ecc(
int r;
#endif
assert(ret_decrypted_data);
assert(ret_decrypted_data_size);
rv = m->C_GetSessionInfo(session, &session_info);
if (rv != CKR_OK)
return log_error_errno(SYNTHETIC_ERRNO(EIO),
+1
View File
@@ -100,6 +100,7 @@ int terminal_urlify(const char *url, const char *text, char **ret) {
char *n;
assert(url);
assert(ret);
/* Takes a URL and a pretty string and formats it as clickable link for the terminal. See
* https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda for details. */
+2
View File
@@ -268,6 +268,8 @@ typedef struct TodoEntry {
} TodoEntry;
static void free_todo_entries(TodoEntry **todos) {
assert(todos);
for (TodoEntry *x = *todos; x && x->dir; x++) {
closedir(x->dir);
free(x->dirname);
+2
View File
@@ -98,6 +98,8 @@ int socket_address_parse_and_warn(SocketAddress *a, const char *s) {
SocketAddress b;
int r;
assert(a);
/* Similar to socket_address_parse() but warns for IPv6 sockets when we don't support them. */
r = socket_address_parse(&b, s);
+4
View File
@@ -139,6 +139,8 @@ int specifier_id128(char specifier, const void *data, const char *root, const vo
const sd_id128_t *id = ASSERT_PTR(data);
char *n;
assert(ret);
n = new(char, SD_ID128_STRING_MAX);
if (!n)
return -ENOMEM;
@@ -151,6 +153,8 @@ int specifier_uuid(char specifier, const void *data, const char *root, const voi
const sd_id128_t *id = ASSERT_PTR(data);
char *n;
assert(ret);
n = new(char, SD_ID128_UUID_STRING_MAX);
if (!n)
return -ENOMEM;
+2
View File
@@ -71,6 +71,8 @@ int get_testdata_dir(const char *suffix, char **ret) {
const char *dir;
char *p;
assert(ret);
load_testdata_env();
/* if the env var is set, use that */
+2
View File
@@ -5038,6 +5038,8 @@ static int tpm2_calculate_seal_public(
int r;
assert(parent);
POINTER_MAY_BE_NULL(attributes);
POINTER_MAY_BE_NULL(policy);
assert(seed);
assert(secret);
assert(ret);
+3
View File
@@ -26,6 +26,9 @@ int parse_vid_range(const char *p, uint16_t *vid, uint16_t *vid_end) {
unsigned lower, upper;
int r;
assert(vid);
assert(vid_end);
r = parse_range(p, &lower, &upper);
if (r < 0)
return r;