advance() should check current string index is in bound.
authorLei Zhang <antiagainst@google.com>
Fri, 6 Nov 2015 20:09:04 +0000 (15:09 -0500)
committerDavid Neto <dneto@google.com>
Tue, 10 Nov 2015 20:57:52 +0000 (15:57 -0500)
source/text_handler.cpp
test/TextAdvance.cpp

index 34b48f1..b46fa01 100644 (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;
index 3ded9d7..ed435ee 100644 (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