Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rtla/actions: Simplify argument parsing

The actions_parse() function uses open-coded logic to extract arguments
from a string. This includes manual length checks and strncmp() calls,
which can be verbose and error-prone.

To simplify and improve the robustness of argument parsing, introduce a
new extract_arg() helper macro. This macro extracts the value from a
"key=value" pair, making the code more concise and readable.

Also, introduce STRING_LENGTH() and strncmp_static() macros to
perform compile-time calculations of string lengths and safer string
comparisons.

Refactor actions_parse() to use these new helpers, resulting in
cleaner and more maintainable code.

Signed-off-by: Wander Lairson Costa <wander@redhat.com>
Link: https://lore.kernel.org/r/20260309195040.1019085-4-wander@redhat.com
Signed-off-by: Tomas Glozar <tglozar@redhat.com>

authored by

Wander Lairson Costa and committed by
Tomas Glozar
989e5b8f b8f7f49a

+52 -15
+42 -15
tools/tracing/rtla/src/actions.c
··· 111 111 action->type = ACTION_CONTINUE; 112 112 } 113 113 114 + static inline const char *__extract_arg(const char *token, const char *opt, size_t opt_len) 115 + { 116 + const size_t tok_len = strlen(token); 117 + 118 + if (tok_len <= opt_len) 119 + return NULL; 120 + 121 + if (strncmp(token, opt, opt_len)) 122 + return NULL; 123 + 124 + return token + opt_len; 125 + } 126 + 127 + /* 128 + * extract_arg - extract argument value from option token 129 + * @token: option token (e.g., "file=trace.txt") 130 + * @opt: option name to match (e.g., "file") 131 + * 132 + * Returns pointer to argument value after "=" if token matches "opt=", 133 + * otherwise returns NULL. 134 + */ 135 + #define extract_arg(token, opt) __extract_arg(token, opt "=", STRING_LENGTH(opt "=")) 136 + 114 137 /* 115 138 * actions_parse - add an action based on text specification 116 139 */ ··· 143 120 enum action_type type = ACTION_NONE; 144 121 const char *token; 145 122 char trigger_c[strlen(trigger) + 1]; 123 + const char *arg_value; 146 124 147 125 /* For ACTION_SIGNAL */ 148 126 int signal = 0, pid = 0; ··· 176 152 if (token == NULL) 177 153 trace_output = tracefn; 178 154 else { 179 - if (strlen(token) > 5 && strncmp(token, "file=", 5) == 0) { 180 - trace_output = token + 5; 181 - } else { 155 + trace_output = extract_arg(token, "file"); 156 + if (!trace_output) 182 157 /* Invalid argument */ 183 158 return -1; 184 - } 185 159 186 160 token = strtok(NULL, ","); 187 161 if (token != NULL) ··· 191 169 case ACTION_SIGNAL: 192 170 /* Takes two arguments, num (signal) and pid */ 193 171 while (token != NULL) { 194 - if (strlen(token) > 4 && strncmp(token, "num=", 4) == 0) { 195 - if (strtoi(token + 4, &signal)) 196 - return -1; 197 - } else if (strlen(token) > 4 && strncmp(token, "pid=", 4) == 0) { 198 - if (strncmp(token + 4, "parent", 7) == 0) 199 - pid = -1; 200 - else if (strtoi(token + 4, &pid)) 172 + arg_value = extract_arg(token, "num"); 173 + if (arg_value) { 174 + if (strtoi(arg_value, &signal)) 201 175 return -1; 202 176 } else { 203 - /* Invalid argument */ 204 - return -1; 177 + arg_value = extract_arg(token, "pid"); 178 + if (arg_value) { 179 + if (strncmp_static(arg_value, "parent") == 0) 180 + pid = -1; 181 + else if (strtoi(arg_value, &pid)) 182 + return -1; 183 + } else { 184 + /* Invalid argument */ 185 + return -1; 186 + } 205 187 } 206 188 207 189 token = strtok(NULL, ","); ··· 220 194 case ACTION_SHELL: 221 195 if (token == NULL) 222 196 return -1; 223 - if (strlen(token) > 8 && strncmp(token, "command=", 8)) 197 + arg_value = extract_arg(token, "command"); 198 + if (!arg_value) 224 199 return -1; 225 - actions_add_shell(self, token + 8); 200 + actions_add_shell(self, arg_value); 226 201 break; 227 202 case ACTION_CONTINUE: 228 203 /* Takes no argument */
+10
tools/tracing/rtla/src/utils.h
··· 14 14 #define MAX_NICE 20 15 15 #define MIN_NICE -19 16 16 17 + #ifndef ARRAY_SIZE 18 + #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) 19 + #endif 20 + 21 + /* Calculate string length at compile time (excluding null terminator) */ 22 + #define STRING_LENGTH(s) (ARRAY_SIZE(s) - sizeof(*(s))) 23 + 24 + /* Compare string with static string, length determined at compile time */ 25 + #define strncmp_static(s1, s2) strncmp(s1, s2, ARRAY_SIZE(s2)) 26 + 17 27 #define container_of(ptr, type, member)({ \ 18 28 const typeof(((type *)0)->member) *__mptr = (ptr); \ 19 29 (type *)((char *)__mptr - offsetof(type, member)) ; })