-
Notifications
You must be signed in to change notification settings - Fork 15k
[MC] AsmLexer assert buffer is null-terminated at CurBuf.end() #154972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,11 @@ AsmLexer::AsmLexer(const MCAsmInfo &MAI) : MAI(MAI) { | |
|
|
||
| void AsmLexer::setBuffer(StringRef Buf, const char *ptr, | ||
| bool EndStatementAtEOF) { | ||
| // Buffer must be NULL-terminated. NULL terminator must reside at `Buf.end()`. | ||
| // It must be safe to dereference `Buf.end()`. | ||
| assert(*Buf.end() == '\0' && | ||
| "Buffer provided to AsmLexer lacks null terminator."); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I spent some time playing around with buffer allocation in order to ensure buffer allocated in by llvm-mc doesn't allocate any additional memory ( After a while I finally realized that AsmLexer always expects that the null terminator of the buffer is present and it is NOT included in the buffer's length. That means that Unfortunately I couldn't find this expectation documented anywhere. I could patch AsmLexer to support non-null-terminated buffers, however is that a change we desire? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To additionally clarify, since I didn't mention it above, if a buffer IS null-terminated, however the null terminator resides at |
||
| CurBuf = Buf; | ||
|
|
||
| if (ptr) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the best way to test for null-termination. Invalid read won't be detected by non-LLVM_USE_SANITIZER=Address builds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's right to require a NUL terminator, like regular
MemoryBufferAPIs provide (getFileOrSTDINMemoryBuffer::getFile.We should add a comment to the header file
llvm/include/llvm/MC/MCParser/AsmLexer.habout this requirement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments added in 8a0b79b