Skip to content

Commit

Permalink
getWord() should not go beyond the end of the source text.
Browse files Browse the repository at this point in the history
This addresses Bug KhronosGroup#126 where EOL is missing at the end of
source file.
  • Loading branch information
antiagainst committed Feb 22, 2016
1 parent d1f64c6 commit 9413fbb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions source/text_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ spv_result_t getWord(spv_text text, spv_position position, std::string& word,

// NOTE: Assumes first character is not white space!
while (true) {
if (endPosition->index >= text->length) {
word.assign(text->str + position->index,
static_cast<size_t>(endPosition->index - position->index));
return SPV_SUCCESS;
}
const char ch = text->str[endPosition->index];
if (ch == '\\')
escaping = !escaping;
Expand All @@ -142,8 +147,9 @@ spv_result_t getWord(spv_text text, spv_position position, std::string& word,
if (escaping || quoting) break;
// Fall through.
case '\0': { // NOTE: End of word found!
word.assign(text->str + position->index,
(size_t)(endPosition->index - position->index));
word.assign(
text->str + position->index,
static_cast<size_t>(endPosition->index - position->index));
return SPV_SUCCESS;
}
default:
Expand Down
15 changes: 15 additions & 0 deletions test/TextWordGet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ TEST(TextWordGet, SemicolonTerminator) {
ASSERT_STREQ("Wo", word.c_str());
}

TEST(TextWordGet, NoTerminator) {
const std::string full_text = "abcdefghijklmn";
for (size_t len = 1; len <= full_text.size(); ++len) {
std::string word;
spv_text_t text = {full_text.data(), len};
spv_position_t endPosition = {};
ASSERT_EQ(SPV_SUCCESS,
AssemblyContext(&text, nullptr).getWord(word, &endPosition));
ASSERT_EQ(0u, endPosition.line);
ASSERT_EQ(len, endPosition.column);
ASSERT_EQ(len, endPosition.index);
ASSERT_EQ(full_text.substr(0, len), word);
}
}

TEST(TextWordGet, MultipleWords) {
AutoText input("Words in a sentence");
AssemblyContext data(input, nullptr);
Expand Down

0 comments on commit 9413fbb

Please sign in to comment.