]> gcc.gnu.org Git - gcc.git/commitdiff
Support only \0-terminated string in c_getstr and return
authorMartin Liska <mliska@suse.cz>
Fri, 14 Oct 2016 09:54:32 +0000 (11:54 +0200)
committerMartin Liska <marxin@gcc.gnu.org>
Fri, 14 Oct 2016 09:54:32 +0000 (09:54 +0000)
* fold-const.c (c_getstr): Support of properly \0-terminated
string constants.  New argument is added.
* fold-const.h: New argument is added.

From-SVN: r241152

gcc/ChangeLog
gcc/fold-const.c
gcc/fold-const.h

index fad5f2af953759cdc11a6e231c4104d877018c06..b907c6622d54eec21874e487c640e88280dc644b 100644 (file)
@@ -1,3 +1,9 @@
+2016-10-14  Martin Liska  <mliska@suse.cz>
+
+       * fold-const.c (c_getstr): Support of properly \0-terminated
+       string constants.  New argument is added.
+       * fold-const.h: New argument is added.
+
 2016-10-14  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>
 
        * config/aarch64/aarch64.c (aarch64_print_hint_for_core_or_arch):
index e2e05545fc02278f6e4056c61dfd76aabe038357..89ed89dc25cddadae2deccc8980469596ce1d5d1 100644 (file)
@@ -14441,24 +14441,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
index 637e46b0d48788217196ba53110b69d302b17db7..bc22c88b334d02d4f0125e0d026e30fc3ad2f7ed 100644 (file)
@@ -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.  */
This page took 0.112878 seconds and 5 git commands to generate.