From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 108616 invoked by alias); 13 Oct 2016 15:24:18 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 102678 invoked by uid 89); 13 Oct 2016 15:24:05 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_PASS autolearn=ham version=3.3.2 spammy=terminating, null-terminated, nullterminated, bonus X-HELO: mx2.suse.de Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 13 Oct 2016 15:23:55 +0000 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 17E16ACBD; Thu, 13 Oct 2016 15:23:53 +0000 (UTC) Subject: [PATCH] Check \0-termination of string in c_getstr (simplified version) To: Richard Biener References: <678ff58e-4aa3-6145-f56b-780bf618338c@suse.cz> <1db7cd13-d403-9a6c-811a-bba82a35ef37@suse.cz> <9ab67f18-42a3-d66e-6777-b066f6d9af76@suse.cz> Cc: GCC Patches From: =?UTF-8?Q?Martin_Li=c5=a1ka?= Message-ID: Date: Thu, 13 Oct 2016 15:24:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2 MIME-Version: 1.0 In-Reply-To: <9ab67f18-42a3-d66e-6777-b066f6d9af76@suse.cz> Content-Type: multipart/mixed; boundary="------------7BD6298B6F21631ACB60011A" X-IsSubscribed: yes X-SW-Source: 2016-10/txt/msg01048.txt.bz2 This is a multi-part message in MIME format. --------------7BD6298B6F21631ACB60011A Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-length: 393 Hello. After receiving feedback from Richi and Wilco Dijkstra, I decided to fully not support not null-terminated strings. It brings more complications and the code has started to be overengineered. Thus c_getstr accepts only such strings and as a bonus it returns length of a string. Patch can bootstrap on ppc64le-redhat-linux and survives regression tests. Ready to be installed? Martin --------------7BD6298B6F21631ACB60011A Content-Type: text/x-patch; name="0001-Support-only-0-terminated-string-in-c_getstr-and-ret-simplified.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Support-only-0-terminated-string-in-c_getstr-and-ret-si"; filename*1="mplified.patch" Content-length: 2935 >From bee44f0dedc86b1c354e21dd87dad6313147dcc3 Mon Sep 17 00:00:00 2001 From: marxin Date: Thu, 13 Oct 2016 10:20:12 +0200 Subject: [PATCH 1/4] Support only \0-terminated string in c_getstr and return strlen gcc/ChangeLog: 2016-10-13 Martin Liska * fold-const.c (c_getstr): Support of properly \0-terminated string constants. New argument is added. * fold-const.h: New argument is added. --- gcc/fold-const.c | 38 +++++++++++++++++++++++++++++--------- gcc/fold-const.h | 2 +- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 02aa484..57a9243 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -14440,24 +14440,44 @@ fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off) } /* Return a char pointer for a C string if it is a string constant - or sum of string constant and integer constant. */ + or sum of string constant and integer constant. We only support + string constants properly terminated with '\0' character. + If STRLEN is a valid pointer, length (including terminating character) + of returned string is stored to the argument. */ const char * -c_getstr (tree src) +c_getstr (tree src, unsigned HOST_WIDE_INT *strlen) { tree offset_node; + if (strlen) + *strlen = 0; + src = string_constant (src, &offset_node); if (src == 0) - return 0; + return NULL; - if (offset_node == 0) - return TREE_STRING_POINTER (src); - else if (!tree_fits_uhwi_p (offset_node) - || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0) - return 0; + unsigned HOST_WIDE_INT offset = 0; + if (offset_node != NULL_TREE) + { + if (!tree_fits_uhwi_p (offset_node)) + return NULL; + else + offset = tree_to_uhwi (offset_node); + } + + unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src); + const char *string = TREE_STRING_POINTER (src); + + /* Support only properly null-terminated strings. */ + if (string_length == 0 + || string[string_length - 1] != '\0' + || offset > string_length) + return NULL; - return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node); + if (strlen) + *strlen = string_length - offset; + return string + offset; } #if CHECKING_P diff --git a/gcc/fold-const.h b/gcc/fold-const.h index 637e46b..bc22c88 100644 --- a/gcc/fold-const.h +++ b/gcc/fold-const.h @@ -182,7 +182,7 @@ extern bool expr_not_equal_to (tree t, const wide_int &); extern tree const_unop (enum tree_code, tree, tree); extern tree const_binop (enum tree_code, tree, tree, tree); extern bool negate_mathfn_p (combined_fn); -extern const char *c_getstr (tree); +extern const char *c_getstr (tree, unsigned HOST_WIDE_INT *strlen = NULL); /* Return OFF converted to a pointer offset type suitable as offset for POINTER_PLUS_EXPR. Use location LOC for this conversion. */ -- 2.9.2 --------------7BD6298B6F21631ACB60011A--