Add regression tests to check whether GCC folds unsafe builtins and
authormartynas <martynas@openbsd.org>
Sat, 28 Dec 2013 02:14:32 +0000 (02:14 +0000)
committermartynas <martynas@openbsd.org>
Sat, 28 Dec 2013 02:14:32 +0000 (02:14 +0000)
actually shows the security warning.  Also add some other cases
where GCC used to yield the warning but shouldn't (e.g. strncat
simplified into strcat).

28 files changed:
regress/gnu/egcs/Makefile
regress/gnu/egcs/gcc-builtins/Makefile [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/sprintf-1.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/sprintf-1.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/sprintf-2.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/sprintf-2.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/sprintf-3.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/sprintf-3.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/stpcpy-1.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/stpcpy-1.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/stpcpy-2.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/stpcpy-2.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/strcat-1.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/strcat-1.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/strcat-2.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/strcat-2.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/strcpy-1.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/strcpy-1.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/strcpy-2.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/strcpy-2.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/strncat-1.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/strncat-1.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/vsprintf-1.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/vsprintf-1.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/vsprintf-2.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/vsprintf-2.c.exp [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/vsprintf-3.c [new file with mode: 0644]
regress/gnu/egcs/gcc-builtins/vsprintf-3.c.exp [new file with mode: 0644]

index a7f3c47..962ff9d 100644 (file)
@@ -1,9 +1,10 @@
-# $OpenBSD: Makefile,v 1.6 2013/08/01 21:26:30 kettenis Exp $
+# $OpenBSD: Makefile,v 1.7 2013/12/28 02:14:32 martynas Exp $
 
 SUBDIR+= gcc libiberty
 
 .if defined(REGRESS_FULL)
 SUBDIR += gcc-bounds
+SUBDIR += gcc-builtins
 .endif
 
 install:
diff --git a/regress/gnu/egcs/gcc-builtins/Makefile b/regress/gnu/egcs/gcc-builtins/Makefile
new file mode 100644 (file)
index 0000000..ecfb1d3
--- /dev/null
@@ -0,0 +1,26 @@
+# $OpenBSD: Makefile,v 1.1 2013/12/28 02:14:32 martynas Exp $
+
+GCC_BUILTINS=  sprintf-1 sprintf-2 sprintf-3 stpcpy-1 stpcpy-2 strcat-1 \
+               strcat-2 strcpy-1 strcpy-2 strncat-1 vsprintf-1 vsprintf-2 \
+               vsprintf-3
+
+TCC=           gcc
+TCFLAGS=       -O2
+TCXXFLAGS=     ${TCFLAGS}
+
+.for i in ${GCC_BUILTINS}
+REGRESS_TARGETS+= c-${i}
+c-${i}:
+       ${TCC} ${TCFLAGS} -o /dev/null ${i}.c 2>&1 | cut -d: -f2- | diff -u - ${.CURDIR}/${i}.c.exp
+.endfor
+
+generate:
+.for i in ${REGRESS_TARGETS}
+       -${TCC} ${TCFLAGS} -o /dev/null ${i:C/^c-//g}.c 2>&1 | cut -d: -f2- >${i:C/^c-//g}.c.exp
+.endfor
+
+.PHONY:        ${REGRESS_TARGETS} generate regress
+
+NOOBJ= Yes
+
+.include <bsd.regress.mk>
diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-1.c b/regress/gnu/egcs/gcc-builtins/sprintf-1.c
new file mode 100644 (file)
index 0000000..6294490
--- /dev/null
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+int
+main(int argc, char **argv)
+{
+       char buf[512];
+       volatile int rv;
+
+       /* This expression cannot be folded. */
+       rv = sprintf(buf, "%s", argv[0]);
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-1.c.exp b/regress/gnu/egcs/gcc-builtins/sprintf-1.c.exp
new file mode 100644 (file)
index 0000000..6d3879e
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `main':
+ warning: sprintf() is often misused, please use snprintf()
diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-2.c b/regress/gnu/egcs/gcc-builtins/sprintf-2.c
new file mode 100644 (file)
index 0000000..49974e1
--- /dev/null
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+int
+main(int argc, char **argv)
+{
+       char buf[10];
+
+       /* This expression can be folded. */
+       sprintf(buf, "%s", "foo");
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-2.c.exp b/regress/gnu/egcs/gcc-builtins/sprintf-2.c.exp
new file mode 100644 (file)
index 0000000..6d3879e
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `main':
+ warning: sprintf() is often misused, please use snprintf()
diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-3.c b/regress/gnu/egcs/gcc-builtins/sprintf-3.c
new file mode 100644 (file)
index 0000000..cac6fa0
--- /dev/null
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+int
+main(int argc, char **argv)
+{
+       char buf[10];
+
+       /* This expression can be folded. */
+       sprintf(buf, "baz");
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-3.c.exp b/regress/gnu/egcs/gcc-builtins/sprintf-3.c.exp
new file mode 100644 (file)
index 0000000..6d3879e
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `main':
+ warning: sprintf() is often misused, please use snprintf()
diff --git a/regress/gnu/egcs/gcc-builtins/stpcpy-1.c b/regress/gnu/egcs/gcc-builtins/stpcpy-1.c
new file mode 100644 (file)
index 0000000..d950628
--- /dev/null
@@ -0,0 +1,13 @@
+#include <string.h>
+
+int
+main(int argc, char **argv)
+{
+       char buf[512];
+       volatile char *rv;
+
+       /* This expression cannot be folded. */
+       rv = stpcpy(buf, argv[0]);
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/stpcpy-1.c.exp b/regress/gnu/egcs/gcc-builtins/stpcpy-1.c.exp
new file mode 100644 (file)
index 0000000..ef88ade
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `main':
+ warning: stpcpy() is dangerous GNU crap; don't use it
diff --git a/regress/gnu/egcs/gcc-builtins/stpcpy-2.c b/regress/gnu/egcs/gcc-builtins/stpcpy-2.c
new file mode 100644 (file)
index 0000000..5700ea9
--- /dev/null
@@ -0,0 +1,12 @@
+#include <string.h>
+
+int
+main(int argc, char **argv)
+{
+       char buf[10];
+
+       /* This expression can be folded. */
+       stpcpy(buf, "foo");
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/stpcpy-2.c.exp b/regress/gnu/egcs/gcc-builtins/stpcpy-2.c.exp
new file mode 100644 (file)
index 0000000..ef88ade
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `main':
+ warning: stpcpy() is dangerous GNU crap; don't use it
diff --git a/regress/gnu/egcs/gcc-builtins/strcat-1.c b/regress/gnu/egcs/gcc-builtins/strcat-1.c
new file mode 100644 (file)
index 0000000..3694d7c
--- /dev/null
@@ -0,0 +1,13 @@
+#include <string.h>
+
+int
+main(int argc, char **argv)
+{
+       char buf[512];
+       volatile char *rv;
+
+       /* This expression cannot be folded. */
+       rv = strcat(buf, argv[0]);
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/strcat-1.c.exp b/regress/gnu/egcs/gcc-builtins/strcat-1.c.exp
new file mode 100644 (file)
index 0000000..e0fe608
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `main':
+ warning: strcat() is almost always misused, please use strlcat()
diff --git a/regress/gnu/egcs/gcc-builtins/strcat-2.c b/regress/gnu/egcs/gcc-builtins/strcat-2.c
new file mode 100644 (file)
index 0000000..52364a7
--- /dev/null
@@ -0,0 +1,12 @@
+#include <string.h>
+
+int
+main(int argc, char **argv)
+{
+       char buf[10];
+
+       /* This expression can be folded. */
+       strcat(buf, "foo");
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/strcat-2.c.exp b/regress/gnu/egcs/gcc-builtins/strcat-2.c.exp
new file mode 100644 (file)
index 0000000..e0fe608
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `main':
+ warning: strcat() is almost always misused, please use strlcat()
diff --git a/regress/gnu/egcs/gcc-builtins/strcpy-1.c b/regress/gnu/egcs/gcc-builtins/strcpy-1.c
new file mode 100644 (file)
index 0000000..1c87e6a
--- /dev/null
@@ -0,0 +1,13 @@
+#include <string.h>
+
+int
+main(int argc, char **argv)
+{
+       char buf[512];
+       volatile char *rv;
+
+       /* This expression cannot be folded. */
+       rv = strcpy(buf, argv[0]);
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/strcpy-1.c.exp b/regress/gnu/egcs/gcc-builtins/strcpy-1.c.exp
new file mode 100644 (file)
index 0000000..2528526
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `main':
+ warning: strcpy() is almost always misused, please use strlcpy()
diff --git a/regress/gnu/egcs/gcc-builtins/strcpy-2.c b/regress/gnu/egcs/gcc-builtins/strcpy-2.c
new file mode 100644 (file)
index 0000000..3862ff1
--- /dev/null
@@ -0,0 +1,12 @@
+#include <string.h>
+
+int
+main(int argc, char **argv)
+{
+       char buf[10];
+
+       /* This expression can be folded. */
+       strcpy(buf, "foo");
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/strcpy-2.c.exp b/regress/gnu/egcs/gcc-builtins/strcpy-2.c.exp
new file mode 100644 (file)
index 0000000..2528526
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `main':
+ warning: strcpy() is almost always misused, please use strlcpy()
diff --git a/regress/gnu/egcs/gcc-builtins/strncat-1.c b/regress/gnu/egcs/gcc-builtins/strncat-1.c
new file mode 100644 (file)
index 0000000..389dd3f
--- /dev/null
@@ -0,0 +1,13 @@
+#include <string.h>
+
+int
+main(int argc, char **argv)
+{
+       char foo[10];
+       const char bar[] = "bar";
+
+       /* The compiler should not simplify this into strcat. */
+       strncat(foo, bar, sizeof(foo));
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/strncat-1.c.exp b/regress/gnu/egcs/gcc-builtins/strncat-1.c.exp
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-1.c b/regress/gnu/egcs/gcc-builtins/vsprintf-1.c
new file mode 100644 (file)
index 0000000..f0a4340
--- /dev/null
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+void
+test_vsprintf(int unused, ...)
+{
+       char buf[512];
+       volatile int rv;
+       va_list ap;
+
+       va_start(ap, unused);
+
+       /* This expression cannot be folded. */
+       rv = vsprintf(buf, "%s", ap);
+
+       va_end(ap);
+}
+
+int
+main(int argc, char **argv)
+{
+       test_vsprintf(0, argv[0]);
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-1.c.exp b/regress/gnu/egcs/gcc-builtins/vsprintf-1.c.exp
new file mode 100644 (file)
index 0000000..836db06
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `test_vsprintf':
+ warning: vsprintf() is often misused, please use vsnprintf()
diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-2.c b/regress/gnu/egcs/gcc-builtins/vsprintf-2.c
new file mode 100644 (file)
index 0000000..fc8c772
--- /dev/null
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+void
+test_vsprintf(int unused, ...)
+{
+       char buf[10];
+       volatile int rv;
+       va_list ap;
+
+       va_start(ap, unused);
+
+       /* This expression can be folded. */
+       rv = vsprintf(buf, "%s", ap);
+
+       va_end(ap);
+}
+
+int
+main(int argc, char **argv)
+{
+       test_vsprintf(0, "foo");
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-2.c.exp b/regress/gnu/egcs/gcc-builtins/vsprintf-2.c.exp
new file mode 100644 (file)
index 0000000..836db06
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `test_vsprintf':
+ warning: vsprintf() is often misused, please use vsnprintf()
diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-3.c b/regress/gnu/egcs/gcc-builtins/vsprintf-3.c
new file mode 100644 (file)
index 0000000..a0d5b9e
--- /dev/null
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+void
+test_vsprintf(int unused, ...)
+{
+       char buf[10];
+       volatile int rv;
+       va_list ap;
+
+       va_start(ap, unused);
+
+       /* This expression can be folded. */
+       rv = vsprintf(buf, "bar", ap);
+
+       va_end(ap);
+}
+
+int
+main(int argc, char **argv)
+{
+       test_vsprintf(0);
+
+       return (1);
+}
diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-3.c.exp b/regress/gnu/egcs/gcc-builtins/vsprintf-3.c.exp
new file mode 100644 (file)
index 0000000..836db06
--- /dev/null
@@ -0,0 +1,2 @@
+ In function `test_vsprintf':
+ warning: vsprintf() is often misused, please use vsnprintf()