public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Bernd Edlinger <bernd.edlinger@hotmail.de>
To: Richard Biener <rguenther@suse.de>, Jeff Law <law@redhat.com>
Cc: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Check the STRING_CSTs in varasm.c
Date: Fri, 17 Aug 2018 10:22:00 -0000	[thread overview]
Message-ID: <AM5PR0701MB265777375A8AAA97B5B2FFC8E43D0@AM5PR0701MB2657.eurprd07.prod.outlook.com> (raw)
In-Reply-To: <alpine.LSU.2.20.1808171130410.16707@zhemvz.fhfr.qr>

Richard Biener wrote:

> Note that I'm a little bit confused here given build_string
> already appends a '\0' after TREE_STRING_LENGTH.  So it is safe
> to call strlen() on all STRING_CSTs.  I think I raised this in
> the review of one of Bernds patches but to be honest all the
> various threads have become a bit convoluted (esp. if joining
> in after two weeks of vacation).
>
> So -- why is what we do currently not enough?
>
> Quoting our single STRING_CST creator:
>
> tree
> build_string (int len, const char *str)
> {
>   tree s;
>   size_t length;
>
>  /* Do not waste bytes provided by padding of struct tree_string.  */
>  length = len + offsetof (struct tree_string, str) + 1;
>
>  record_node_allocation_statistics (STRING_CST, length);
>
>  s = (tree) ggc_internal_alloc (length);
>
>  memset (s, 0, sizeof (struct tree_typed));
>  TREE_SET_CODE (s, STRING_CST);
>  TREE_CONSTANT (s) = 1;
>  TREE_STRING_LENGTH (s) = len;
>  memcpy (s->string.str, str, len);
>  s->string.str[len] = '\0';
>
>  return s;
>}

Thanks for this question.

I have two issues with that.
1. it adds not a wide character nul, only a single byte '\0'.
2. the '\0' here is _guaranteed_ not to be assembled
    by varasm.
   Since it is at TREE_STRING_LENGTH+1.

That is fine for some string constants, like ASM constaraints.
it makes most of the time sense, as long as it is not
used for folding of memxxx functions.

Of course the whole patch series is dependent on the
consensus about how we want to handle string constants
in the middle-end, so no problem with going back to the
drawing board, that's the reason why I did not apply the
already approved bits, and I guess we are not in a hurry.

What I see, is that string constants are most of the time
handled like the C language strings, that is there
are different character wides, and the array_type on the
string constants, includes another NUL (wide) char which
is assembled along with the rest of the bytes, but it is
not written in the language string constant.
The front end appends this before giving the string
constant to build_string.
That means at least most of the time.

So I thought it might be good to have some more
easily checkable things regarding the zero termiation,
or what is allowed for excess precision.

It's possible that this shoots over the target, but I think
this hardening in the varasm is of some value.

This way how this patch works has one advantage:
That it is easy to check for "there must be one wide char zero",
if it is someting that can't be checked, like:
"there may be a nul or not", then it is impossible to be checked.

Well yeah, it's an idea.



Bernd.

  reply	other threads:[~2018-08-17 10:22 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-01 11:35 Bernd Edlinger
2018-08-03 21:36 ` Jeff Law
2018-08-03 21:42   ` Bernd Edlinger
2018-08-04  5:55   ` Bernd Edlinger
2018-08-05 10:28     ` Bernd Edlinger
2018-08-17  4:46       ` Jeff Law
2018-08-17 12:13         ` Bernd Edlinger
2018-08-18  3:43           ` Jeff Law
2018-08-17  9:38     ` Richard Biener
2018-08-17 10:38       ` Bernd Edlinger
2018-08-17 12:19         ` Richard Biener
2018-08-17 12:36           ` Bernd Edlinger
2018-08-17 13:38             ` Richard Biener
2018-08-17 13:53               ` Bernd Edlinger
2018-08-18  3:47                 ` Jeff Law
2018-08-22 14:27                 ` Bernd Edlinger
2018-08-22 20:54                   ` Martin Sebor
2018-08-22 22:52                     ` Bernd Edlinger
2018-08-24 20:18                     ` Bernd Edlinger
2018-09-13 18:44                       ` Jeff Law
2018-09-13 19:08                         ` Bernd Edlinger
2018-09-13 18:59                       ` Jeff Law
2018-09-13 19:51                         ` Bernd Edlinger
2018-09-13 21:32                           ` Jeff Law
2018-09-13 22:02                       ` Jeff Law
2018-08-17  9:33   ` Richard Biener
2018-08-17 10:22     ` Bernd Edlinger [this message]
2018-08-17 12:14       ` 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=AM5PR0701MB265777375A8AAA97B5B2FFC8E43D0@AM5PR0701MB2657.eurprd07.prod.outlook.com \
    --to=bernd.edlinger@hotmail.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=law@redhat.com \
    --cc=rguenther@suse.de \
    /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).