Avoid going out of bound in advanceLine() and refine comments.
authorLei Zhang <antiagainst@google.com>
Thu, 21 Apr 2016 19:50:23 +0000 (15:50 -0400)
committerLei Zhang <antiagainst@google.com>
Thu, 21 Apr 2016 20:08:51 +0000 (16:08 -0400)
source/text_handler.cpp
test/TextAdvance.cpp

index 3803465..2a30205 100644 (file)
@@ -46,14 +46,11 @@ using spvutils::BitwiseCast;
 using spvutils::FloatProxy;
 using spvutils::HexFloat;
 
-/// @brief Advance text to the start of the next line
-///
-/// @param[in] text to be parsed
-/// @param[in,out] position position text has been advanced to
-///
-/// @return result code
+// Advances |text| to the start of the next line and writes the new position to
+// |position|.
 spv_result_t advanceLine(spv_text text, spv_position position) {
   while (true) {
+    if (position->index >= text->length) return SPV_END_OF_STREAM;
     switch (text->str[position->index]) {
       case '\0':
         return SPV_END_OF_STREAM;
@@ -70,15 +67,11 @@ spv_result_t advanceLine(spv_text text, spv_position position) {
   }
 }
 
-/// @brief Advance text to first non white space character
-/// If a null terminator is found during the text advance SPV_END_OF_STREAM is
-/// returned, SPV_SUCCESS otherwise. No error checking is performed on the
-/// parameters, its the users responsibility to ensure these are non null.
-///
-/// @param[in] text to be parsed
-/// @param[in,out] position text has been advanced to
-///
-/// @return result code
+// Advances |text| to first non white space character and writes the new
+// position to |position|.
+// If a null terminator is found during the text advance, SPV_END_OF_STREAM is
+// returned, SPV_SUCCESS otherwise. No error checking is performed on the
+// parameters, its the users responsibility to ensure these are non null.
 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;
index 0f18530..f1044cb 100644 (file)
@@ -88,6 +88,14 @@ TEST(TextAdvance, NullTerminator) {
   ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
 }
 
+TEST(TextAdvance, NoNullTerminatorAfterCommentLine) {
+  std::string input = "; comment|padding beyond the end";
+  spv_text_t text = {input.data(), 9};
+  AssemblyContext data(&text, nullptr);
+  ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
+  EXPECT_EQ(9u, data.position().index);
+}
+
 TEST(TextAdvance, NoNullTerminator) {
   spv_text_t text = {"OpNop\nSomething else in memory", 6};
   AssemblyContext data(&text, nullptr);