Skip to content

Commit 42b57f2

Browse files
authored
Merge pull request #674 from mihalicyn/coverity_fixes_jan2025
A bunch of Coverity warnings fixes
2 parents 19e2c99 + 28be637 commit 42b57f2

File tree

10 files changed

+48
-40
lines changed

10 files changed

+48
-40
lines changed

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ config_include = include_directories('.')
182182
add_project_arguments('-include', 'config.h', language: 'c')
183183

184184
# Binary.
185-
lxcfs_sources = files('src/lxcfs.c')
185+
lxcfs_sources = files('src/lxcfs.c', 'src/utils.c')
186186
lxcfs = executable(
187187
'lxcfs',
188188
lxcfs_sources,

src/bindings.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ static void sigusr2_toggle_virtualization(int signo, siginfo_t *info, void *extr
902902
bool set_runtime_path(const char* new_path)
903903
{
904904
if (new_path && strlen(new_path) < PATH_MAX) {
905-
strcpy(runtime_path, new_path);
905+
strlcpy(runtime_path, new_path, sizeof(runtime_path));
906906
lxcfs_info("Using runtime path %s", runtime_path);
907907
return true;
908908
} else {

src/cgroups/cgroup.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "../macro.h"
2626
#include "../memory_utils.h"
27+
#include "../utils.h"
2728
#include "cgroup.h"
2829
#include "cgroup_utils.h"
2930

src/cgroups/cgroup_utils.c

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "../macro.h"
1818
#include "../memory_utils.h"
19+
#include "../utils.h"
1920
#include "cgroup.h"
2021
#include "cgroup_utils.h"
2122

@@ -442,32 +443,6 @@ int safe_mount(const char *src, const char *dest, const char *fstype,
442443
return 0;
443444
}
444445

445-
#if !HAVE_STRLCPY
446-
size_t strlcpy(char *dest, const char *src, size_t size)
447-
{
448-
size_t ret = strlen(src);
449-
450-
if (size) {
451-
size_t len = (ret >= size) ? size - 1 : ret;
452-
memcpy(dest, src, len);
453-
dest[len] = '\0';
454-
}
455-
456-
return ret;
457-
}
458-
#endif
459-
460-
#if !HAVE_STRLCAT
461-
size_t strlcat(char *d, const char *s, size_t n)
462-
{
463-
size_t l = strnlen(d, n);
464-
if (l == n)
465-
return l + strlen(s);
466-
467-
return l + strlcpy(d + l, s, n - l);
468-
}
469-
#endif
470-
471446
FILE *fopen_cloexec(const char *path, const char *mode)
472447
{
473448
__do_close int fd = -EBADF;

src/cgroups/cgroup_utils.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,6 @@ extern bool dir_exists(const char *path);
6161
extern int safe_mount(const char *src, const char *dest, const char *fstype,
6262
unsigned long flags, const void *data, const char *rootfs);
6363

64-
#if !HAVE_STRLCPY
65-
extern size_t strlcpy(char *, const char *, size_t);
66-
#endif
67-
68-
#if !HAVE_STRLCAT
69-
extern size_t strlcat(char *d, const char *s, size_t n);
70-
#endif
71-
7264
extern FILE *fopen_cloexec(const char *path, const char *mode);
7365
extern void append_line(char **dest, size_t oldlen, char *new, size_t newlen);
7466
extern char *read_file(const char *fnam);

src/cpuset_parse.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ static int cpuset_getrange(const char *c, int *a, int *b)
3939
*/
4040
bool cpu_in_cpuset(int cpu, const char *cpuset)
4141
{
42+
if (!strlen(cpuset))
43+
return false;
44+
4245
for (const char *c = cpuset; c; c = cpuset_nexttok(c)) {
4346
int a, b, ret;
4447

src/lxcfs.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "lxcfs_fuse_compat.h"
3232
#include "macro.h"
3333
#include "memory_utils.h"
34+
#include "utils.h"
3435

3536
#define PID_FILE "/lxcfs.pid"
3637

@@ -1432,10 +1433,10 @@ int main(int argc, char *argv[])
14321433
}
14331434

14341435
if (runtime_path_arg) {
1435-
strcpy(runtime_path, runtime_path_arg);
1436+
strlcpy(runtime_path, runtime_path_arg, sizeof(runtime_path));
14361437
lxcfs_info("runtime path set to %s", runtime_path);
14371438
}
1438-
strcpy(opts->runtime_path, runtime_path);
1439+
strlcpy(opts->runtime_path, runtime_path, sizeof(opts->runtime_path));
14391440

14401441
fuse_argv[fuse_argc++] = argv[0];
14411442
if (debug)

src/utils.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ bool wait_for_sock(int sock, int timeout)
164164
{
165165
__do_close int epfd = -EBADF;
166166
struct epoll_event ev;
167-
int ret, now, starttime, deltatime;
167+
int ret;
168+
time_t now, starttime, deltatime;
168169

169170
if ((starttime = time(NULL)) < 0)
170171
return false;
@@ -713,3 +714,29 @@ bool can_access_personality(void)
713714

714715
return could_access_init_personality != 0;
715716
}
717+
718+
#if !HAVE_STRLCPY
719+
size_t strlcpy(char *dest, const char *src, size_t size)
720+
{
721+
size_t ret = strlen(src);
722+
723+
if (size) {
724+
size_t len = (ret >= size) ? size - 1 : ret;
725+
memcpy(dest, src, len);
726+
dest[len] = '\0';
727+
}
728+
729+
return ret;
730+
}
731+
#endif
732+
733+
#if !HAVE_STRLCAT
734+
size_t strlcat(char *d, const char *s, size_t n)
735+
{
736+
size_t l = strnlen(d, n);
737+
if (l == n)
738+
return l + strlen(s);
739+
740+
return l + strlcpy(d + l, s, n - l);
741+
}
742+
#endif

src/utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,12 @@ extern int get_task_personality(pid_t pid, __u32 *personality);
8282
extern bool can_access_personality(void);
8383
extern int get_host_personality(__u32 *personality);
8484

85+
#if !HAVE_STRLCPY
86+
extern size_t strlcpy(char *, const char *, size_t);
87+
#endif
88+
89+
#if !HAVE_STRLCAT
90+
extern size_t strlcat(char *d, const char *s, size_t n);
91+
#endif
92+
8593
#endif /* __LXCFS_UTILS_H */

tests/test-read.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int main(int argc, char *argv[]){
4545
sleep(1);
4646
}
4747
printf("======read sum: %d======\n", sum);
48-
close(fd);
48+
if (fd >= 0)
49+
close(fd);
4950
return 0;
5051
}

0 commit comments

Comments
 (0)