diff options
author | Siddhesh Poyarekar <siddhesh@gotplt.org> | 2021-11-15 23:03:14 +0530 |
---|---|---|
committer | Siddhesh Poyarekar <siddhesh@gotplt.org> | 2021-11-16 04:20:31 +0530 |
commit | cea4dab861bae6536dd5655a42d73f2c17f655f7 (patch) | |
tree | 205fe929f25b0dc3b051f1f72a11b3e71bf101e3 /gcc/testsuite/gcc.dg | |
parent | d1753b4be981fcb176f56b6ed45f7dbe1177d641 (diff) | |
download | gcc-cea4dab861bae6536dd5655a42d73f2c17f655f7.zip gcc-cea4dab861bae6536dd5655a42d73f2c17f655f7.tar.gz gcc-cea4dab861bae6536dd5655a42d73f2c17f655f7.tar.bz2 |
gimple-fold: Use ranges to simplify _chk calls
Instead of comparing LEN and SIZE only if they are constants, use their
ranges to decide if LEN will always be lower than or same as SIZE.
This change ends up putting the stringop-overflow warning line number
against the strcpy implementation, so adjust the warning check to be
line number agnostic.
gcc/ChangeLog:
* gimple-fold.c (known_lower): New function.
(gimple_fold_builtin_strncat_chk,
gimple_fold_builtin_memory_chk, gimple_fold_builtin_stxcpy_chk,
gimple_fold_builtin_stxncpy_chk,
gimple_fold_builtin_snprintf_chk,
gimple_fold_builtin_sprintf_chk): Use it.
gcc/testsuite/ChangeLog:
* gcc.dg/Wobjsize-1.c: Make warning change line agnostic.
* gcc.dg/fold-stringops-2.c: New test.
Signed-off-by: Siddhesh Poyarekar <siddhesh@gotplt.org>
Diffstat (limited to 'gcc/testsuite/gcc.dg')
-rw-r--r-- | gcc/testsuite/gcc.dg/Wobjsize-1.c | 5 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/fold-stringops-2.c | 49 |
2 files changed, 52 insertions, 2 deletions
diff --git a/gcc/testsuite/gcc.dg/Wobjsize-1.c b/gcc/testsuite/gcc.dg/Wobjsize-1.c index 2bd2f93..988b8bc 100644 --- a/gcc/testsuite/gcc.dg/Wobjsize-1.c +++ b/gcc/testsuite/gcc.dg/Wobjsize-1.c @@ -7,11 +7,12 @@ char buf[6]; int main(int argc, char **argv) { - strcpy (buf,"hello "); /* { dg-warning "\\\[-Wstringop-overflow" } */ + strcpy (buf,"hello "); return 0; } -/* { dg-message "file included" "included" { target *-*-* } 0 } +/* { dg-warning "\\\[-Wstringop-overflow" "warning" { target *-*-* } 0 } + { dg-message "file included" "included" { target *-*-* } 0 } { dg-message "inlined from" "inlined" { target *-*-* } 0 } The test might emit two warnings, one for the strcpy call and diff --git a/gcc/testsuite/gcc.dg/fold-stringops-2.c b/gcc/testsuite/gcc.dg/fold-stringops-2.c new file mode 100644 index 0000000..0b415df --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-stringops-2.c @@ -0,0 +1,49 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +#define bos(__d) __builtin_object_size ((__d), 0) + +char * +safe1 (const char *src, int cond, __SIZE_TYPE__ len) +{ + char *dst; + + if (cond) + dst = __builtin_malloc (1024); + else + dst = __builtin_malloc (2048); + + len = len > 2048 ? 2048 : len; + + return __builtin___memcpy_chk (dst, src, len, bos (dst)); +} + +char * +safe2 (const char *src, int cond, unsigned char len) +{ + char *dst; + + if (cond) + dst = __builtin_malloc (1024); + else + dst = __builtin_malloc (2048); + + return __builtin___strncpy_chk (dst, src, len, bos (dst)); +} + +int +safe3 (const char *src, int cond, unsigned char len) +{ + char *dst; + + if (cond) + dst = __builtin_malloc (1024); + else + dst = __builtin_malloc (2048); + + return __builtin___snprintf_chk (dst, len, 0, bos (dst), "%s", src); +} + +/* { dg-final { scan-assembler-not "__memcpy_chk" } } */ +/* { dg-final { scan-assembler-not "__strncpy_chk" } } */ +/* { dg-final { scan-assembler-not "__snprintf_chk" } } */ |