Skip to content

Commit 8c6121e

Browse files
committed
Fix major issues reported by SonarQube
1 parent f46af40 commit 8c6121e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

memcr.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ static void md5_final(unsigned char *md, unsigned int *len, void *ctx)
356356
}
357357
#endif
358358

359-
static void parasite_status_signal(pid_t pid, int status)
359+
static void parasite_status_signal(int status)
360360
{
361361
pthread_mutex_lock(&parasite_watch.lock);
362362
parasite_watch.changed = 1;
@@ -449,7 +449,7 @@ static void cleanup_pid(pid_t pid)
449449

450450
static int iterate_pstree(pid_t pid, int skip_self, int max_threads, int (*callback)(pid_t pid))
451451
{
452-
int ret;
452+
int ret = -1;
453453
char path[PATH_MAX];
454454
DIR *task_dir;
455455
struct dirent *ent;
@@ -1208,6 +1208,8 @@ static void get_target_rss(pid_t tid, struct vm_stats *vms)
12081208
FILE *f;
12091209
char buf[1024];
12101210

1211+
memset(vms, 0x0, sizeof(struct vm_stats));
1212+
12111213
f = fopen_proc(tid, "status");
12121214
if (!f)
12131215
return;
@@ -1526,6 +1528,7 @@ static int get_vma_pages(int pd, int md, int cd, struct vm_area *vma, int fd)
15261528
ret = parasite_write_read(cd, &req, (void*)map_buf, req.u.pagemap.len);
15271529
if (ret != req.u.pagemap.len) {
15281530
fprintf(stderr, "parasite_write_read() %d / %ld\n", ret, req.u.pagemap.len);
1531+
free(map_buf);
15291532
return -1;
15301533
}
15311534
}
@@ -1984,7 +1987,7 @@ static int peek(pid_t pid, unsigned long *addr, unsigned long *dst, size_t len)
19841987

19851988
static int poke(pid_t pid, unsigned long *addr, unsigned long *src, size_t len)
19861989
{
1987-
int ret;
1990+
int ret = -1;
19881991
int i;
19891992

19901993
/* len must be a multiple of CPU word size */
@@ -2141,7 +2144,7 @@ static void *parasite_watch_thread(void *ptr)
21412144
return NULL;
21422145
}
21432146

2144-
parasite_status_signal(pid, status);
2147+
parasite_status_signal(status);
21452148

21462149
return NULL;
21472150
}
@@ -2260,6 +2263,11 @@ static int execute_parasite_checkpoint(pid_t pid)
22602263

22612264
/* mmap space for parasite */
22622265
ret = execute_blob(&ctx, mmap_blob, mmap_blob_size, sizeof(parasite_blob), 0);
2266+
/* -4096LU is expanded to 0xFFFFF000 in 32bits,
2267+
* 0xFFFFF000 is max valid VMA address for 32bits,
2268+
* the executed mmap_blob calls mmap syscall, which returns errors as small negative integers
2269+
* so the comparison against 0xFFFFF000 will catch those errors - in fact any value being not a valid address
2270+
*/
22632271
if (ret >= -4096LU) {
22642272
fprintf(stdout, "[-] mmap failed: %lx\n", ret);
22652273
signals_unblock(pid);

0 commit comments

Comments
 (0)