advance() should check current string index is in bound.

This commit is contained in:
Lei Zhang 2015-11-06 15:09:04 -05:00 committed by David Neto
parent 9042f40f7c
commit 8f6ba14b58
2 changed files with 9 additions and 0 deletions

View File

@ -78,6 +78,7 @@ spv_result_t advanceLine(spv_text text, spv_position position) {
/// @return result code
spv_result_t advance(spv_text text, spv_position position) {
// NOTE: Consume white space, otherwise don't advance.
if (position->index >= text->length) return SPV_END_OF_STREAM;
switch (text->str[position->index]) {
case '\0':
return SPV_END_OF_STREAM;

View File

@ -88,4 +88,12 @@ TEST(TextAdvance, NullTerminator) {
ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
}
TEST(TextAdvance, NoNullTerminator) {
spv_text_t text = {"OpNop\nSomething else in memory", 6};
AssemblyContext data(&text, nullptr);
const spv_position_t line_break = {1, 5, 5};
data.setPosition(line_break);
ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
}
} // anonymous namespace