Thread View: linux.kernel.git
1 messages
1 total messages
Started by Andy Koppe
Sun, 22 Nov 2026 07:06
Re: [PATCH v4 1/5] utf8: introduce bounds for display_mode_esc_sequence_len()
Author: Andy Koppe
Date: Sun, 22 Nov 2026 07:06
Date: Sun, 22 Nov 2026 07:06
45 lines
13189 bytes
13189 bytes
Dirk Gouders wrote:
> + * Return the length of the ANSI display mode escape sequence (e.g.,
> + * "\033[1;31m") that appears at the beginning of the string starting at start,
> + * or 0 if the string does not begin with a complete such sequence.
> + * If end is NULL, assume a NUL terminated string, else never look at *end or
> + * beyond.
> + */
> +size_t display_mode_esc_sequence_len(const char *start, const char *end)
> {
> - const char *p = s;
> - if (*p++ != '\033')
> + const char *p = start;
> +
> + if ((end && end <= p) || *p++ != '\033')
> return 0;
> - if (*p++ != '[')
> + if ((end && end <= p) || *p++ != '[')
> return 0;
> - while (isdigit(*p) || *p == ';')
> + while (((end && p < end) || (!end && *p)) && (isdigit(*p) || *p == ';'))
> p++;
> - if (*p++ != 'm')
> + if ((end && end <= p) || (!end && !*p) || *p++ != 'm')
> return 0;
Those checks can be simplified a bit:
while (!(end && p >= end) && (isdigit(*p) || *p == ';'))
p++;
if ((end && p >= end) || *p++ != 'm')
return 0;
In case you want to go back to a length parameter instead of the
optional end pointer: I looked it up and functions such as strncmp()
and POSIX strnlen() that take a length bound actually support passing
SIZE_MAX as the length.
The key is not to compute `s + len`, because that can create a pointer
more than one past the end of the object, which is undefined behaviour
even without dereferencing the pointer. Instead, the bounds need to be
checked with something like `p - s < len` before reading `*p`, which
avoids creating an undefined pointer.
Andy
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads