[PATCH] D127369: [Object][COFF] Fix section name parsing error when the name field is not null-padded

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 10 11:42:33 PDT 2022


efriedma added inline comments.


================
Comment at: llvm/lib/Object/COFFObjectFile.cpp:1171
     } else {
-      if (Name.substr(1).getAsInteger(10, Offset))
         return createStringError(object_error::parse_failed,
----------------
pzheng wrote:
> pzheng wrote:
> > rnk wrote:
> > > I think it's a bug that getAsInteger doesn't work on non-null terminated StringRefs. It's not an invariant that StringRefs are null terminated. We explicitly form a non-null terminated StringRef on line 1161 above.
> > hmm..., not sure if getAsInteger is supposed to handle a situation like this, the description of the function isn't very clear, but I tend to agree with you that this could be bug unless there are already code in LLVM which assumes getAsInteger should fail given such input.
> Looking at the implementation of getAsInteger, it looks like it's actually supposed to fail with such input where only the first part of it is a valid integer. getAsInteger requires the whole string to be consumed or else it's considered as a failure.
In the testcase, `Name.substr(1)` contains the value "4\0abcde", i.e. an embedded null.  Since '\0' isn't a digit, getAsInteger() is correctly rejecting it.  consumeInteger() just stops parsing at '\0'.

I suspect this code shouldn't be passing down a StringRef with embedded nulls, though.  Maybe the `if (Sec->Name[COFF::NameSize - 1] == 0)` check is wrong.  (The spec says "null-padded", but maybe in practice Microsoft tools just treat it as "null-terminated".)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127369/new/

https://reviews.llvm.org/D127369



More information about the llvm-commits mailing list