From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1930) id 6A7613858424; Wed, 2 Mar 2022 00:06:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A7613858424 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Martin Sebor To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-7440] Add a test for true positives related to PR104715. X-Act-Checkin: gcc X-Git-Author: Martin Sebor X-Git-Refname: refs/heads/master X-Git-Oldrev: 1a0e3bba4b735fa8e4463d52950d0ce9c30c16c7 X-Git-Newrev: b11465f1150a11d6a9b186417c468c8191ae41a1 Message-Id: <20220302000640.6A7613858424@sourceware.org> Date: Wed, 2 Mar 2022 00:06:40 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Mar 2022 00:06:40 -0000 https://gcc.gnu.org/g:b11465f1150a11d6a9b186417c468c8191ae41a1 commit r12-7440-gb11465f1150a11d6a9b186417c468c8191ae41a1 Author: Martin Sebor Date: Tue Mar 1 16:56:06 2022 -0700 Add a test for true positives related to PR104715. Related to: PR tree-optimization/104715 - false dangling pointer with strstr gcc/testsuite/ChangeLog: PR tree-optimization/104715 * gcc.dg/Wdangling-pointer-3.c: New test. Diff: --- gcc/testsuite/gcc.dg/Wdangling-pointer-3.c | 92 ++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/gcc/testsuite/gcc.dg/Wdangling-pointer-3.c b/gcc/testsuite/gcc.dg/Wdangling-pointer-3.c new file mode 100644 index 00000000000..01859303fcf --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wdangling-pointer-3.c @@ -0,0 +1,92 @@ +/* PR tree-optimization/104715 - false dangling pointer with strstr + Vertify that using pointers that have become dangling after they were + passed to and returned from strstr is diagnosed. + { dg-do compile } + { dg-options "-Wall" } */ + +extern char* strstr (const char*, const char*); + +void sink (const void*); + +void nowarn_strstr_static (const char *s) +{ + char *t1; + + { + static const char a[] = "abc"; + t1 = strstr (a, s); + sink (t1); + } + + sink (t1); +} + + +void nowarn_strstr_lit (const char *s) +{ + char *t2; + + { + t2 = strstr ("def", s); + sink (t2); + } + + sink (t2); +} + + +void warn_strstr_comp_lit (const char *s) +{ + char *t3; + + { + const char *a = + (char[]){ '1', '\0' }; // { dg-message "unnamed temporary defined here" } + t3 = strstr (a, s); + sink (t3); + } + + sink (t3); // { dg-warning "using dangling pointer 't3' to an unnamed temporary" } +} + + +void warn_strstr_arg (const char *s) +{ + char *t4; + + { + char a[] = "1"; // { dg-message "'a' declared here" } + t4 = strstr (a, s); + sink (t4); + } + + sink (t4); // { dg-warning "using dangling pointer 't4' to 'a'" } +} + + +void warn_strstr_arg_plus_cst (const char *s) +{ + char *t5; + + { + char a[] = "12"; // { dg-message "'a' declared here" } + t5 = strstr (a + 1, s); + sink (t5); + } + + sink (t5); // { dg-warning "using dangling pointer 't5' to 'a'" } +} + + +void warn_strstr_arg_plus_var (const char *s, int i) +{ + char *t6; + + { + char a[] = "123"; // { dg-message "'a' declared here" } + t6 = strstr (a + i, s); + sink (t6++); + } + + sink (t6); // { dg-warning "using dangling pointer 't6' to 'a'" } +}