From 8f6ba14b58b4922384bfd931b11224f76496cf09 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 6 Nov 2015 15:09:04 -0500 Subject: [PATCH] advance() should check current string index is in bound. --- source/text_handler.cpp | 1 + test/TextAdvance.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/source/text_handler.cpp b/source/text_handler.cpp index 34b48f1..b46fa01 100644 --- a/source/text_handler.cpp +++ b/source/text_handler.cpp @@ -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; diff --git a/test/TextAdvance.cpp b/test/TextAdvance.cpp index 3ded9d7..ed435ee 100644 --- a/test/TextAdvance.cpp +++ b/test/TextAdvance.cpp @@ -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 -- 2.7.4