public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: Jeff Law <law@redhat.com>, Gcc Patch List <gcc-patches@gcc.gnu.org>
Subject: [PATCH 4/4] enhance overflow and truncation detection in strncpy and strncat (PR 81117)
Date: Sun, 06 Aug 2017 20:07:00 -0000	[thread overview]
Message-ID: <11f5f0e3-b3c6-233f-c736-91bbe36247ca@gmail.com> (raw)
In-Reply-To: <a0069a6d-d4a8-67df-e2f3-902dc7704ab2@redhat.com>

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

For completeness, patch 4 are the (already preapproved) fixups
to GCC to let it compile with the -Wstringop-truncation option.
I will commit this patch shortly.

Martin

[-- Attachment #2: gcc-81117-4.diff --]
[-- Type: text/x-patch, Size: 3119 bytes --]

PR c/81117 - Improve buffer overflow checking in strncpy

gcc/ada/ChangeLog:

	PR c/81117
	* adadecode.c (__gnat_decode): Replace pointless strncpy with
	memcpy.
	* argv.c (__gnat_fill_arg): Same.

gcc/c-family/ChangeLog:

	PR c/81117
	* c-common.c (resort_sorted_fields): Replace pointless strncpy
	with memcpy.

gcc/fortran/ChangeLog:

	PR c/81117
	* decl.c (build_sym): Replace pointless strncpy with strcpy.

gcc/objc/ChangeLog:

	PR c/81117
	* objc-encoding.c (encode_type): Replace pointless strncpy with
	memcpy.

diff --git a/gcc/ada/adadecode.c b/gcc/ada/adadecode.c
index 8c9c7ab..0cbef81 100644
--- a/gcc/ada/adadecode.c
+++ b/gcc/ada/adadecode.c
@@ -330,7 +330,7 @@ __gnat_decode (const char *coded_name, char *ada_name, int verbose)
 	      }
 
 	    /* Write symbol in the space.  */
-	    strncpy (optoken, trans_table[k][1], oplen);
+	    memcpy (optoken, trans_table[k][1], oplen);
 	  }
 	else
 	  k++;
diff --git a/gcc/ada/argv.c b/gcc/ada/argv.c
index 430404e..aee0f88 100644
--- a/gcc/ada/argv.c
+++ b/gcc/ada/argv.c
@@ -92,7 +92,7 @@ void
 __gnat_fill_arg (char *a, int i)
 {
   if (gnat_argv != NULL)
-    strncpy (a, gnat_argv[i], strlen(gnat_argv[i]));
+    memcpy (a, gnat_argv[i], strlen (gnat_argv[i]));
 }
 
 int
@@ -118,7 +118,7 @@ void
 __gnat_fill_env (char *a, int i)
 {
   if (gnat_envp != NULL)
-    strncpy (a, gnat_envp[i], strlen (gnat_envp[i]));
+    memcpy (a, gnat_envp[i], strlen (gnat_envp[i]));
 }
 
 #ifdef __cplusplus
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index feb0904..8c18caf 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -5942,10 +5942,10 @@ resort_sorted_fields (void *obj,
 static char *
 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
 {
-  const int lhs_size = strlen (lhs);
+  const size_t lhs_size = strlen (lhs);
   char *result = XNEWVEC (char, lhs_size + rhs_size);
-  strncpy (result, lhs, lhs_size);
-  strncpy (result + lhs_size, rhs_start, rhs_size);
+  memcpy (result, lhs, lhs_size);
+  memcpy (result + lhs_size, rhs_start, rhs_size);
   return result;
 }
 
diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
index 54ee5d3..82971e8 100644
--- a/gcc/fortran/decl.c
+++ b/gcc/fortran/decl.c
@@ -1417,11 +1417,9 @@ build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
     {
       char u_name[GFC_MAX_SYMBOL_LEN + 1];
       gfc_symtree *st;
-      int nlen;
 
-      nlen = strlen(name);
-      gcc_assert (nlen <= GFC_MAX_SYMBOL_LEN);
-      strncpy (u_name, name, nlen + 1);
+      gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
+      strcpy (u_name, name);
       u_name[0] = upper;
 
       st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
diff --git a/gcc/objc/objc-encoding.c b/gcc/objc/objc-encoding.c
index 2a2dfa5..e5d4f38 100644
--- a/gcc/objc/objc-encoding.c
+++ b/gcc/objc/objc-encoding.c
@@ -734,7 +734,7 @@ encode_type (tree type, int curtype, int format)
 
 	  /* Rewrite "in const" from "nr" to "rn".  */
 	  if (curtype >= 1 && !strncmp (enc - 1, "nr", 2))
-	    strncpy (enc - 1, "rn", 2);
+	    memcpy (enc - 1, "rn", 2);
 	}
     }
 }

  parent reply	other threads:[~2017-08-06 20:07 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-08 20:45 [PATCH] " Martin Sebor
2017-07-18  2:51 ` [PING] " Martin Sebor
2017-07-25  3:10   ` [PING #2] " Martin Sebor
2017-07-31 17:29 ` Jeff Law
2017-07-31 19:42   ` Martin Sebor
2017-08-02 16:59     ` Jeff Law
2017-08-06 20:07       ` [PATCH 2/4] " Martin Sebor
2017-08-10  6:39         ` Jeff Law
2017-08-14 18:04           ` Martin Sebor
2017-08-14 18:29             ` Joseph Myers
2017-08-14 19:26               ` Martin Sebor
2017-08-14 20:41                 ` Joseph Myers
2017-08-14 20:44                   ` Martin Sebor
2017-08-15  3:03                     ` Joseph Myers
2017-11-10 23:03         ` Marc Glisse
2017-11-11 21:10           ` Martin Sebor
2017-08-06 20:07       ` Martin Sebor [this message]
2017-08-06 20:07       ` [PATCH] " Martin Sebor
2017-08-09 19:21         ` Jeff Law
2017-08-06 20:07       ` [PATCH 3/4] " Martin Sebor
2017-08-10  7:17         ` Jeff Law
2017-08-10  7:39           ` Richard Biener
2017-08-10 20:21           ` Martin Sebor
2017-08-15  3:06             ` Martin Sebor
2017-08-23 21:11               ` [PING] " Martin Sebor
2017-08-29  5:07                 ` [PING 2] " Martin Sebor
2017-09-19 15:44                   ` [PING 3] " Martin Sebor
2017-09-26  2:27                     ` [PING 4] " Martin Sebor
2017-10-02 22:15             ` Jeff Law
2017-10-21  0:26               ` Martin Sebor
2017-11-04  3:49                 ` Jeff Law
2017-11-10  0:17                   ` Martin Sebor
2017-11-10  0:31                     ` Jeff Law
2017-11-14  9:24         ` [testsuite, committed] Require alloca for c-c++-common/Wstringop-truncation.c Tom de Vries
2017-11-15 15:30         ` [testsuite, committed] Compile strncpy-fix-1.c with -Wno-stringop-truncation Tom de Vries
2017-11-15 15:58           ` Martin Sebor
2017-08-06 20:07       ` [PATCH 1/4] enhance overflow and truncation detection in strncpy and strncat (PR 81117) Martin Sebor
2017-08-10  5:02         ` Jeff Law
2017-08-14 19:21           ` Martin Sebor

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=11f5f0e3-b3c6-233f-c736-91bbe36247ca@gmail.com \
    --to=msebor@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=law@redhat.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).