public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <ryxi@stu.xidian.edu.cn>
To: gcc-patches@gcc.gnu.org
Cc: ryxi@stu.xidian.edu.cn
Subject: [PATCH 2/6] New warnings -Wstring-plus-{char, int} (PR c++/62181)
Date: Mon, 12 Jun 2017 01:32:00 -0000	[thread overview]
Message-ID: <1497231174.27153.9.camel@stu.xidian.edu.cn> (raw)
In-Reply-To: <1497230800.27153.4.camel@stu.xidian.edu.cn>

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

This patch adds warning option -Wstring-plus-int for C/C++.

gcc/ChangeLog:

2017-06-12  Xi Ruoyao  <ryxi@stu.xidian.edu.cn>

	* c-family/c.opt: New option -Wstring-plus-int.
	* c-family/c-common.c (pointer_int_sum): Checking for
	-Wstring-plus-int.
---
 gcc/c-family/c-common.c | 25 +++++++++++++++++++++++++
 gcc/c-family/c.opt      |  5 +++++
 2 files changed, 30 insertions(+)

-- 
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University

[-- Attachment #2: 0002-New-warning-option-Wstring-plus-int.patch --]
[-- Type: text/x-patch, Size: 2253 bytes --]

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 4395e51..1eee48f 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -3100,6 +3100,31 @@ pointer_int_sum (location_t loc, enum tree_code resultcode,
   else
     size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
 
+  /* Handle -Wstring-plus-int, warn for adding string literals
+     and an integer which may result in a wild pointer.  */
+  if (warn_string_plus_int
+      && resultcode == PLUS_EXPR
+      && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (result_type))))
+    {
+      tree orig_ptrop = tree_strip_nop_conversions(ptrop);
+      if (TREE_CODE (orig_ptrop) == ADDR_EXPR)
+        {
+          tree obj = TREE_OPERAND (orig_ptrop, 0);
+          if (TREE_CODE (obj) == STRING_CST)
+            {
+              tree t = TYPE_DOMAIN (TREE_TYPE (obj));
+              if (TREE_CODE (intop) != INTEGER_CST
+                  || tree_int_cst_lt (intop, TYPE_MIN_VALUE (t))
+                  || int_cst_value (intop)
+                     > int_cst_value (TYPE_MAX_VALUE (t)) + 1)
+                warning_at (loc, OPT_Wstring_plus_int,
+                            "add %qT to string does not append to "
+                            "the string",
+                            TREE_TYPE (intop));
+            }
+        }
+    }
+
   /* We are manipulating pointer values, so we don't need to warn
      about relying on undefined signed overflow.  We disable the
      warning here because we use integer types so fold won't know that
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 37bb236..94ba3eb 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -732,6 +732,11 @@ C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_stringop_overflow) Ini
 Under the control of Object Size type, warn about buffer overflow in string
 manipulation functions like memcpy and strcpy.
 
+Wstring-plus-int
+C ObjC C++ ObjC++ Var(warn_string_plus_int) Warning
+Warn about adding strings and integers, which is likely an ill-formed
+attempt to append the string.
+
 Wsuggest-attribute=format
 C ObjC C++ ObjC++ Var(warn_suggest_attribute_format) Warning
 Warn about functions which might be candidates for format attributes.
-- 
2.7.1


  parent reply	other threads:[~2017-06-12  1:32 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-12  1:26 [PATCH 0/6] " Xi Ruoyao
2017-06-12  1:31 ` [PATCH 1/6] " Xi Ruoyao
2017-06-12  1:32 ` Xi Ruoyao [this message]
2017-06-19 16:51   ` [PATCH 2/6] " Martin Sebor
2017-06-19 17:28     ` Xi Ruoyao
2017-06-19 18:44       ` Martin Sebor
2017-06-19 19:36         ` Xi Ruoyao
2017-06-22 10:26         ` Xi Ruoyao
2017-07-15 16:33           ` Gerald Pfeifer
2017-06-12  1:34 ` [PATCH 3/6] " Xi Ruoyao
2017-06-19 16:30   ` Martin Sebor
2017-06-19 17:35     ` Xi Ruoyao
2017-06-12  1:36 ` [PATCH 4/6] " Xi Ruoyao
2017-06-12  1:39 ` [PATCH 6/6] " Xi Ruoyao
2017-06-19 16:57   ` Martin Sebor
2017-06-12  1:39 ` [PATCH 5/6] " Xi Ruoyao
2017-06-19 12:43 ` [PING PATCH 0/6] " Xi Ruoyao
2017-06-19 16:20 ` [PATCH " 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=1497231174.27153.9.camel@stu.xidian.edu.cn \
    --to=ryxi@stu.xidian.edu.cn \
    --cc=gcc-patches@gcc.gnu.org \
    /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).