From 781ff3d80e88d7d0df019eb3e82ef2a3fb64429c Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Mon, 18 Jun 2018 16:32:59 +0000 Subject: PR tree-optimization/81384 - built-in form of strnlen missing gcc/ChangeLog: PR tree-optimization/81384 * builtin-types.def (BT_FN_SIZE_CONST_STRING_SIZE): New. * builtins.c (expand_builtin_strnlen): New function. (expand_builtin): Call it. (fold_builtin_n): Avoid setting TREE_NO_WARNING. * builtins.def (BUILT_IN_STRNLEN): New. * calls.c (maybe_warn_nonstring_arg): Handle BUILT_IN_STRNLEN. Warn for bounds in excess of maximum object size. * tree-ssa-strlen.c (maybe_set_strlen_range): Return tree representing single-value ranges. Handle strnlen. (handle_builtin_strlen): Handle strnlen. (strlen_check_and_optimize_stmt): Same. * doc/extend.texi (Other Builtins): Document strnlen. gcc/testsuite/ChangeLog: PR tree-optimization/81384 * gcc.c-torture/execute/builtins/lib/strnlen.c: New test. * gcc.c-torture/execute/builtins/strnlen-lib.c: New test. * gcc.c-torture/execute/builtins/strnlen.c: New test. * gcc.dg/attr-nonstring-2.c: New test. * gcc.dg/attr-nonstring-3.c: New test. * gcc.dg/attr-nonstring-4.c: New test. * gcc.dg/strlenopt-45.c: New test. * gcc.dg/strlenopt.h (strnlen): Declare. From-SVN: r261705 --- gcc/testsuite/gcc.dg/attr-nonstring-4.c | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/attr-nonstring-4.c (limited to 'gcc/testsuite/gcc.dg/attr-nonstring-4.c') diff --git a/gcc/testsuite/gcc.dg/attr-nonstring-4.c b/gcc/testsuite/gcc.dg/attr-nonstring-4.c new file mode 100644 index 0000000..7daff97 --- /dev/null +++ b/gcc/testsuite/gcc.dg/attr-nonstring-4.c @@ -0,0 +1,64 @@ +/* PR middle-end/81384 - built-in form of strnlen missing + + Verify that a strnlen bound in excess of the maximum object size + is diagnosed regardless of attribute nonstring. Also verify that + a bound that's greater than the size of a non-string array is + diagnosed, even if it's not in excess of the maximum object size. + + { dg-do compile } + { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */ + +#include "range.h" + +extern size_t strnlen (const char*, size_t); + +#define STR /* not non-string */ +#define NS __attribute__ ((nonstring)) + +#define _CAT(s, n) s ## n +#define CAT(s, n) _CAT (s, n) +#define UNIQ(n) CAT (n, __LINE__) + +void sink (size_t); + +#define T(attr, N, bound) \ + do { \ + extern attr char UNIQ (a)[N]; \ + sink (strnlen (UNIQ (a), bound)); \ + } while (0) + +void strnlen_cst (void) +{ + size_t n = DIFF_MAX; + + T (STR, /* [] */, n); + T (STR, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ + + T (STR, 1, n); + T (STR, 2, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ + + T (NS, /* [] */, n); + T (NS, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ + + T (NS, 9, n); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound" } */ + T (NS, 10, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ +} + + +void strnlen_range (void) +{ + size_t n = DIFF_MAX; + n = UR (n, n + 1); + + T (STR, /* [] */, n); + T (STR, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ + + T (STR, 1, n); + T (STR, 2, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ + + T (NS, /* [] */, n); + T (NS, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ + + T (NS, 9, n); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound" } */ + T (NS, 10, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ +} -- cgit v1.1