public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR fortran/99147 - Sanitizer detects heap-use-after-free in gfc_add_flavor
@ 2021-02-18 21:12 Harald Anlauf
  2021-02-19  8:34 ` Paul Richard Thomas
  0 siblings, 1 reply; 2+ messages in thread
From: Harald Anlauf @ 2021-02-18 21:12 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 964 bytes --]

Dear all,

the PR reports an issue detected with an ASAN instrumented compiler,
which can also be verified with valgrind.  It appears that the state
of gfc_new_block could be such that it should not be dereferenced.
Reversing the order of condition evaluation helped.

I failed to find out why this should happen, but then other places
in the code put dereferences of gfc_new_block behind other checks.
Simple things like initializing gfc_new_block with NULL in decl.c
did not help.

Regtested on x86_64-pc-linux-gnu.  No testcase added since the issue
can be found only with an instrumented compiler or valgrind.

I consider the patch to be obvious and trivial, but post it here
in case somebody wants to dig deeper.

OK for master?

Thanks,
Harald


PR fortran/99147 - Sanitizer detects heap-use-after-free in gfc_add_flavor

Reverse order of conditions to avoid invalid read.

gcc/fortran/ChangeLog:

	* symbol.c (gfc_add_flavor): Reverse order of conditions.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr99147.patch --]
[-- Type: text/x-patch, Size: 697 bytes --]

diff --git a/gcc/fortran/symbol.c b/gcc/fortran/symbol.c
index 3b988d1be22..e982374d9d1 100644
--- a/gcc/fortran/symbol.c
+++ b/gcc/fortran/symbol.c
@@ -1772,8 +1772,8 @@ gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
   /* Copying a procedure dummy argument for a module procedure in a
      submodule results in the flavor being copied and would result in
      an error without this.  */
-  if (gfc_new_block && gfc_new_block->abr_modproc_decl
-      && attr->flavor == f && f == FL_PROCEDURE)
+  if (attr->flavor == f && f == FL_PROCEDURE
+      && gfc_new_block && gfc_new_block->abr_modproc_decl)
     return true;

   if (attr->flavor != FL_UNKNOWN)

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

* Re: [PATCH] PR fortran/99147 - Sanitizer detects heap-use-after-free in gfc_add_flavor
  2021-02-18 21:12 [PATCH] PR fortran/99147 - Sanitizer detects heap-use-after-free in gfc_add_flavor Harald Anlauf
@ 2021-02-19  8:34 ` Paul Richard Thomas
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Richard Thomas @ 2021-02-19  8:34 UTC (permalink / raw)
  To: Harald Anlauf; +Cc: fortran, gcc-patches

Hi Harald,

It looks 'obvious' to me too and is certainly OK for master.

Thanks

Paul


On Thu, 18 Feb 2021 at 21:30, Harald Anlauf via Fortran <fortran@gcc.gnu.org>
wrote:

> Dear all,
>
> the PR reports an issue detected with an ASAN instrumented compiler,
> which can also be verified with valgrind.  It appears that the state
> of gfc_new_block could be such that it should not be dereferenced.
> Reversing the order of condition evaluation helped.
>
> I failed to find out why this should happen, but then other places
> in the code put dereferences of gfc_new_block behind other checks.
> Simple things like initializing gfc_new_block with NULL in decl.c
> did not help.
>
> Regtested on x86_64-pc-linux-gnu.  No testcase added since the issue
> can be found only with an instrumented compiler or valgrind.
>
> I consider the patch to be obvious and trivial, but post it here
> in case somebody wants to dig deeper.
>
> OK for master?
>
> Thanks,
> Harald
>
>
> PR fortran/99147 - Sanitizer detects heap-use-after-free in gfc_add_flavor
>
> Reverse order of conditions to avoid invalid read.
>
> gcc/fortran/ChangeLog:
>
>         * symbol.c (gfc_add_flavor): Reverse order of conditions.
>
>

-- 
"If you can't explain it simply, you don't understand it well enough" -
Albert Einstein

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

end of thread, other threads:[~2021-02-19  8:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-18 21:12 [PATCH] PR fortran/99147 - Sanitizer detects heap-use-after-free in gfc_add_flavor Harald Anlauf
2021-02-19  8:34 ` Paul Richard Thomas

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