public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Check the STRING_CSTs in varasm.c
@ 2018-08-01 11:35 Bernd Edlinger
  2018-08-03 21:36 ` Jeff Law
  0 siblings, 1 reply; 28+ messages in thread
From: Bernd Edlinger @ 2018-08-01 11:35 UTC (permalink / raw)
  To: gcc-patches, Richard Biener

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

Hi,

this completes the previous patches, and adds a check in varasm.c
that ensures that all string constants are NUL terminated,
And that varasm does not strip anything but _exactly_ one NUL
character.

Bootstrapped and reg-tested on x86_64-pc-linux-gnu.
Is it OK for trunk?


Thanks
Bernd.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch-varasm.diff --]
[-- Type: text/x-patch; name="patch-varasm.diff", Size: 1579 bytes --]

2018-08-01  Bernd Edlinger  <bernd.edlinger@hotmail.de>

	* varasm.c (check_string_literal): New checking function.
	(output_constant): Use it.

diff -pur gcc/varasm.c gcc/varasm.c
--- gcc/varasm.c	2018-07-17 11:19:27.000000000 +0200
+++ gcc/varasm.c	2018-07-31 10:16:12.058827505 +0200
@@ -4774,6 +4774,29 @@ initializer_constant_valid_for_bitfield_
   return false;
 }
 
+/* Check if a STRING_CST fits into the field.
+   Tolerate only the case when the NUL termination
+   does not fit into the field.   */
+
+bool
+check_string_literal (tree string, unsigned HOST_WIDE_INT size)
+{
+  tree eltype = TREE_TYPE (TREE_TYPE (string));
+  unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
+  const char *p = TREE_STRING_POINTER (string);
+  int len = TREE_STRING_LENGTH (string);
+
+  if (elts != 1 && elts != 2 && elts != 4)
+    return false;
+  if (len <= 0 || len % elts != 0)
+    return false;
+  if ((unsigned)len != size && (unsigned)len != size + elts)
+    return false;
+  if (memcmp (p + len - elts, "\0\0\0\0", elts) != 0)
+    return false;
+  return true;
+}
+
 /* output_constructor outer state of relevance in recursive calls, typically
    for nested aggregate bitfields.  */
 
@@ -4942,6 +4965,7 @@ output_constant (tree exp, unsigned HOST
 	case STRING_CST:
 	  thissize
 	    = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
+	  gcc_checking_assert (check_string_literal (exp, thissize));
 	  assemble_string (TREE_STRING_POINTER (exp), thissize);
 	  break;
 	case VECTOR_CST:

^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2018-09-13 21:44 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-01 11:35 [PATCH] Check the STRING_CSTs in varasm.c 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
2018-08-17 12:14       ` Richard Biener

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).