Skip to content

Commit 701fb43

Browse files
committed
refactor: Remove VLA usage in hwinfo and target (docker, k8s) modules
1 parent d8033d3 commit 701fb43

File tree

6 files changed

+83
-84
lines changed

6 files changed

+83
-84
lines changed

src/hwinfo.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@
3636
#include "hwinfo.h"
3737
#include "util.h"
3838

39+
/*
40+
* SYSFS_CPU_PATH stores the path leading to the available CPUs for the system.
41+
*/
42+
#define SYSFS_CPU_PATH "/sys/bus/cpu/devices"
43+
44+
/*
45+
* CPU_ID_REGEX is the regex used to extract the CPU id from its name.
46+
* CPU_ID_REGEX_EXPECTED_MATCHES is the number of expected matches from the regex. (num groups + 1)
47+
*/
48+
#define CPU_ID_REGEX "^cpu([0-9]+)$"
49+
#define CPU_ID_REGEX_EXPECTED_MATCHES 2
50+
51+
3952
static struct hwinfo_pkg *
4053
hwinfo_pkg_create(void)
4154
{
@@ -79,11 +92,11 @@ static int
7992
get_cpu_online_status(const char *cpu_dir)
8093
{
8194
int status = 1;
82-
char path[256];
95+
char path[PATH_MAX] = {0};
8396
FILE *f = NULL;
8497
char buffer[2]; /* boolean expected */
8598

86-
snprintf(path, sizeof(path), "%s/%s/online", SYSFS_CPU_PATH, cpu_dir);
99+
snprintf(path, PATH_MAX, "%s/%s/online", SYSFS_CPU_PATH, cpu_dir);
87100

88101
f = fopen(path, "r");
89102
if (f) {
@@ -105,11 +118,11 @@ static char *
105118
get_package_id(const char *cpu_dir)
106119
{
107120
FILE *f = NULL;
108-
char path[256];
121+
char path[PATH_MAX] = {0};
109122
char buffer[24]; /* log10(ULLONG_MAX) */
110123
char *id = NULL;
111124

112-
snprintf(path, sizeof(path), "%s/%s/topology/physical_package_id", SYSFS_CPU_PATH, cpu_dir);
125+
snprintf(path, PATH_MAX, "%s/%s/topology/physical_package_id", SYSFS_CPU_PATH, cpu_dir);
113126

114127
f = fopen(path, "r");
115128
if (f) {
@@ -126,13 +139,11 @@ static char *
126139
parse_cpu_id_from_name(const char *str)
127140
{
128141
regex_t re = {0};
129-
const char *expr = "^cpu([0-9]+)$";
130-
const size_t num_matches = 2; /* num groups + 1 */
131-
regmatch_t matches[num_matches];
142+
regmatch_t matches[CPU_ID_REGEX_EXPECTED_MATCHES];
132143
char *id = NULL;
133144

134-
if (!regcomp(&re, expr, REG_EXTENDED)) {
135-
if (!regexec(&re, str, num_matches, matches, 0)) {
145+
if (!regcomp(&re, CPU_ID_REGEX, REG_EXTENDED)) {
146+
if (!regexec(&re, str, CPU_ID_REGEX_EXPECTED_MATCHES, matches, 0)) {
136147
id = strndup(str + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
137148
}
138149
regfree(&re);

src/hwinfo.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@
3535
#include <stddef.h>
3636
#include <czmq.h>
3737

38-
/*
39-
* SYSFS_CPU_PATH stores the path leading to the available CPUs for the system.
40-
*/
41-
#define SYSFS_CPU_PATH "/sys/bus/cpu/devices"
4238

4339
/*
4440
* hwinfo_pkg stores information about the package.

src/target_docker.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@
3838
#include "target.h"
3939
#include "target_docker.h"
4040

41+
/*
42+
* CONTAINER_ID_REGEX is the regex used to extract the Docker container id from the cgroup path.
43+
* CONTAINER_ID_REGEX_EXPECTED_MATCHES is the number of expected matches from the regex. (num groups + 1)
44+
*/
45+
#define CONTAINER_ID_REGEX "perf_event/docker/([a-f0-9]{64})$"
46+
#define CONTAINER_ID_REGEX_EXPECTED_MATCHES 2
47+
48+
/*
49+
* CONTAINER_NAME_REGEX is the regex used to extract the Docker container name from its json configuration file.
50+
* CONTAINER_NAME_REGEX_EXPECTED_MATCHES is the number of expected matches from the regex. (num groups + 1)
51+
*/
52+
#define CONTAINER_NAME_REGEX "\"Name\":\"/([a-zA-Z0-9][a-zA-Z0-9_.-]+)\""
53+
#define CONTAINER_NAME_REGEX_EXPECTED_MATCHES 2
54+
55+
4156
int
4257
target_docker_validate(const char *cgroup_path)
4358
{
@@ -47,7 +62,7 @@ target_docker_validate(const char *cgroup_path)
4762
if (!cgroup_path)
4863
return false;
4964

50-
if (!regcomp(&re, TARGET_DOCKER_EXTRACT_CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) {
65+
if (!regcomp(&re, CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) {
5166
if (!regexec(&re, cgroup_path, 0, NULL, 0))
5267
is_docker_target = true;
5368

@@ -61,20 +76,18 @@ static char *
6176
build_container_config_path(const char *cgroup_path)
6277
{
6378
regex_t re;
64-
const size_t num_matches = 2;
65-
regmatch_t matches[num_matches];
66-
int res;
67-
char config_path_buffer[TARGET_DOCKER_CONFIG_PATH_BUFFER_SIZE];
79+
regmatch_t matches[CONTAINER_ID_REGEX_EXPECTED_MATCHES];
80+
regoff_t length;
81+
const char *id = NULL;
82+
char buffer[PATH_MAX] = {0};
6883
char *config_path = NULL;
6984

70-
if (!regcomp(&re, TARGET_DOCKER_EXTRACT_CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE)) {
71-
if (!regexec(&re, cgroup_path, num_matches, matches, 0)) {
72-
res = snprintf(config_path_buffer, TARGET_DOCKER_CONFIG_PATH_BUFFER_SIZE,
73-
"/var/lib/docker/containers/%.*s/config.v2.json",
74-
matches[1].rm_eo - matches[1].rm_so, cgroup_path + matches[1].rm_so);
75-
76-
if (res > 0 && res < TARGET_DOCKER_CONFIG_PATH_BUFFER_SIZE)
77-
config_path = strdup(config_path_buffer);
85+
if (!regcomp(&re, CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE)) {
86+
if (!regexec(&re, cgroup_path, CONTAINER_ID_REGEX_EXPECTED_MATCHES, matches, 0)) {
87+
id = cgroup_path + matches[1].rm_so;
88+
length = matches[1].rm_eo - matches[1].rm_so;
89+
snprintf(buffer, PATH_MAX, "/var/lib/docker/containers/%.*s/config.v2.json", length, id);
90+
config_path = strdup(buffer);
7891
}
7992
regfree(&re);
8093
}
@@ -90,8 +103,7 @@ target_docker_resolve_name(struct target *target)
90103
char *json = NULL;
91104
size_t json_len;
92105
regex_t re;
93-
const size_t num_matches = 2;
94-
regmatch_t matches[num_matches];
106+
regmatch_t matches[CONTAINER_NAME_REGEX_EXPECTED_MATCHES];
95107
char *target_name = NULL;
96108

97109
config_path = build_container_config_path(target->cgroup_path);
@@ -101,8 +113,8 @@ target_docker_resolve_name(struct target *target)
101113
json_file = fopen(config_path, "r");
102114
if (json_file) {
103115
if (getline(&json, &json_len, json_file) != -1) {
104-
if (!regcomp(&re, TARGET_DOCKER_CONFIG_EXTRACT_NAME_REGEX, REG_EXTENDED | REG_NEWLINE)) {
105-
if (!regexec(&re, json, num_matches, matches, 0))
116+
if (!regcomp(&re, CONTAINER_NAME_REGEX, REG_EXTENDED | REG_NEWLINE)) {
117+
if (!regexec(&re, json, CONTAINER_NAME_REGEX_EXPECTED_MATCHES, matches, 0))
106118
target_name = strndup(json + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
107119

108120
regfree(&re);

src/target_docker.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,6 @@
3434

3535
#include "target.h"
3636

37-
/*
38-
* TARGET_DOCKER_EXTRACT_CONTAINER_ID_REGEX stores the regex used to extract the container id from of a cgroup path.
39-
*/
40-
#define TARGET_DOCKER_EXTRACT_CONTAINER_ID_REGEX "perf_event/docker/([a-f0-9]{64})$"
41-
42-
/*
43-
* DOCKER_CONFIG_PATH_BUFFER_SIZE stores the max length of the path to the container config file.
44-
* /var/lib/docker/containers/756535dc6e9ab9b560f84c85063f55952273a23192641fc2756aa9721d9d1000/config.v2.json = 106
45-
*/
46-
#define TARGET_DOCKER_CONFIG_PATH_BUFFER_SIZE 128
47-
48-
/*
49-
* DOCKER_CONFIG_EXTRACT_NAME_REGEX stores the regex used to extract the name of the container from its json config file.
50-
*/
51-
#define TARGET_DOCKER_CONFIG_EXTRACT_NAME_REGEX "\"Name\":\"/([a-zA-Z0-9][a-zA-Z0-9_.-]+)\""
52-
5337
/*
5438
* target_docker_validate check if the cgroup path lead to a valid Docker target.
5539
*/

src/target_kubernetes.c

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@
3535
#include "target.h"
3636
#include "target_kubernetes.h"
3737

38+
/*
39+
* CONTAINER_ID_REGEX is the regex used to extract the Docker container id from a cgroup path.
40+
* CONTAINER_ID_REGEX_EXPECTED_MATCHES is the number of matches expected from the regex. (num groups + 1)
41+
*/
42+
#define CONTAINER_ID_REGEX \
43+
"perf_event/kubepods/" \
44+
"(besteffort/|burstable/|)" \
45+
"(pod[a-zA-Z0-9][a-zA-Z0-9.-]+)/" /* Pod ID */ \
46+
"([a-f0-9]{64})" /* Container ID */ \
47+
"(/[a-zA-Z0-9][a-zA-Z0-9.-]+|)" /* Resource group */
48+
#define CONTAINER_ID_REGEX_EXPECTED_MATCHES 5
49+
50+
/*
51+
* CONTAINER_NAME_REGEX is the regex used to extract the name of the Docker container from its json configuration file.
52+
* CONTAINER_NAME_REGEX_EXPECTED_MATCHES is the number of matches expected from the regex. (num groups + 1)
53+
*/
54+
#define CONTAINER_NAME_REGEX "\"Name\":\"/([a-zA-Z0-9][a-zA-Z0-9_.-]+)\""
55+
#define CONTAINER_NAME_REGEX_EXPECTED_MATCHES 2
56+
57+
3858
int
3959
target_kubernetes_validate(const char *cgroup_path)
4060
{
@@ -44,7 +64,7 @@ target_kubernetes_validate(const char *cgroup_path)
4464
if (!cgroup_path)
4565
return false;
4666

47-
if (!regcomp(&re, TARGET_KUBERNETES_EXTRACT_CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) {
67+
if (!regcomp(&re, CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) {
4868
if (!regexec(&re, cgroup_path, 0, NULL, 0))
4969
is_kubernetes_target = true;
5070

@@ -58,20 +78,18 @@ static char *
5878
build_container_config_path(const char *cgroup_path)
5979
{
6080
regex_t re;
61-
const size_t num_matches = 5;
62-
regmatch_t matches[num_matches];
63-
int res;
64-
char path_buffer[TARGET_KUBERNETES_CONFIG_PATH_BUFFER_SIZE];
81+
regmatch_t matches[CONTAINER_ID_REGEX_EXPECTED_MATCHES];
82+
regoff_t length;
83+
const char *id = NULL;
84+
char buffer[PATH_MAX] = {0};
6585
char *config_path = NULL;
6686

67-
if (!regcomp(&re, TARGET_KUBERNETES_EXTRACT_CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE)) {
68-
if (!regexec(&re, cgroup_path, num_matches, matches, 0)) {
69-
res = snprintf(path_buffer, TARGET_KUBERNETES_CONFIG_PATH_BUFFER_SIZE,
70-
"/var/lib/docker/containers/%.*s/config.v2.json",
71-
matches[3].rm_eo - matches[3].rm_so, cgroup_path + matches[3].rm_so);
72-
73-
if (res > 0 && res < TARGET_KUBERNETES_CONFIG_PATH_BUFFER_SIZE)
74-
config_path = strdup(path_buffer);
87+
if (!regcomp(&re, CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE)) {
88+
if (!regexec(&re, cgroup_path, CONTAINER_ID_REGEX_EXPECTED_MATCHES, matches, 0)) {
89+
id = cgroup_path + matches[3].rm_so;
90+
length = matches[3].rm_eo - matches[3].rm_so;
91+
snprintf(buffer, PATH_MAX, "/var/lib/docker/containers/%.*s/config.v2.json", length, id);
92+
config_path = strdup(buffer);
7593
}
7694
regfree(&re);
7795
}
@@ -87,8 +105,7 @@ target_kubernetes_resolve_name(struct target *target)
87105
char *json = NULL;
88106
size_t json_len;
89107
regex_t re;
90-
const size_t num_matches = 2;
91-
regmatch_t matches[num_matches];
108+
regmatch_t matches[CONTAINER_NAME_REGEX_EXPECTED_MATCHES];
92109
char *target_name = NULL;
93110

94111
config_path = build_container_config_path(target->cgroup_path);
@@ -98,8 +115,8 @@ target_kubernetes_resolve_name(struct target *target)
98115
json_file = fopen(config_path, "r");
99116
if (json_file) {
100117
if (getline(&json, &json_len, json_file) != -1) {
101-
if (!regcomp(&re, TARGET_KUBERNETES_EXTRACT_CONTAINER_NAME_REGEX, REG_EXTENDED | REG_NEWLINE)) {
102-
if (!regexec(&re, json, num_matches, matches, 0))
118+
if (!regcomp(&re, CONTAINER_NAME_REGEX, REG_EXTENDED | REG_NEWLINE)) {
119+
if (!regexec(&re, json, CONTAINER_NAME_REGEX_EXPECTED_MATCHES, matches, 0))
103120
target_name = strndup(json + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
104121

105122
regfree(&re);

src/target_kubernetes.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,6 @@
3434

3535
#include "target.h"
3636

37-
/*
38-
* TARGET_KUBERNETES_EXTRACT_CONTAINER_ID_REGEX stores the regex used to extract the Docker container id from a cgroup path.
39-
*/
40-
#define TARGET_KUBERNETES_EXTRACT_CONTAINER_ID_REGEX \
41-
"perf_event/kubepods/" \
42-
"(besteffort/|burstable/|)" \
43-
"(pod[a-zA-Z0-9][a-zA-Z0-9.-]+)/" /* Pod ID */ \
44-
"([a-f0-9]{64})" /* Container ID */ \
45-
"(/[a-zA-Z0-9][a-zA-Z0-9.-]+|)" /* Resource group */
46-
47-
/*
48-
* TARGET_KUBERNETES_EXTRACT_CONTAINER_NAME_REGEX stores the regex used to extract the name of the Docker container from
49-
* the json configuration file.
50-
*/
51-
#define TARGET_KUBERNETES_EXTRACT_CONTAINER_NAME_REGEX "\"Name\":\"/([a-zA-Z0-9][a-zA-Z0-9_.-]+)\""
52-
53-
/*
54-
* TARGET_KUBERNETES_CONFIG_PATH_BUFFER_SIZE stores the buffer size for the path to the Docker config file.
55-
*/
56-
#define TARGET_KUBERNETES_CONFIG_PATH_BUFFER_SIZE 128
57-
5837
/*
5938
* target_kubernetes_validate check if the cgroup path lead to a valid Kubernetes target.
6039
*/

0 commit comments

Comments
 (0)