<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58964>58964</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] Bug in `mlir::parseType` handling stringrefs
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir:core
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          lattner
      </td>
    </tr>
</table>

<pre>
    The signature for `mlir::parseType` takes a StringRef for the type to parse, which doesn't need to be null terminated.  It is implemented with this logic:

```
template <typename T, typename ParserFn>
static T parseSymbol(StringRef inputStr, MLIRContext *context, size_t &numRead,
                     ParserFn &&parserFn) {
  SourceMgr sourceMgr;
  auto memBuffer = MemoryBuffer::getMemBuffer(
      inputStr, /*BufferName=*/"<mlir_parser_buffer>",
      /*RequiresNullTerminator=*/false);
 ```

Which forms a memory buffer around that and then goes and uses the MLIR parser to parse it.  The problem with this is that the MLIR asm parser does in fact require the memory buffer to be null terminated, which causes crashes.  I think the implementation of parseSymbol should use `getMemBufferCopy` when the input buffer isn't already nul terminated.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx1VE1v2zAM_TX2hVjgrzjJwYe2aYACazG0AXYsZJu2tcpWJsnrsl8_Uo6TdOgMwRYpknokH13q-ljsOwQr20G40SA02kCQR72SJkhvaB2Esbg_HpC04MQbWhDw4owc2mdsvL2jCI4swGnw5kFyB--drDqoNdohSFYOBsSaDUqEYVQKHJpe0qVYLwAeHEgLsj8o7HEgHbxL11Fg0irdyoqxRNsgmt95dFpedEieFAqC9I6BDKJH2DOKs_SNcZndEKT3k491wskK9hPil2NfahUk60tmcjiMjkQO8_j14flOE7DfDoLkppq2fGLlH3xlZT6M_TOKmpTTBfDZM8NgB1qHGVWygWB1Ozu-6NFU-NgasPMuSM-nYqQy9tjfjk2D1K10C4_Ya3OcFFPbWnSPswmldQ3pOrEg2VE-k9kT1YmCkey1CRWTafA6gXwtT9Hv-ehjklOUZ_w5SoP2idq7P3VXm3PERihmxuaSyT9dnN7fPW-IVj0TrfeJwXQ3CKPHgVjUCQfCb3CAVjMlSRotbZiL3K2preZMSZCOeMZcPxhdEs2uGCbtFPLsK2w_-zOBqWLQiMqBmTL0hh-RfUrsyxhUwoOrjLAdWiY83zy8-Uhn2hMh9QC6uaYk2E6PyifH9bpu650-HHko37kKPhA3dkYkT3MnlCFWHhnb9cyFWMR5vk5X63iZhXWR1pt0I0InncIiWN5yHYLlFm7HltP__y-ho9IrGhmwfnIMNjYcjSo65w6W7Zkbu5aKPZaLSvckKPVr_nyhbvzAikZpJ60d0dJmud7kWdgVyTrNk0hkYhVlq3UWx3WclZmoy6gsq3Ueh0qUqCyjJUqe4FXaIBN0uQ1lkURJEsdxmtCTZou0buo8bupVuslLpKHIIuyFVAtGstCmDU3hQZVja-lQSevs5VBY_k2iLw_HpznsNHkI5wY0ocdfePB_AVhzuq8">