Skip to content

Commit

Permalink
src: Fix incorrect use of cpu_set_t
Browse files Browse the repository at this point in the history
cpu_set_t allocated dynamically with CPU_ALLOC need to be manipulated
with CPU_*_S() family of macro, otherwise it will be assumed that the
size is equal to sizeof(cpu_set_t), leading to undefined behaviours.
  • Loading branch information
DouglasRaillard committed Mar 31, 2021
1 parent 3a95bf5 commit 36fc52d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/rt-app.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ static int create_cpuset_str(cpuset_data_t *cpu_data)
for (i = 0; i < 10000 && cpu_count; ++i) {
unsigned int n;

if (CPU_ISSET(i, cpu_data->cpuset)) {
if (CPU_ISSET_S(cpu_data->cpusetsize, i, cpu_data->cpuset)) {
--cpu_count;
if (size_needed <= (idx + 1)) {
log_error("Not enough memory for array");
Expand Down Expand Up @@ -861,7 +861,12 @@ static void set_thread_affinity(thread_data_t *data, cpuset_data_t *cpu_data)
if (actual_cpu_data->cpuset == NULL)
actual_cpu_data = &data->def_cpu_data;

if (!CPU_EQUAL(actual_cpu_data->cpuset, data->curr_cpu_data->cpuset))
if (!CPU_EQUAL_S(
actual_cpu_data->cpusetsize <
data->curr_cpu_data->cpusetsize ?
actual_cpu_data->cpusetsize :
data->curr_cpu_data->cpusetsize,
actual_cpu_data->cpuset, data->curr_cpu_data->cpuset))
{
log_debug("[%d] setting cpu affinity to CPU(s) %s", data->ind,
actual_cpu_data->cpuset_str);
Expand Down
4 changes: 2 additions & 2 deletions src/rt-app_parse_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ static void parse_cpuset_data(struct json_object *obj, cpuset_data_t *data)
data->cpuset_str = strdup(json_object_to_json_string(cpuset_obj));
data->cpusetsize = sizeof(cpu_set_t);
data->cpuset = malloc(data->cpusetsize);
CPU_ZERO(data->cpuset);
CPU_ZERO_S(data->cpusetsize, data->cpuset);
for (i = 0; i < json_object_array_length(cpuset_obj); i++) {
cpu = json_object_array_get_idx(cpuset_obj, i);
cpu_idx = json_object_get_int(cpu);
Expand All @@ -773,7 +773,7 @@ static void parse_cpuset_data(struct json_object *obj, cpuset_data_t *data)
free(data->cpuset_str);
exit(EXIT_INV_CONFIG);
}
CPU_SET(cpu_idx, data->cpuset);
CPU_SET_S(data->cpusetsize, cpu_idx, data->cpuset);
}
} else {
data->cpuset_str = strdup("-");
Expand Down

0 comments on commit 36fc52d

Please sign in to comment.