aboutsummaryrefslogtreecommitdiff
path: root/libcpp/lex.c
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2016-12-15 18:05:05 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2016-12-15 18:05:05 +0000
commita3998c2fb1630638db83defcd2c680111d65b973 (patch)
tree6691b9007e679032843ddb4ba453fa49409bf3d2 /libcpp/lex.c
parenta3038e190b271744d0b5e2e83d389864fed4d54e (diff)
Fix use-after-free lexing unterminated raw strings (PR preprocessor/78811)
gcc/ChangeLog: PR preprocessor/78680 PR preprocessor/78811 * input.c (struct selftest::lexer_test): Add field m_implicitly_expect_EOF. (selftest::lexer_error_sink): New class. (selftest::lexer_error_sink::s_singleton): New global. (selftest::lexer_test::lexer_test): Initialize new field "m_implicitly_expect_EOF". (selftest::lexer_test::~lexer_test): Conditionalize the check for the EOF token on the new field. (selftest::test_lexer_string_locations_raw_string_unterminated): New function. (selftest::input_c_tests): Call the new test. libcpp/ChangeLog: PR preprocessor/78680 PR preprocessor/78811 * lex.c (_cpp_lex_direct): Only determine the end-location of the token and build a range for non-reserved start locations. Do not do it for EOF tokens. From-SVN: r243721
Diffstat (limited to 'libcpp/lex.c')
-rw-r--r--libcpp/lex.c40
1 files changed, 21 insertions, 19 deletions
diff --git a/libcpp/lex.c b/libcpp/lex.c
index ae458926b75..9b1bdf8ba30 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -3089,25 +3089,27 @@ _cpp_lex_direct (cpp_reader *pfile)
break;
}
- /* Ensure that any line notes are processed, so that we have the
- correct physical line/column for the end-point of the token even
- when a logical line is split via one or more backslashes. */
- if (buffer->cur >= buffer->notes[buffer->cur_note].pos
- && !pfile->overlaid_buffer)
- _cpp_process_line_notes (pfile, false);
-
- source_range tok_range;
- tok_range.m_start = result->src_loc;
- if (result->src_loc >= RESERVED_LOCATION_COUNT)
- tok_range.m_finish
- = linemap_position_for_column (pfile->line_table,
- CPP_BUF_COLUMN (buffer, buffer->cur));
- else
- tok_range.m_finish = tok_range.m_start;
-
- result->src_loc = COMBINE_LOCATION_DATA (pfile->line_table,
- result->src_loc,
- tok_range, NULL);
+ /* Potentially convert the location of the token to a range. */
+ if (result->src_loc >= RESERVED_LOCATION_COUNT
+ && result->type != CPP_EOF)
+ {
+ /* Ensure that any line notes are processed, so that we have the
+ correct physical line/column for the end-point of the token even
+ when a logical line is split via one or more backslashes. */
+ if (buffer->cur >= buffer->notes[buffer->cur_note].pos
+ && !pfile->overlaid_buffer)
+ _cpp_process_line_notes (pfile, false);
+
+ source_range tok_range;
+ tok_range.m_start = result->src_loc;
+ tok_range.m_finish
+ = linemap_position_for_column (pfile->line_table,
+ CPP_BUF_COLUMN (buffer, buffer->cur));
+
+ result->src_loc = COMBINE_LOCATION_DATA (pfile->line_table,
+ result->src_loc,
+ tok_range, NULL);
+ }
return result;
}