From: "Martin Liška" <mliska@suse.cz>
To: Richard Biener <richard.guenther@gmail.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Check \0-termination of string in c_getstr
Date: Wed, 12 Oct 2016 13:14:00 -0000 [thread overview]
Message-ID: <9ab67f18-42a3-d66e-6777-b066f6d9af76@suse.cz> (raw)
In-Reply-To: <CAFiYyc1zmcyTsSkR8-XJ1L8eXOEqS2+G-wEChkPdKiw+GBka8g@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 684 bytes --]
On 10/11/2016 12:28 PM, Richard Biener wrote:
> On Tue, Oct 11, 2016 at 11:27 AM, Martin Liška <mliska@suse.cz> wrote:
>> As mentioned in the email that I reply to, c_getstr should check
>> null termination of string constants.
>>
>> Tests of the whole series have been running.
>
> Looks ok to me (if testing passes).
Thanks for the review, however I decided to enhance the API to support
a requested length argument (if a string is not null terminated)
and to return length of the string(usable for strn* functions folding).
Patch can bootstrap on ppc64le-redhat-linux and survives regression tests as
a whole series.
Martin
>
> Thanks,
> Richard.
>
>> Thanks,
>> Martin
[-- Attachment #2: 0002-Enhance-c_getstr-API.patch --]
[-- Type: text/x-patch, Size: 3289 bytes --]
From e6c16ea038104ef15b087ff9fca23d9b2406e65e Mon Sep 17 00:00:00 2001
From: marxin <mliska@suse.cz>
Date: Mon, 10 Oct 2016 12:13:12 +0200
Subject: [PATCH] Enhance c_getstr API
gcc/ChangeLog:
2016-10-10 Martin Liska <mliska@suse.cz>
* fold-const.c (c_getstr): Guard string termination, or validate
that requested length is within a string constant.
* fold-const.h (c_getstr): Set default value for the new arg.
---
gcc/fold-const.c | 44 +++++++++++++++++++++++++++++++++++---------
gcc/fold-const.h | 3 ++-
2 files changed, 37 insertions(+), 10 deletions(-)
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 02aa484..eb53e84 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -14440,24 +14440,50 @@ 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.
+ If the string constant is properly zero-terminated, the constant is returned.
+ Otherwise, if REQ_LENGTH is a non-negative number, the constant
+ is returned if the string length is greater or equal to REQ_LENGTH.
+ If STRLEN is a valid pointer, length (including terminatinch character)
+ of returned string is stored to the argument. */
const char *
-c_getstr (tree src)
+c_getstr (tree src, HOST_WIDE_INT req_length, 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);
+ }
- return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
+ unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
+ const char *string = TREE_STRING_POINTER (src);
+ if (offset > string_length)
+ return NULL;
+
+ /* If the string is properly '\0' character terminated, return it. */
+ if ((string_length > 0 && string[string_length - 1] == 0)
+ || (req_length != -1
+ && (unsigned HOST_WIDE_INT)req_length <= string_length - offset))
+ {
+ if (strlen)
+ *strlen = string_length - offset;
+ return string + offset;
+ }
+ else
+ return NULL;
}
#if CHECKING_P
diff --git a/gcc/fold-const.h b/gcc/fold-const.h
index 637e46b..bbf831a 100644
--- a/gcc/fold-const.h
+++ b/gcc/fold-const.h
@@ -182,7 +182,8 @@ 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 src, HOST_WIDE_INT req_length = -1,
+ 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
next prev parent reply other threads:[~2016-10-12 13:14 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-17 6:55 [PATCH 0/3] Better folding of 2 string builtin-ins marxin
2016-08-17 6:55 ` [PATCH 3/3] Test folding of strn{case}cmp and memchr marxin
2016-08-17 7:52 ` Martin Liška
2016-10-07 8:42 ` [PATCH 3/3] Test folding of str{n}{case}cmp and memchr (version 2) Martin Liška
2016-10-11 9:39 ` [PATCH] Test folding of str{n}{case}cmp and memchr (version 3) Martin Liška
2016-10-12 8:35 ` Richard Biener
2016-10-12 13:20 ` Martin Liška
2016-10-13 15:27 ` [PATCH] Test folding of str{n}{case}cmp and memchr (simplified version 4) Martin Liška
2016-10-14 11:58 ` Richard Biener
2016-08-17 6:55 ` [PATCH 1/3] Fold BUILT_IN_STRNCASECMP marxin
2016-08-17 7:10 ` Jakub Jelinek
2016-08-17 7:52 ` Martin Liška
2016-10-07 8:39 ` [PATCH 1/3] Fold __builtin_str{n}{case}cmp functions (version 2) Martin Liška
2016-10-07 10:50 ` Richard Biener
2016-10-11 9:26 ` Martin Liška
2016-10-11 10:27 ` Richard Biener
2016-10-11 9:28 ` [PATCH] Add a helper function: create_tmp Martin Liška
2016-10-11 10:30 ` Richard Biener
2016-10-11 10:31 ` Richard Biener
2016-10-12 10:50 ` Martin Liška
2016-10-11 9:28 ` [PATCH] Check \0-termination of string in c_getstr Martin Liška
2016-10-11 10:28 ` Richard Biener
2016-10-12 13:14 ` Martin Liška [this message]
2016-10-13 15:24 ` [PATCH] Check \0-termination of string in c_getstr (simplified version) Martin Liška
2016-10-14 9:38 ` Richard Biener
2016-10-14 11:10 ` Martin Liška
2016-10-11 9:34 ` [PATCH] Fold __builtin_str{n}{case}cmp functions (version 3) Martin Liška
2016-10-12 8:30 ` Richard Biener
2016-10-12 13:17 ` Martin Liška
2016-10-13 15:25 ` [PATCH] Fold __builtin_str{n}{case}cmp functions (simplified version 4) Martin Liška
2016-10-14 11:55 ` Richard Biener
2016-08-17 6:55 ` [PATCH 2/3] Smarter folding of __builtin_memchr marxin
2016-08-17 8:41 ` Richard Biener
2016-10-07 8:41 ` [PATCH 2/3] Fold __builtin_memchr (version 2) Martin Liška
2016-10-07 11:01 ` Richard Biener
2016-10-11 9:38 ` [PATCH] Fold __builtin_memchr (version 3) Martin Liška
2016-10-12 8:34 ` Richard Biener
2016-10-12 8:36 ` Richard Biener
2016-10-12 13:19 ` Martin Liška
2016-10-13 15:26 ` [PATCH] Fold __builtin_memchr (simplified version 4) Martin Liška
2016-10-14 11:57 ` Richard Biener
2016-10-12 13:48 ` [RFC] Possible folding opportunities for string built-ins Martin Liška
2016-10-12 15:55 ` Joseph Myers
2016-10-12 19:45 ` Jim Wilson
2016-10-13 8:38 ` Richard Biener
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=9ab67f18-42a3-d66e-6777-b066f6d9af76@suse.cz \
--to=mliska@suse.cz \
--cc=gcc-patches@gcc.gnu.org \
--cc=richard.guenther@gmail.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).