Skip to content

Commit b3910a7

Browse files
walaclenticularis39
authored andcommitted
rtla: Enforce exact match for time unit suffixes
The parse_ns_duration() function currently uses prefix matching for detecting time units. This approach is problematic as it silently accepts malformed strings such as "100nsx" or "100us_invalid" by ignoring the trailing characters, leading to potential configuration errors. Introduce a match_time_unit() helper that checks the suffix matches exactly and is followed by either end-of-string or a ':' delimiter. The ':' is needed because parse_ns_duration() is also called from get_long_ns_after_colon() when parsing SCHED_DEADLINE priority specifications in the format "d:runtime:period" (e.g., "d:10ms:100ms"). A plain strcmp() would reject valid deadline strings because the suffix "ms" is followed by ":100ms", not end-of-string. Similarly, strncmp_static() would fail because ARRAY_SIZE() includes the NUL terminator, making it equivalent to strcmp() for this comparison. The match_time_unit() helper solves both problems: it rejects malformed input like "100msx" while correctly handling the colon-delimited deadline format. Signed-off-by: Wander Lairson Costa <wander@redhat.com> Link: https://lore.kernel.org/r/20260309195040.1019085-13-wander@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent 265905d commit b3910a7

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

tools/tracing/rtla/src/utils.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,21 @@ long parse_seconds_duration(char *val)
213213
return t;
214214
}
215215

216+
/*
217+
* match_time_unit - check if str starts with unit followed by end-of-string or ':'
218+
*
219+
* This allows the time unit parser to work both in standalone duration strings
220+
* like "100ms" and in colon-delimited SCHED_DEADLINE specifications like
221+
* "d:10ms:100ms", while still rejecting malformed input like "100msx".
222+
*/
223+
static bool match_time_unit(const char *str, const char *unit)
224+
{
225+
size_t len = strlen(unit);
226+
227+
return strncmp(str, unit, len) == 0 &&
228+
(str[len] == '\0' || str[len] == ':');
229+
}
230+
216231
/*
217232
* parse_ns_duration - parse duration with ns/us/ms/s converting it to nanoseconds
218233
*/
@@ -224,15 +239,15 @@ long parse_ns_duration(char *val)
224239
t = strtol(val, &end, 10);
225240

226241
if (end) {
227-
if (!strncmp(end, "ns", 2)) {
242+
if (match_time_unit(end, "ns")) {
228243
return t;
229-
} else if (!strncmp(end, "us", 2)) {
244+
} else if (match_time_unit(end, "us")) {
230245
t *= 1000;
231246
return t;
232-
} else if (!strncmp(end, "ms", 2)) {
247+
} else if (match_time_unit(end, "ms")) {
233248
t *= 1000 * 1000;
234249
return t;
235-
} else if (!strncmp(end, "s", 1)) {
250+
} else if (match_time_unit(end, "s")) {
236251
t *= 1000 * 1000 * 1000;
237252
return t;
238253
}

0 commit comments

Comments
 (0)