public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dcb314 at hotmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/58237] New: gcc fails to detect obvious resource leaks
Date: Sat, 24 Aug 2013 15:01:00 -0000	[thread overview]
Message-ID: <bug-58237-4@http.gcc.gnu.org/bugzilla/> (raw)

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58237

            Bug ID: 58237
           Summary: gcc fails to detect obvious resource leaks
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dcb314 at hotmail dot com

Some code has obvious resource leaks

# include <stdio.h>

void f1(const char *str)
{
    FILE * fp = fopen(str, "r");
    char buf[10];

    while (fgets(buf, 10, fp) != NULL)
    {
        /* Do something with buf */
    }
    /* Missing call to fclose. Need warning here for resource leak */
}

It would be nice if gcc could notice the obvious missing call
to fclose and produce a warning about a resource leak.

To help some test driven development, here are some additional test cases.

This case is slightly more subtle, but still a warning would
be nice for when flag == 0.

void f2(const char *str, int flag)
{
    FILE * fp = fopen(str, "r");
    char buf[10];

    while (fgets(buf, 10, fp) != NULL)
    {
        /* Do something with buf */
    }
    /* fclose only sometimes called.
     * Still a leak for the case when flag == 0.
     */
    if (flag)
        fclose(fp);
}

For this one, all bets are off since we don't know what function
f31() does with fp.

extern void f31( FILE * fp);

void f3(const char *str)
{
    FILE * fp = fopen(str, "r");
    char buf[10];

    while (fgets(buf, 10, fp) != NULL)
    {
        /* Do something with buf */
    }
    /* Not sure if fclose executed by f31 or not. Say nothing */
    f31(fp);
}

Here is the obvious case where we shouldn't say anything

void f4(const char *str)
{
    FILE * fp = fopen(str, "r");
    char buf[10];

    while (fgets(buf, 10, fp) != NULL)
    {
        /* Do something with buf */
    }
    /* Nothing to say here. */
    fclose(fp);
}

And here is another one where producing a warning message
seems counter - productive.

void main(int argc, const char * argv[])
{
    FILE * fp = fopen(argv[0], "r");
    char buf[10];

    while (fgets(buf, 10, fp) != NULL)
    {
        /* Do something with buf */
    }
    /* Nothing to say here, because we are in main. */
}

Of course, there are many other matched pairs of functions
(open, close), (opendir, closedir) etc that could also have
resource leak warnings. 

A useful first step would be to pick one pair and implement for that.
Then extend to other pairs.

Here is cppcheck doing what I want

$ ~/cppcheck/cppcheck/cppcheck aug24a.cc
Checking aug24a.cc...
[aug24a.cc:16]: (error) Resource leak: fp
[aug24a.cc:32]: (error) Resource leak: fp
$


             reply	other threads:[~2013-08-24 15:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-24 15:01 dcb314 at hotmail dot com [this message]
2013-09-12 15:04 ` [Bug c/58237] " mpolacek at gcc dot gnu.org
2013-09-12 15:37 ` manu at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-58237-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).