X Tutup
Skip to content

Fix #9343 (memleak FP when return with cast)#2162

Merged
danmar merged 1 commit intodanmar:masterfrom
rikardfalkeborn:9343-memleak-with-return-with-casts
Sep 20, 2019
Merged

Fix #9343 (memleak FP when return with cast)#2162
danmar merged 1 commit intodanmar:masterfrom
rikardfalkeborn:9343-memleak-with-return-with-casts

Conversation

@rikardfalkeborn
Copy link
Copy Markdown
Contributor

This was most likely introduced when the checks were changed to run on
the full tokenlist instead of the simplified one.

void return7() { // #9343
check("uint8_t *f() {\n"
" void *x = malloc(1);\n"
" return (uint8_t *)x;\n"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(I didn't look for further details, just an idea)
Should one also test

return (uint8_t)malloc(1);

?
And true positives

   return (short)x;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you mean that we should warn for those cases since the casts destroy information, or did you forget to add * to the type?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

* is missing by intention.
Those are serious issues and maybe even deserve a distinct error message.
I was just wondering what the current results are and if they are going to change with this PR.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

good find @amai2012 !

@rikardfalkeborn : I let you decide: do you want to fix that now then feel free to do it. If you want to merge it now I think it seems ready.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What is the desired outcome of the above examples? A memleak warning, or a new warning?

Also, should we warn if the size of the integer type is equal to the size of a pointer (maybe a portability warning if there isn't one already)?

I think I'd prefer a new warning that deals with all casts or truncations of dynamically allocated memory (maybe there is one already, I didn't check), what do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also, I think that the for first case (return (uint8_t)malloc(1);), there's no warning, regardless of this change or not, and for the second case (return (short)x;), there was a memleak warning before but with this change there isn't one.

Also, again guessing, I think the behaviour with this change is identical to the one when checks were run on the simplified tokenlist.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think I'd prefer a new warning that deals with all casts or truncations of dynamically allocated memory (maybe there is one already, I didn't check), what do you think?

If you are up for it.. That sounds great! In my humble opinion, it would probably work with a normal memleak message but it might be a bit off for our users.

Also, should we warn if the size of the integer type is equal to the size of a pointer (maybe a portability warning if there isn't one already)?

If the size is equal... I doubt that we can warn. I am guessing there would be quite a lot of noise. The standard does say that you should use intptr but I don't think people care about that.

For different size.. it would be ok to warn however some compilers do have such warning already.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I finally got around to it, I think that just ignoring returns with casts is ok for now.

This was most likely introduced when the checks were changed to run on
the full tokenlist instead of the simplified one.

Take care to warn about cases where casts destroy the pointer, such as

	uint8_t f() {
		void* x = malloc(1);
		return (uint8_t)x;
	}
@rikardfalkeborn rikardfalkeborn force-pushed the 9343-memleak-with-return-with-casts branch from efc2cd5 to 98a3c13 Compare September 18, 2019 22:52
@danmar danmar merged commit 007b5d3 into danmar:master Sep 20, 2019
@rikardfalkeborn rikardfalkeborn deleted the 9343-memleak-with-return-with-casts branch September 20, 2019 19:26
jubnzv pushed a commit to jubnzv/cppcheck that referenced this pull request Nov 13, 2019
This was most likely introduced when the checks were changed to run on
the full tokenlist instead of the simplified one.

Take care to warn about cases where casts destroy the pointer, such as

	uint8_t f() {
		void* x = malloc(1);
		return (uint8_t)x;
	}
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

X Tutup