public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-6639] analyzer, testsuite: add test coverage for various builtins
Date: Mon, 13 Mar 2023 18:54:44 +0000 (GMT)	[thread overview]
Message-ID: <20230313185444.97C0C3858D1E@sourceware.org> (raw)

https://gcc.gnu.org/g:fa9d3aa67894c8cadf9840be424b566a8c53c8e4

commit r13-6639-gfa9d3aa67894c8cadf9840be424b566a8c53c8e4
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Mon Mar 13 14:53:04 2023 -0400

    analyzer, testsuite: add test coverage for various builtins
    
    gcc/testsuite/ChangeLog:
            * gcc.dg/analyzer/exec-1.c: New test.
            * gcc.dg/analyzer/snprintf-concat.c: New test.
            * gcc.dg/analyzer/vsnprintf-1.c: New test.
    
    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

Diff:
---
 gcc/testsuite/gcc.dg/analyzer/exec-1.c          | 43 +++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/analyzer/snprintf-concat.c | 35 ++++++++++++++++++++
 gcc/testsuite/gcc.dg/analyzer/vsnprintf-1.c     | 11 +++++++
 3 files changed, 89 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/analyzer/exec-1.c b/gcc/testsuite/gcc.dg/analyzer/exec-1.c
new file mode 100644
index 00000000000..6b71118bd54
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/exec-1.c
@@ -0,0 +1,43 @@
+#define NULL ((void *)0)
+
+extern int execl(const char *pathname, const char *arg, ...);
+extern int execlp(const char *file, const char *arg, ...);
+extern int execle(const char *pathname, const char *arg, ...);
+extern int execv(const char *pathname, char *const argv[]);
+extern int execvp(const char *file, char *const argv[]);
+extern int execvpe(const char *file, char *const argv[], char *const envp[]);
+
+int test_execl_ls_al ()
+{
+  return execl ("/usr/bin/ls", "ls", "-al", NULL);
+}
+
+int test_execlpl_ls_al ()
+{
+  return execlp ("ls", "ls", "-al", NULL);
+}
+
+int test_execle_ls_al ()
+{
+  const char *env[3] = {"FOO=BAR", "BAZ", NULL};
+  return execl ("/usr/bin/ls", "ls", "-al", NULL, env);
+}
+
+int test_execv_ls_al ()
+{
+  char *argv[3] = {"ls", "-al", NULL};
+  return execv ("/usr/bin/ls", argv);
+}
+
+int test_execvp_ls_al ()
+{
+  char *argv[3] = {"ls", "-al", NULL};
+  return execvp ("ls", argv);
+}
+
+int test_execvpe_ls_al ()
+{
+  char *env[3] = {"FOO=BAR", "BAZ", NULL};
+  char *argv[3] = {"ls", "-al", NULL};
+  return execvpe ("ls", argv, env);
+}
diff --git a/gcc/testsuite/gcc.dg/analyzer/snprintf-concat.c b/gcc/testsuite/gcc.dg/analyzer/snprintf-concat.c
new file mode 100644
index 00000000000..a557dee6e44
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/snprintf-concat.c
@@ -0,0 +1,35 @@
+typedef __SIZE_TYPE__ size_t;
+#define NULL ((void *)0)
+
+extern size_t
+strlen(const char* __s) __attribute__((__nothrow__, __leaf__))
+__attribute__((__pure__)) __attribute__((__nonnull__(1)));
+
+extern void*
+malloc(size_t __size) __attribute__((__nothrow__, __leaf__))
+__attribute__((__malloc__)) __attribute__((__alloc_size__(1)));
+
+extern int
+snprintf(char* __restrict __s, size_t size, const char* __restrict, ...)
+  __attribute__((__nothrow__));
+
+char *
+test_1 (const char *a, const char *b)
+{
+  size_t sz = strlen (a) + strlen (b) + 2;
+  char *p = malloc (sz);
+  if (!p)
+    return NULL;
+  snprintf (p, sz, "%s/%s", a, b);
+  return p;
+}
+
+void
+test_2 (const char *a, const char *b)
+{
+  size_t sz = strlen (a) + strlen (b) + 2;
+  char *p = malloc (sz); /* { dg-message "allocated here" "PR 107017" { xfail *-*-* } } */
+  if (!p)
+    return;
+  snprintf (p, sz, "%s/%s", a, b); /* { dg-warning "leak of 'p'" "PR 107017" { xfail *-*-* } } */
+}
diff --git a/gcc/testsuite/gcc.dg/analyzer/vsnprintf-1.c b/gcc/testsuite/gcc.dg/analyzer/vsnprintf-1.c
new file mode 100644
index 00000000000..209c4b6cbb6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/vsnprintf-1.c
@@ -0,0 +1,11 @@
+typedef __SIZE_TYPE__ size_t;
+
+int
+my_snprintf(char *pos, size_t left, const char *fmt, ...)
+{
+  __builtin_va_list ap;
+  __builtin_va_start(ap, fmt);
+  const int len = __builtin_vsnprintf(pos, left, fmt, ap);
+  __builtin_va_end(ap);
+  return len;
+}

                 reply	other threads:[~2023-03-13 18:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230313185444.97C0C3858D1E@sourceware.org \
    --to=dmalcolm@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).