+2018-08-29 Bernd Edlinger <bernd.edlinger@hotmail.de>
+
+ PR middle-end/87053
+ * builtins.c (c_strlen): Improve range checks.
+
2018-08-29 Martin Sebor <msebor@redhat.com>
Jeff Law <law@redhat.com>
tree
c_strlen (tree src, int only_value, unsigned eltsize)
{
- gcc_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
+ gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
STRIP_NOPS (src);
if (TREE_CODE (src) == COND_EXPR
&& (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
a null character if we can represent it as a single HOST_WIDE_INT. */
if (byteoff == 0)
eltoff = 0;
- else if (! tree_fits_shwi_p (byteoff))
+ else if (! tree_fits_uhwi_p (byteoff) || tree_to_uhwi (byteoff) % eltsize)
eltoff = -1;
else
- eltoff = tree_to_shwi (byteoff) / eltsize;
+ eltoff = tree_to_uhwi (byteoff) / eltsize;
/* If the offset is known to be out of bounds, warn, and call strlen at
runtime. */
unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
strelts - eltoff);
+ /* Don't know what to return if there was no zero termination.
+ Ideally this would turn into a gcc_checking_assert over time. */
+ if (len > maxelts - eltoff)
+ return NULL_TREE;
+
return ssize_int (len);
}
+2018-08-29 Bernd Edlinger <bernd.edlinger@hotmail.de>
+
+ PR middle-end/87053
+ * gcc.c-torture/execute/pr87053.c: New test.
+
2018-08-29 Jakub Jelinek <jakub@redhat.com>
PR c++/87095
--- /dev/null
+/* PR middle-end/87053 */
+
+const union
+{ struct {
+ char x[4];
+ char y[4];
+ };
+ struct {
+ char z[8];
+ };
+} u = {{"1234", "567"}};
+
+int main ()
+{
+ if (__builtin_strlen (u.z) != 7)
+ __builtin_abort ();
+}