Skip to content

Commit

Permalink
cfg-source: do proper boundary checking of yylloc values
Browse files Browse the repository at this point in the history
Sometimes location tracking is buggy, make sure we don't address outside
of the source text.

Signed-off-by: Balazs Scheidler <balazs.scheidler@axoflow.com>
  • Loading branch information
bazsi committed Jan 24, 2025
1 parent f959a11 commit 9d488d6
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/cfg-source.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,18 @@ _extract_source_from_buffer_location(GString *result, CfgIncludeLevel *level, co

if (lineno == yylloc->first_line)
{
gint token_start = MIN(linelen, yylloc->first_column - 1);

if (yylloc->first_line == yylloc->last_line)
g_string_append_len(result, &line[MIN(linelen, yylloc->first_column-1)], yylloc->last_column - yylloc->first_column);
{
/* both last_column & first_column are 1 based, they cancel that out */
gint token_len = yylloc->last_column - yylloc->first_column;
if (token_start + token_len > linelen)
token_len = linelen - token_start;
g_string_append_len(result, &line[token_start], token_len);
}
else
g_string_append(result, &line[MIN(linelen, yylloc->first_column-1)]);
g_string_append(result, &line[token_start]);
}
else if (lineno < yylloc->last_line)
{
Expand All @@ -260,8 +268,12 @@ _extract_source_from_buffer_location(GString *result, CfgIncludeLevel *level, co
}
else if (lineno == yylloc->last_line)
{
/* last_column is 1 based */
gint token_len = yylloc->last_column - 1;
if (token_len > linelen)
token_len = linelen;
g_string_append_c(result, ' ');
g_string_append_len(result, line, yylloc->last_column);
g_string_append_len(result, line, token_len);
}
}

Expand Down

0 comments on commit 9d488d6

Please sign in to comment.