Skip to content

Commit 9d488d6

Browse files
committed
cfg-source: do proper boundary checking of yylloc values
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>
1 parent f959a11 commit 9d488d6

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lib/cfg-source.c

+15-3
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,18 @@ _extract_source_from_buffer_location(GString *result, CfgIncludeLevel *level, co
248248

249249
if (lineno == yylloc->first_line)
250250
{
251+
gint token_start = MIN(linelen, yylloc->first_column - 1);
252+
251253
if (yylloc->first_line == yylloc->last_line)
252-
g_string_append_len(result, &line[MIN(linelen, yylloc->first_column-1)], yylloc->last_column - yylloc->first_column);
254+
{
255+
/* both last_column & first_column are 1 based, they cancel that out */
256+
gint token_len = yylloc->last_column - yylloc->first_column;
257+
if (token_start + token_len > linelen)
258+
token_len = linelen - token_start;
259+
g_string_append_len(result, &line[token_start], token_len);
260+
}
253261
else
254-
g_string_append(result, &line[MIN(linelen, yylloc->first_column-1)]);
262+
g_string_append(result, &line[token_start]);
255263
}
256264
else if (lineno < yylloc->last_line)
257265
{
@@ -260,8 +268,12 @@ _extract_source_from_buffer_location(GString *result, CfgIncludeLevel *level, co
260268
}
261269
else if (lineno == yylloc->last_line)
262270
{
271+
/* last_column is 1 based */
272+
gint token_len = yylloc->last_column - 1;
273+
if (token_len > linelen)
274+
token_len = linelen;
263275
g_string_append_c(result, ' ');
264-
g_string_append_len(result, line, yylloc->last_column);
276+
g_string_append_len(result, line, token_len);
265277
}
266278
}
267279

0 commit comments

Comments
 (0)