public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-3290] analyzer: handle strdup and strndup
@ 2020-09-18 21:39 David Malcolm
0 siblings, 0 replies; only message in thread
From: David Malcolm @ 2020-09-18 21:39 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:c89956cba9d1a5fbf059f7880ff49418718a2965
commit r11-3290-gc89956cba9d1a5fbf059f7880ff49418718a2965
Author: David Malcolm <dmalcolm@redhat.com>
Date: Wed Sep 16 17:21:32 2020 -0400
analyzer: handle strdup and strndup
gcc/analyzer/ChangeLog:
* sm-malloc.cc (malloc_state_machine::on_stmt): Handle strdup and
strndup as being malloc-like allocators.
gcc/testsuite/ChangeLog:
* gcc.dg/analyzer/strdup-1.c: New test.
* gcc.dg/analyzer/strndup-1.c: New test.
Diff:
---
gcc/analyzer/sm-malloc.cc | 4 +++-
gcc/testsuite/gcc.dg/analyzer/strdup-1.c | 21 +++++++++++++++++++++
gcc/testsuite/gcc.dg/analyzer/strndup-1.c | 21 +++++++++++++++++++++
3 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/gcc/analyzer/sm-malloc.cc b/gcc/analyzer/sm-malloc.cc
index 2b5a870d35f..90d1da14586 100644
--- a/gcc/analyzer/sm-malloc.cc
+++ b/gcc/analyzer/sm-malloc.cc
@@ -984,7 +984,9 @@ malloc_state_machine::on_stmt (sm_context *sm_ctxt,
|| is_std_named_call_p (callee_fndecl, "malloc", call, 1)
|| is_std_named_call_p (callee_fndecl, "calloc", call, 2)
|| is_named_call_p (callee_fndecl, "__builtin_malloc", call, 1)
- || is_named_call_p (callee_fndecl, "__builtin_calloc", call, 2))
+ || is_named_call_p (callee_fndecl, "__builtin_calloc", call, 2)
+ || is_named_call_p (callee_fndecl, "strdup", call, 1)
+ || is_named_call_p (callee_fndecl, "strndup", call, 2))
{
on_allocator_call (sm_ctxt, call, m_malloc);
return true;
diff --git a/gcc/testsuite/gcc.dg/analyzer/strdup-1.c b/gcc/testsuite/gcc.dg/analyzer/strdup-1.c
new file mode 100644
index 00000000000..6b950ca23a9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/strdup-1.c
@@ -0,0 +1,21 @@
+#include <string.h>
+#include <stdlib.h>
+
+extern void requires_nonnull (void *ptr)
+ __attribute__((nonnull));
+
+void test_1 (const char *s)
+{
+ char *p = strdup (s); /* { dg-message "allocated here" } */
+} /* { dg-warning "leak of 'p'" } */
+
+void test_2 (const char *s)
+{
+ char *p = strdup (s);
+ free (p);
+}
+void test_3 (const char *s)
+{
+ char *p = strdup (s); /* { dg-message "this call could return NULL" } */
+ requires_nonnull (p); /* { dg-warning "use of possibly-NULL 'p'" } */
+}
diff --git a/gcc/testsuite/gcc.dg/analyzer/strndup-1.c b/gcc/testsuite/gcc.dg/analyzer/strndup-1.c
new file mode 100644
index 00000000000..23d9b6070ce
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/strndup-1.c
@@ -0,0 +1,21 @@
+#include <string.h>
+#include <stdlib.h>
+
+extern void requires_nonnull (void *ptr)
+ __attribute__((nonnull));
+
+void test_1 (const char *s)
+{
+ char *p = strndup (s, 42); /* { dg-message "allocated here" } */
+} /* { dg-warning "leak of 'p'" } */
+
+void test_2 (const char *s)
+{
+ char *p = strndup (s, 42);
+ free (p);
+}
+void test_3 (const char *s)
+{
+ char *p = strndup (s, 42); /* { dg-message "this call could return NULL" } */
+ requires_nonnull (p); /* { dg-warning "use of possibly-NULL 'p'" } */
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2020-09-18 21:39 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-18 21:39 [gcc r11-3290] analyzer: handle strdup and strndup David Malcolm
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).