Skip to content

Commit eced518

Browse files
authored
Fix major issues reported by SonarQube (LibertyGlobal#109)
1 parent a5a8f2f commit eced518

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

memcr.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
#define ARCH_NAME "unknown"
7474
#endif
7575

76+
/* from kernel include/linux/err.h */
77+
#define MAX_ERRNO 4095
78+
#define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
79+
7680
#define NT_PRSTATUS 1
7781

7882
#define __round_mask(x, y) ((__typeof__(x))((y)-1))
@@ -356,7 +360,7 @@ static void md5_final(unsigned char *md, unsigned int *len, void *ctx)
356360
}
357361
#endif
358362

359-
static void parasite_status_signal(pid_t pid, int status)
363+
static void parasite_status_signal(int status)
360364
{
361365
pthread_mutex_lock(&parasite_watch.lock);
362366
parasite_watch.changed = 1;
@@ -449,12 +453,14 @@ static void cleanup_pid(pid_t pid)
449453

450454
static int iterate_pstree(pid_t pid, int skip_self, int max_threads, int (*callback)(pid_t pid))
451455
{
452-
int ret;
456+
int ret = -1;
453457
char path[PATH_MAX];
454458
DIR *task_dir;
455459
struct dirent *ent;
456460
int nr_threads = 0;
457461

462+
assert(callback != NULL);
463+
458464
snprintf(path, sizeof(path), "/proc/%d/task", pid);
459465
task_dir = opendir(path);
460466
if (!task_dir) {
@@ -466,6 +472,7 @@ static int iterate_pstree(pid_t pid, int skip_self, int max_threads, int (*callb
466472
pid_t tid;
467473
char *eptr;
468474

475+
ret = 0;
469476
tid = strtoul(ent->d_name, &eptr, 0);
470477
if (*eptr != '\0')
471478
continue;
@@ -1208,6 +1215,8 @@ static void get_target_rss(pid_t tid, struct vm_stats *vms)
12081215
FILE *f;
12091216
char buf[1024];
12101217

1218+
memset(vms, 0x0, sizeof(struct vm_stats));
1219+
12111220
f = fopen_proc(tid, "status");
12121221
if (!f)
12131222
return;
@@ -1526,6 +1535,7 @@ static int get_vma_pages(int pd, int md, int cd, struct vm_area *vma, int fd)
15261535
ret = parasite_write_read(cd, &req, (void*)map_buf, req.u.pagemap.len);
15271536
if (ret != req.u.pagemap.len) {
15281537
fprintf(stderr, "parasite_write_read() %d / %ld\n", ret, req.u.pagemap.len);
1538+
free(map_buf);
15291539
return -1;
15301540
}
15311541
}
@@ -1984,7 +1994,7 @@ static int peek(pid_t pid, unsigned long *addr, unsigned long *dst, size_t len)
19841994

19851995
static int poke(pid_t pid, unsigned long *addr, unsigned long *src, size_t len)
19861996
{
1987-
int ret;
1997+
int ret = -1;
19881998
int i;
19891999

19902000
/* len must be a multiple of CPU word size */
@@ -2141,7 +2151,7 @@ static void *parasite_watch_thread(void *ptr)
21412151
return NULL;
21422152
}
21432153

2144-
parasite_status_signal(pid, status);
2154+
parasite_status_signal(status);
21452155

21462156
return NULL;
21472157
}
@@ -2260,7 +2270,8 @@ static int execute_parasite_checkpoint(pid_t pid)
22602270

22612271
/* mmap space for parasite */
22622272
ret = execute_blob(&ctx, mmap_blob, mmap_blob_size, sizeof(parasite_blob), 0);
2263-
if (ret >= -4096LU) {
2273+
/* the executed mmap_blob calls directly mmap syscall, which returns errors in range -1 to -4095 */
2274+
if (IS_ERR_VALUE(ret)) {
22642275
fprintf(stdout, "[-] mmap blob failed: %lx\n", ret);
22652276
signals_unblock(pid);
22662277
ctx_restore(pid);

0 commit comments

Comments
 (0)