From abadc2f84638a70a3fa1f16edf1335ff1d78248f Mon Sep 17 00:00:00 2001 From: law Date: Thu, 13 Sep 2018 21:40:38 +0000 Subject: [PATCH] * c-typeck.c (digest_init): Shorten overlength strings. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@264291 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/c/ChangeLog | 4 ++++ gcc/c/c-typeck.c | 15 +++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 74d34b1ddb0..c28fb1c0ebc 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,7 @@ +2018-09-13 Bernd Edlinger + + * c-typeck.c (digest_init): Shorten overlength strings. + 2018-09-06 Bernd Edlinger * c-decl.c (finish_decl): Call complete_flexible_array_elts. diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 5f8df12564d..a7e2538e1ac 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -7485,19 +7485,17 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype, } } - TREE_TYPE (inside_init) = type; if (TYPE_DOMAIN (type) != NULL_TREE && TYPE_SIZE (type) != NULL_TREE && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) { unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); + unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT; /* Subtract the size of a single (possibly wide) character because it's ok to ignore the terminating null char that is counted in the length of the constant. */ - if (compare_tree_int (TYPE_SIZE_UNIT (type), - (len - (TYPE_PRECISION (typ1) - / BITS_PER_UNIT))) < 0) + if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0) pedwarn_init (init_loc, 0, ("initializer-string for array of chars " "is too long")); @@ -7506,8 +7504,17 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype, warning_at (init_loc, OPT_Wc___compat, ("initializer-string for array chars " "is too long for C++")); + if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0) + { + unsigned HOST_WIDE_INT size + = tree_to_uhwi (TYPE_SIZE_UNIT (type)); + const char *p = TREE_STRING_POINTER (inside_init); + + inside_init = build_string (size, p); + } } + TREE_TYPE (inside_init) = type; return inside_init; } else if (INTEGRAL_TYPE_P (typ1)) -- 2.11.4.GIT