public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/58237] New: gcc fails to detect obvious resource leaks
@ 2013-08-24 15:01 dcb314 at hotmail dot com
  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
  0 siblings, 2 replies; 3+ messages in thread
From: dcb314 at hotmail dot com @ 2013-08-24 15:01 UTC (permalink / raw)
  To: gcc-bugs

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
$


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/58237] gcc fails to detect obvious resource leaks
  2013-08-24 15:01 [Bug c/58237] New: gcc fails to detect obvious resource leaks dcb314 at hotmail dot com
@ 2013-09-12 15:04 ` mpolacek at gcc dot gnu.org
  2013-09-12 15:37 ` manu at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2013-09-12 15:04 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I don't think GCC is the right place to implement this.  SA tools are more
appropriate for this.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/58237] gcc fails to detect obvious resource leaks
  2013-08-24 15:01 [Bug c/58237] New: gcc fails to detect obvious resource leaks dcb314 at hotmail dot com
  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
  1 sibling, 0 replies; 3+ messages in thread
From: manu at gcc dot gnu.org @ 2013-09-12 15:37 UTC (permalink / raw)
  To: gcc-bugs

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

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu.org

--- Comment #2 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
(In reply to Marek Polacek from comment #1)
> I don't think GCC is the right place to implement this.  SA tools are more
> appropriate for this.

Or a GCC plugin. I vaguely remember some discussion that GCC could distribute a
plugin that allowed users to perform this kind of expensive static analysis. Of
course, the issue (as always) is that someone needs to implement such a thing
and the number of people (interested && capable && available) is close to zero.

(cppcheck and clang-analyzer do *many* things that GCC will never be able to
do)
>From gcc-bugs-return-429682-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Thu Sep 12 16:35:33 2013
Return-Path: <gcc-bugs-return-429682-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 16801 invoked by alias); 12 Sep 2013 16:35:33 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 16744 invoked by uid 48); 12 Sep 2013 16:35:30 -0000
From: "tprince at computer dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libgomp/58392] internal compiler error: in expand_GOMP_SIMD_VF, at internal-fn.c (omp simd inside omp parallel)
Date: Thu, 12 Sep 2013 16:35:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: libgomp
X-Bugzilla-Version: unknown
X-Bugzilla-Keywords:
X-Bugzilla-Severity: normal
X-Bugzilla-Who: tprince at computer dot org
X-Bugzilla-Status: RESOLVED
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields: bug_status resolution
Message-ID: <bug-58392-4-iSo9xHgAQQ@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-58392-4@http.gcc.gnu.org/bugzilla/>
References: <bug-58392-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2013-09/txt/msg00922.txt.bz2
Content-length: 408

http://gcc.gnu.org/bugzilla/show_bug.cgi?idX392

tprince at computer dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from tprince at computer dot org ---
Fixes my test case.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-09-12 15:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-24 15:01 [Bug c/58237] New: gcc fails to detect obvious resource leaks dcb314 at hotmail dot com
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

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).