Skip to content

Conversation

ghost
Copy link

@ghost ghost commented Aug 20, 2013

http://d.puremagic.com/issues/show_bug.cgi?id=10398

These changes fix some memory issues in dmd detected by memcheck during druntime and phobos compilation.

  1. There are issues with local uninitialized variable code cs; which is later used in compare expressions with undefined value.
  2. in Lexer::nextToken() *t pointer is uninitialized and its value is later copied.
  3. Code which indirectly calls StringEntry::alloc may exhibit off-by-one error.

@@ -296,8 +296,8 @@ void Lexer::deprecation(const char *format, ...)
}

TOK Lexer::nextToken()
{ Token *t;

{ Token *t = (Token*)mem.malloc(sizeof(Token));;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix is likely wrong (t is only used in the token.next branch), and would be performance suicide either way.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is later saved in freelist global, but it is possible to move allocation inside if(token.next).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems as if the return value of mem.malloc is never used in your code, as t is immediately set to token.next in the respective branch.

I haven't touched that part of DMD in a while, but what is supposed to happen here is that the next token is copied into the space for the currently processed one, and then the allocation for the former is discarded. Discarded here means adding it to the manually maintained free list (as we have to avoid allocation overhead here at any cost for performance).

In case token.next can point to garbage, this obviously is a problem that needs to be fixed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this looks wrong and is not solving anything. If t is required to be initialised, use NULL. Or even better, move the declaration into the true branch. Token *t = token.next;.

@ghost
Copy link
Author

ghost commented Aug 21, 2013

@klickverbot @ibuclaw Yes. I removed allocation.

if (token.next)
{
t = token.next;
Token *t = token.next;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how this addresses:

2.in Lexer::nextToken() *t pointer is uninitialized and its value is later copied.

@ghost
Copy link
Author

ghost commented Aug 31, 2013

Ok, closed.

@ghost ghost closed this Aug 31, 2013
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants