diff --git a/guest-agent/guest-scale-agent-2.0/guest_scale_agent.c b/guest-agent/guest-scale-agent-2.0/guest_scale_agent.c index 6863ee50..39c5e62c 100644 --- a/guest-agent/guest-scale-agent-2.0/guest_scale_agent.c +++ b/guest-agent/guest-scale-agent-2.0/guest_scale_agent.c @@ -130,14 +130,17 @@ int online_cpu(unsigned cpu) rc = read(fd, &val, 1); if (rc != 1){ ERR_LOG("can't read cpu online value: %m"); + close(fd); return -1; } if (val == '1') { ERR_LOG("cpu %d is already online", cpu); + close(fd); return 0; } val = '1'; rc = write(fd, &val, 1); + close(fd); if (rc != 1){ ERR_LOG("can't set cpu %d online", cpu); return -1; @@ -160,14 +163,17 @@ int offline_cpu(unsigned cpu) rc = read(fd, &val, 1); if (rc != 1){ ERR_LOG("can't read cpu online value: %m"); + close(fd); return -1; } if (val == '0') { ERR_LOG("cpu %d is already offline\n", cpu); + close(fd); return 0; } val = '0'; rc = write(fd, &val, 1); + close(fd); if (rc != 1){ ERR_LOG("can't set cpu %d offline", cpu); return -1; @@ -189,6 +195,7 @@ int get_highest_online_cpu(void) } rc = read(fd, buf, sizeof(buf)); + close(fd); if (rc < 2) { ERR_LOG("error parsing /sys/devices/system/cpu/online, too few chars"); return -1; @@ -304,6 +311,7 @@ pick_cpu: // no need to release jobj_array as its ownership is transferred to jobj_response struct json_object *jobj_array = new_json_obj_from_array(current_online_cpus); json_object_object_add(jobj_response, ONLINE_CPUS, jobj_array); + free(current_online_cpus); return; failed: @@ -374,7 +382,7 @@ void cpu_scale_up(json_object *jobj_request, // no need to release jobj_array as its ownership is transferred to jobj_response struct json_object *jobj_array = new_json_obj_from_array(current_online_cpus); json_object_object_add(jobj_response, ONLINE_CPUS, jobj_array); - + free(current_online_cpus); return; failed: diff --git a/guest-agent/guest-scale-agent-2.0/parser.c b/guest-agent/guest-scale-agent-2.0/parser.c index 3eaef1c1..6a4e2068 100644 --- a/guest-agent/guest-scale-agent-2.0/parser.c +++ b/guest-agent/guest-scale-agent-2.0/parser.c @@ -65,15 +65,15 @@ struct online_cpus *range_to_array(const char *range) struct online_cpus *cpuarray = (struct online_cpus *) malloc(BUFLEN); int start, end; int inrange = 0; - char *token, *tmp; + char *token, *tmp, *tobe_free; int done = 0; - tmp = strdup(range); - strcpy(tmp, range); - token = tmp; + tobe_free = strdup(range); + token = tmp = tobe_free; if (*tmp == '\0') { /* empty string, no online cpus */ cpuarray->numcpus = 0; + free(tobe_free); return cpuarray; } @@ -125,9 +125,11 @@ struct online_cpus *range_to_array(const char *range) break; } cpuarray->numcpus = end+1; + free(tobe_free); return cpuarray; error: free(cpuarray); + free(tobe_free); return 0; }