[OpenSmalltalk/opensmalltalk-vm] code smell (IntelliSense C6308): realloc could cause a memory leak (#335)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[OpenSmalltalk/opensmalltalk-vm] code smell (IntelliSense C6308): realloc could cause a memory leak (#335)

David T Lewis
 

In this pattern:

ptr = realloc( ptr , new_size );

realloc may fail when HEAP memory is exhausted, then return 0, and overwrite ptr with a NULL pointer...
But realloc did not free(ptr), it rather leaves it untouched!

OK, we rarely exhaust HEAP nowadays, and consequences might be catastrophic anyway.
But it is a code smell. It should be written like this:

aux = realloc( ptr , new_size );
if( aux == NULL ) /* handle_the_error */ {};
else ptr = aux;


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/OpenSmalltalk/opensmalltalk-vm","title":"OpenSmalltalk/opensmalltalk-vm","subtitle":"GitHub repository","main_image_url":"https://github.githubassets.com/images/email/message_cards/header.png","avatar_image_url":"https://github.githubassets.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/OpenSmalltalk/opensmalltalk-vm"}},"updates":{"snippets":[{"icon":"DESCRIPTION","message":"code smell (IntelliSense C6308): realloc could cause a memory leak (#335)"}],"action":{"name":"View Issue","url":"https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/335"}}}</script> <script type="application/ld+json">[ { "@context": "http://schema.org", "@type": "EmailMessage", "potentialAction": { "@type": "ViewAction", "target": "https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/335", "url": "https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/335", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { "@type": "Organization", "name": "GitHub", "url": "https://github.com" } } ]</script>
Reply | Threaded
Open this post in threaded view
|

Re: [OpenSmalltalk/opensmalltalk-vm] code smell (IntelliSense C6308): realloc could cause a memory leak (#335)

David T Lewis
 

See: 82d1c33#diff-ea77aa3cf89ad27f0980e8a8b857fb1cR585


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/OpenSmalltalk/opensmalltalk-vm","title":"OpenSmalltalk/opensmalltalk-vm","subtitle":"GitHub repository","main_image_url":"https://github.githubassets.com/images/email/message_cards/header.png","avatar_image_url":"https://github.githubassets.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/OpenSmalltalk/opensmalltalk-vm"}},"updates":{"snippets":[{"icon":"PERSON","message":"@krono in #335: See: https://github.com/OpenSmalltalk/opensmalltalk-vm/commit/82d1c33a1c5756b0a76bd37539d15a4824368d59#diff-ea77aa3cf89ad27f0980e8a8b857fb1cR585"}],"action":{"name":"View Issue","url":"https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/335#issuecomment-450760390"}}}</script> <script type="application/ld+json">[ { "@context": "http://schema.org", "@type": "EmailMessage", "potentialAction": { "@type": "ViewAction", "target": "https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/335#issuecomment-450760390", "url": "https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/335#issuecomment-450760390", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { "@type": "Organization", "name": "GitHub", "url": "https://github.com" } } ]</script>
Reply | Threaded
Open this post in threaded view
|

Re: [OpenSmalltalk/opensmalltalk-vm] code smell (IntelliSense C6308): realloc could cause a memory leak (#335)

timrowledge
In reply to this post by David T Lewis
 


> On 2019-01-01, at 1:39 PM, Nicolas Cellier <[hidden email]> wrote:
>
> In this pattern:
>
> ptr = realloc( ptr , new_size );
>
> realloc may fail when HEAP memory is exhausted, then return 0, and overwrite ptr with a NULL pointer...
> But realloc did not free(ptr), it rather leaves it untouched!

That's just... awful. If it can't realloc it should damn well leave the original alone


tim
--
tim Rowledge; [hidden email]; http://www.rowledge.org/tim
Useful random insult:- Not much to show for four billion years of evolution.


Reply | Threaded
Open this post in threaded view
|

Re: [OpenSmalltalk/opensmalltalk-vm] code smell (IntelliSense C6308): realloc could cause a memory leak (#335)

David T Lewis
In reply to this post by David T Lewis
 

Tim, to be sure about understanding: realloc does the right thing, it leaves the original alone.
It's we, programmers that do not do the right thing: we overwrite a valid pointer with a NULL pointer. And now, there is no references to the valid pointer anymore => memory leak (+ eventually other side effects of having a NULL pointer hanging).


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/OpenSmalltalk/opensmalltalk-vm","title":"OpenSmalltalk/opensmalltalk-vm","subtitle":"GitHub repository","main_image_url":"https://github.githubassets.com/images/email/message_cards/header.png","avatar_image_url":"https://github.githubassets.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/OpenSmalltalk/opensmalltalk-vm"}},"updates":{"snippets":[{"icon":"PERSON","message":"@nicolas-cellier-aka-nice in #335: Tim, to be sure about understanding: `realloc` does the right thing, it leaves the original alone.\r\nIt's we, programmers that do not do the right thing: we overwrite a valid pointer with a NULL pointer. And now, there is no references to the valid pointer anymore =\u003e memory leak (+ eventually other side effects of having a NULL pointer hanging)."}],"action":{"name":"View Issue","url":"https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/335#issuecomment-450888651"}}}</script> <script type="application/ld+json">[ { "@context": "http://schema.org", "@type": "EmailMessage", "potentialAction": { "@type": "ViewAction", "target": "https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/335#issuecomment-450888651", "url": "https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/335#issuecomment-450888651", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { "@type": "Organization", "name": "GitHub", "url": "https://github.com" } } ]</script>
Reply | Threaded
Open this post in threaded view
|

Re: [OpenSmalltalk/opensmalltalk-vm] code smell (IntelliSense C6308): realloc could cause a memory leak (#335)

timrowledge
 


> On 2019-01-02, at 7:12 AM, Nicolas Cellier <[hidden email]> wrote:
>
> Tim, to be sure about understanding: realloc does the right thing, it leaves the original alone.
> It's we, programmers that do not do the right thing: we overwrite a valid pointer with a NULL pointer.

Yeah, I thought I had cancelled my email once I realised that was what was going on. Apparently I overwrote the pointer with nil....

tim
--
tim Rowledge; [hidden email]; http://www.rowledge.org/tim
Strange OpCodes: ESBD: Erase System and Burn Documentation