From: David Malcolm <dmalcolm@redhat.com>
To: Mir Immad <mirimnan017@gmail.com>, gcc@gcc.gnu.org
Subject: Re: [PATCH] static analysis support for posix file desccriptor APIs
Date: Fri, 24 Jun 2022 14:05:06 -0400 [thread overview]
Message-ID: <8d8eca80dbe287a79f633b410fbba3b775de3054.camel@redhat.com> (raw)
In-Reply-To: <CAE1-7oxk=t7AzdBLB8+u+eU+5TDHYFbZ1-wtwdQJSRkC8iu9ug@mail.gmail.com>
On Thu, 2022-06-23 at 23:58 +0530, Mir Immad wrote:
> Hi Dave,
> Thanks for the suggestions,
>
> I changed most of the things that you suggested, however reporting
> for
> warnings like close of known invalid fd was problematic:
>
> consider the following code:
>
> if (fd >= 0)
> { write (fd,...); }
> close(fd);
>
> As I was checking the exploded graph for this; the "close(fd)" stmt
> when
> visited by the FALSE edge of if stmt (fd < 0) finds fd to be in
> m_invalid
> state; hence warns about "close on known invalid fd" which I believe
> is not
> true as fd at that point is not *known* to be invalid. I spent quite
> some
> time on this and decided not to add this diagnosis for now.
That choice seems reasonable to me.
>
> Also, when close transitions m_invalid to m_close; this leads to
> double
> close even when the second close is outside the ( < 0 ) condition
> which
> again does not seem right.
> if (fd < 0)
> close(fd):
> close(fd); // double close here.
"close" on an invalid FD doesn't give you a closed FD, you still have
an invalid FD.
By analogy with sm-malloc.cc: free (NULL) doesn't make NULL a freed
pointer, it's still NULL, and thus just a no-op.
(although in this case: close(invalid) will also set errno to EBADF,
so it's not strictly a no-op).
>
> > Maybe consolidate on_read and on_write by adding a subroutine that
> > checks for m_closed, and for checking access mode (maybe a
> > "check_for_open_fd" that takes an access mode enum value amongst
> > other
> > params. If you pass around caller_fndecl, I think much of this
> > code
> > can be shared that way between on_read and on_write (which will
> > help
> > simplify things if we want to support further functions)
>
> I hope I got this right.
>
>
> > > + }
> > > + }
> > > + else
> > > + {
> > >+ /* FIXME: add leak reporting */
>
> > Do you have a testcase that exhibits this behavior?
>
> I was thinking of the following case:
> void test()
> {
> open(..);
> }
> Here the resources are leaked because there is no way to free them.
Please add a test case for this, marking it with xfail if need be.
But hopefully it will be easy to implement the FIXME here, with an:
sm->on_warn (new fd_leak ());
or somesuch (not sure if there's a "tree var" you can give it for this
case though, might have to use NULL).
>
> In "read" and "write" funcs, I'm warning for unchecked_use_of_fd and
> access_mode_mismatch only when we know fd is not in closed state.
> Otherwise, such code leads to lot of irrelevant warnings, example:
>
> void test1(const char *path, void *buf) {
> int fd = open(path, O_RDONLY);
> if (fd >= 0)
> {
> close(fd);
> read(fd, buf, 1); // read on closed fd + read on possibly invalid
> fd
> write(fd, buf, 1); // write on closed fd + write on possibly invlid
> fd
> }
> }
Should we transition the FD to the "stop" state after the first
warning? That way we ought to avoid a bunch of followup warnings for
that FD.
>
>
> Adding docs for new options still remains pending. I added some new
> tests;
> and all tests are passing. The stuff about O_* macros is left as-is.
>
> I'm sending a patch in another email.
>
> Thanks a lot.
Thanks
Dave
prev parent reply other threads:[~2022-06-24 18:05 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-21 16:30 Mir Immad
2022-06-21 16:30 ` Mir Immad
2022-06-21 18:34 ` David Malcolm
2022-06-21 18:50 ` Joseph Myers
2022-06-21 20:31 ` David Malcolm
2022-06-21 21:18 ` Joseph Myers
2022-06-23 18:28 ` Mir Immad
2022-06-24 18:05 ` David Malcolm [this message]
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=8d8eca80dbe287a79f633b410fbba3b775de3054.camel@redhat.com \
--to=dmalcolm@redhat.com \
--cc=gcc@gcc.gnu.org \
--cc=mirimnan017@gmail.com \
/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).