Skip to content

Commit

Permalink
Merge pull request #674 from mihalicyn/coverity_fixes_jan2025
Browse files Browse the repository at this point in the history
A bunch of Coverity warnings fixes
  • Loading branch information
stgraber authored Jan 31, 2025
2 parents 19e2c99 + 28be637 commit 42b57f2
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ config_include = include_directories('.')
add_project_arguments('-include', 'config.h', language: 'c')

# Binary.
lxcfs_sources = files('src/lxcfs.c')
lxcfs_sources = files('src/lxcfs.c', 'src/utils.c')
lxcfs = executable(
'lxcfs',
lxcfs_sources,
Expand Down
2 changes: 1 addition & 1 deletion src/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ static void sigusr2_toggle_virtualization(int signo, siginfo_t *info, void *extr
bool set_runtime_path(const char* new_path)
{
if (new_path && strlen(new_path) < PATH_MAX) {
strcpy(runtime_path, new_path);
strlcpy(runtime_path, new_path, sizeof(runtime_path));
lxcfs_info("Using runtime path %s", runtime_path);
return true;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/cgroups/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "../macro.h"
#include "../memory_utils.h"
#include "../utils.h"
#include "cgroup.h"
#include "cgroup_utils.h"

Expand Down
27 changes: 1 addition & 26 deletions src/cgroups/cgroup_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "../macro.h"
#include "../memory_utils.h"
#include "../utils.h"
#include "cgroup.h"
#include "cgroup_utils.h"

Expand Down Expand Up @@ -442,32 +443,6 @@ int safe_mount(const char *src, const char *dest, const char *fstype,
return 0;
}

#if !HAVE_STRLCPY
size_t strlcpy(char *dest, const char *src, size_t size)
{
size_t ret = strlen(src);

if (size) {
size_t len = (ret >= size) ? size - 1 : ret;
memcpy(dest, src, len);
dest[len] = '\0';
}

return ret;
}
#endif

#if !HAVE_STRLCAT
size_t strlcat(char *d, const char *s, size_t n)
{
size_t l = strnlen(d, n);
if (l == n)
return l + strlen(s);

return l + strlcpy(d + l, s, n - l);
}
#endif

FILE *fopen_cloexec(const char *path, const char *mode)
{
__do_close int fd = -EBADF;
Expand Down
8 changes: 0 additions & 8 deletions src/cgroups/cgroup_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ extern bool dir_exists(const char *path);
extern int safe_mount(const char *src, const char *dest, const char *fstype,
unsigned long flags, const void *data, const char *rootfs);

#if !HAVE_STRLCPY
extern size_t strlcpy(char *, const char *, size_t);
#endif

#if !HAVE_STRLCAT
extern size_t strlcat(char *d, const char *s, size_t n);
#endif

extern FILE *fopen_cloexec(const char *path, const char *mode);
extern void append_line(char **dest, size_t oldlen, char *new, size_t newlen);
extern char *read_file(const char *fnam);
Expand Down
3 changes: 3 additions & 0 deletions src/cpuset_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ static int cpuset_getrange(const char *c, int *a, int *b)
*/
bool cpu_in_cpuset(int cpu, const char *cpuset)
{
if (!strlen(cpuset))
return false;

for (const char *c = cpuset; c; c = cpuset_nexttok(c)) {
int a, b, ret;

Expand Down
5 changes: 3 additions & 2 deletions src/lxcfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "lxcfs_fuse_compat.h"
#include "macro.h"
#include "memory_utils.h"
#include "utils.h"

#define PID_FILE "/lxcfs.pid"

Expand Down Expand Up @@ -1432,10 +1433,10 @@ int main(int argc, char *argv[])
}

if (runtime_path_arg) {
strcpy(runtime_path, runtime_path_arg);
strlcpy(runtime_path, runtime_path_arg, sizeof(runtime_path));
lxcfs_info("runtime path set to %s", runtime_path);
}
strcpy(opts->runtime_path, runtime_path);
strlcpy(opts->runtime_path, runtime_path, sizeof(opts->runtime_path));

fuse_argv[fuse_argc++] = argv[0];
if (debug)
Expand Down
29 changes: 28 additions & 1 deletion src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ bool wait_for_sock(int sock, int timeout)
{
__do_close int epfd = -EBADF;
struct epoll_event ev;
int ret, now, starttime, deltatime;
int ret;
time_t now, starttime, deltatime;

if ((starttime = time(NULL)) < 0)
return false;
Expand Down Expand Up @@ -713,3 +714,29 @@ bool can_access_personality(void)

return could_access_init_personality != 0;
}

#if !HAVE_STRLCPY
size_t strlcpy(char *dest, const char *src, size_t size)
{
size_t ret = strlen(src);

if (size) {
size_t len = (ret >= size) ? size - 1 : ret;
memcpy(dest, src, len);
dest[len] = '\0';
}

return ret;
}
#endif

#if !HAVE_STRLCAT
size_t strlcat(char *d, const char *s, size_t n)
{
size_t l = strnlen(d, n);
if (l == n)
return l + strlen(s);

return l + strlcpy(d + l, s, n - l);
}
#endif
8 changes: 8 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ extern int get_task_personality(pid_t pid, __u32 *personality);
extern bool can_access_personality(void);
extern int get_host_personality(__u32 *personality);

#if !HAVE_STRLCPY
extern size_t strlcpy(char *, const char *, size_t);
#endif

#if !HAVE_STRLCAT
extern size_t strlcat(char *d, const char *s, size_t n);
#endif

#endif /* __LXCFS_UTILS_H */
3 changes: 2 additions & 1 deletion tests/test-read.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ int main(int argc, char *argv[]){
sleep(1);
}
printf("======read sum: %d======\n", sum);
close(fd);
if (fd >= 0)
close(fd);
return 0;
}

0 comments on commit 42b57f2

Please sign in to comment.