This is an archive of the discontinued LLVM Phabricator instance.

Fix invalid memory access in llvm-mc if file ends in non-newline whitespace or comma
ClosedPublic

Authored by colinl on Nov 6 2014, 12:30 PM.

Details

Reviewers
rafael
Summary

Str may be empty after .find_first_not_of(" \t\r,") and the following Str[0] accesses invalid memory.

Diff Detail

Event Timeline

colinl updated this revision to Diff 15886.Nov 6 2014, 12:30 PM
colinl retitled this revision from to Fix invalid memory access in llvm-mc if file ends in non-newline whitespace or comma.
colinl updated this object.
colinl edited the test plan for this revision. (Show Details)
colinl set the repository for this revision to rL LLVM.
colinl added a subscriber: Unknown Object (MLST).

Don't we also have to handle npos?

colinl added a comment.Nov 6 2014, 1:39 PM

It looks like substr accepts npos as a start and returns the empty string

/ \param Start The index of the starting character in the substring; if
/ the index is npos or greater than the length of the string then the
/// empty substring will be returned.

I find the code a bit hard to read as written.

can we rewrite it to be just a series of "if (not interesting) { eat; continue to see if there is more;}"? Something like

static bool SkipToToken(StringRef &Str) {

for (;;) {
  if (Str.empty())
    return false;

  // Strip horizontal whitespace and commas.                                   
  if (size_t Pos = Str.find_first_not_of(" \t\r,")) {
    Str = Str.substr(Pos);
    continue;
  }

  if (Str[0] == '#') {
    Str = Str.substr(Str.find_first_of('\n'));
    continue;
  }

  if (Str[0] == '\n') {
    Str = Str.substr(1);
    continue;
  }

  return true;
}

}

colinl updated this revision to Diff 15899.Nov 6 2014, 3:48 PM

Agreed that loop is a little difficult.

It looks like we can also move the check for '\n' in to the find_first_not_of check.

If this looks good I'll submit it.

rafael accepted this revision.Nov 10 2014, 10:30 AM
rafael edited edge metadata.

LGTM

This revision is now accepted and ready to land.Nov 10 2014, 10:30 AM
colinl closed this revision.Nov 13 2014, 8:43 AM

Submitted.