public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Qing Zhao <qing.zhao@oracle.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>
Cc: fortran@gcc.gnu.org, blomqvist.janne@gmail.com, tkoenig@netcologne.de
Subject: Patch to fix an undefined behavior in fortran/decl.c
Date: Fri, 01 Dec 2017 15:04:00 -0000	[thread overview]
Message-ID: <47AB1118-E914-4ABB-BCE9-89A4C4867768@oracle.com> (raw)

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

             reply	other threads:[~2017-12-01 15:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-01 15:04 Qing Zhao [this message]
2017-12-01 17:36 ` Thomas Koenig

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=47AB1118-E914-4ABB-BCE9-89A4C4867768@oracle.com \
    --to=qing.zhao@oracle.com \
    --cc=blomqvist.janne@gmail.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=tkoenig@netcologne.de \
    /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).