fix(posix): tighten dirent shim and use nullptr

The dirent() shim's scandir() growth path used `sizeof(*next)` where the
inner type is `struct dirent **`, which clang-tidy's
bugprone-sizeof-expression flags as a pointer-to-aggregate sizeof. Spell
the element type out (`struct dirent *`) so the realloc/qsort sizes are
unambiguous on both 32- and 64-bit. Convert the remaining NULL tokens in
the shim and the lockstep test to nullptr to satisfy hicpp-use-nullptr.

Signed-off-by: Nuno Marques <n.marques21@hotmail.com>
This commit is contained in:
Nuno Marques
2026-05-07 16:37:32 -07:00
parent b6edd0706f
commit d331137c56
2 changed files with 14 additions and 14 deletions
+12 -12
View File
@@ -108,10 +108,10 @@ typedef struct px4_dir_shim {
*/
static inline DIR *opendir(const char *name)
{
if (!name) { errno = EINVAL; return NULL; }
if (!name) { errno = EINVAL; return nullptr; }
DIR *d = (DIR *)calloc(1, sizeof(DIR));
if (!d) { errno = ENOMEM; return NULL; }
if (!d) { errno = ENOMEM; return nullptr; }
/* Translate "foo" -> "foo\*" so FindFirstFile enumerates the
* directory's contents rather than matching the directory itself. */
@@ -119,14 +119,14 @@ static inline DIR *opendir(const char *name)
if (n <= 0 || (size_t)n >= sizeof(d->pattern)) {
free(d);
errno = ENAMETOOLONG;
return NULL;
return nullptr;
}
d->handle = FindFirstFileA(d->pattern, &d->find_data);
if (d->handle == INVALID_HANDLE_VALUE) {
free(d);
errno = ENOENT;
return NULL;
return nullptr;
}
d->first = 1;
d->offset = 0;
@@ -141,12 +141,12 @@ static inline DIR *opendir(const char *name)
*/
static inline struct dirent *readdir(DIR *d)
{
if (!d) { errno = EBADF; return NULL; }
if (!d) { errno = EBADF; return nullptr; }
if (d->first) {
d->first = 0;
} else if (!FindNextFileA(d->handle, &d->find_data)) {
return NULL; /* end of directory */
return nullptr; /* end of directory */
}
strncpy(d->entry.d_name, d->find_data.cFileName, NAME_MAX);
@@ -245,7 +245,7 @@ static inline int scandir(const char *dirp, struct dirent ***namelist,
return -1;
}
*namelist = NULL;
*namelist = nullptr;
DIR *dir = opendir(dirp);
@@ -255,17 +255,17 @@ static inline int scandir(const char *dirp, struct dirent ***namelist,
size_t count = 0;
size_t capacity = 0;
struct dirent **list = NULL;
struct dirent *entry = NULL;
struct dirent **list = nullptr;
struct dirent *entry = nullptr;
while ((entry = readdir(dir)) != NULL) {
while ((entry = readdir(dir)) != nullptr) {
if (select && !select(entry)) {
continue;
}
if (count == capacity) {
size_t next_capacity = capacity ? capacity * 2 : 16;
struct dirent **next = (struct dirent **)realloc(list, next_capacity * sizeof(*next));
struct dirent **next = (struct dirent **)realloc(list, next_capacity * sizeof(struct dirent *));
if (!next) {
for (size_t i = 0; i < count; ++i) {
@@ -302,7 +302,7 @@ static inline int scandir(const char *dirp, struct dirent ***namelist,
closedir(dir);
if (compar && count > 1) {
qsort(list, count, sizeof(*list), (int (*)(const void *, const void *))compar);
qsort(list, count, sizeof(struct dirent *), (int (*)(const void *, const void *))compar);
}
*namelist = list;
@@ -127,10 +127,10 @@ void test_locked_semaphore_getting_unlocked()
void test_condition_signal_wakes_without_time_advance()
{
pthread_cond_t cond;
pthread_cond_init(&cond, NULL);
pthread_cond_init(&cond, nullptr);
pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);
pthread_mutex_init(&lock, nullptr);
LockstepScheduler ls;
ls.set_absolute_time(some_time_us);