public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Patch to fix an undefined behavior in fortran/decl.c
@ 2017-12-01 15:04 Qing Zhao
  2017-12-01 17:36 ` Thomas Koenig
  0 siblings, 1 reply; 2+ messages in thread
From: Qing Zhao @ 2017-12-01 15:04 UTC (permalink / raw)
  To: gcc-patches; +Cc: fortran, blomqvist.janne, tkoenig

Hi,

this is a very straightforward fix for an undefined behavior in fortran/decl.c:


In the man page of sprintf, it's clearly state:

===
NOTES
      Some programs imprudently rely on code such as the following

          sprintf(buf, "%s some further text", buf);

      to append text to buf.  However, the standards explicitly note that the  results
      are  undefined if source and destination buffers overlap when calling sprintf(),
      snprintf(), vsprintf(), and vsnprintf().  Depending on  the  version  of  gcc(1)
      used,  and  the compiler options employed, calls such as the above will not pro‐
      duce the expected results.
===

in gcc/fortran/decl.c, there is exactly such case as following:

3361       sprintf (name, "%s_%d", name, kind_value);

fixed it in this patch.

bootstraped and tested on both X86 and Aarch64. no regression.

Okay for trunk?

thanks.

Qing

====================================================
gcc/fortran/ChangeLog

2017-11-30  Qing Zhao  <qing.zhao@oracle.com <mailto:qing.zhao@oracle.com>>

       * fortran/decl.c (gfc_get_pdt_instance): Adjust the call to
       sprintf to avoid the same buffer being both source and
       destination.

---
gcc/fortran/decl.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
index e57cfde..02dda24 100644
--- a/gcc/fortran/decl.c
+++ b/gcc/fortran/decl.c
@@ -3358,7 +3358,7 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
       }

      gfc_extract_int (kind_expr, &kind_value);
-      sprintf (name, "%s_%d", name, kind_value);
+      sprintf (name + strlen (name), "_%d", kind_value);

      if (!name_seen && actual_param)
       actual_param = actual_param->next;
-- 
1.9.1

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

* Re: Patch to fix an undefined behavior in fortran/decl.c
  2017-12-01 15:04 Patch to fix an undefined behavior in fortran/decl.c Qing Zhao
@ 2017-12-01 17:36 ` Thomas Koenig
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Koenig @ 2017-12-01 17:36 UTC (permalink / raw)
  To: Qing Zhao, gcc-patches; +Cc: fortran, blomqvist.janne

HI Quing,

> this is a very straightforward fix for an undefined behavior in 
> fortran/decl.c:

 > -      sprintf (name, "%s_%d", name, kind_value);
 > +      sprintf (name + strlen (name), "_%d", kind_value);

OK for trunk. Thanks for the patch!

Regards

	Thomas

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

end of thread, other threads:[~2017-12-01 17:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-01 15:04 Patch to fix an undefined behavior in fortran/decl.c Qing Zhao
2017-12-01 17:36 ` Thomas Koenig

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