mirror of
git://gcc.gnu.org/git/gcc.git
synced 2025-02-19 10:49:58 +08:00
analyzer: add get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc [PR106286]
This patch adds get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc which could be used by SARIF output. Lightly tested on x86_64 Linux. gcc/analyzer/ChangeLog: PR analyzer/106286 * sm-fd.cc: (fd_diagnostic::get_meaning_for_state_change): New. gcc/testsuite/ChangeLog: PR analyzer/106286 * gcc.dg/analyzer/fd-meaning.c: New test. Signed-off-by: Immad Mir <mirimmad@outlook.com>
This commit is contained in:
parent
c2481a6bb7
commit
0f82c0ea8d
@ -229,6 +229,20 @@ public:
|
||||
return label_text ();
|
||||
}
|
||||
|
||||
diagnostic_event::meaning
|
||||
get_meaning_for_state_change (
|
||||
const evdesc::state_change &change) const final override
|
||||
{
|
||||
if (change.m_old_state == m_sm.get_start_state ()
|
||||
&& (m_sm.is_unchecked_fd_p (change.m_new_state)))
|
||||
return diagnostic_event::meaning (diagnostic_event::VERB_acquire,
|
||||
diagnostic_event::NOUN_resource);
|
||||
if (change.m_new_state == m_sm.m_closed)
|
||||
return diagnostic_event::meaning (diagnostic_event::VERB_release,
|
||||
diagnostic_event::NOUN_resource);
|
||||
return diagnostic_event::meaning ();
|
||||
}
|
||||
|
||||
protected:
|
||||
const fd_state_machine &m_sm;
|
||||
tree m_arg;
|
||||
|
37
gcc/testsuite/gcc.dg/analyzer/fd-meaning.c
Normal file
37
gcc/testsuite/gcc.dg/analyzer/fd-meaning.c
Normal file
@ -0,0 +1,37 @@
|
||||
/* { dg-additional-options "-fanalyzer-verbose-state-changes" } */
|
||||
int open(const char *, int mode);
|
||||
void close(int fd);
|
||||
|
||||
#define O_RDONLY 0
|
||||
#define O_WRONLY 1
|
||||
#define O_RDWR 2
|
||||
|
||||
void test_1 (const char* path)
|
||||
{
|
||||
int fd = open (path, O_RDWR); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
|
||||
if (fd != -1)
|
||||
{
|
||||
close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
|
||||
close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
|
||||
}
|
||||
}
|
||||
|
||||
void test_2 (const char* path)
|
||||
{
|
||||
int fd = open (path, O_RDONLY); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
|
||||
if (fd != -1)
|
||||
{
|
||||
close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
|
||||
close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
|
||||
}
|
||||
}
|
||||
|
||||
void test_3 (const char* path)
|
||||
{
|
||||
int fd = open (path, O_WRONLY); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
|
||||
if (fd != -1)
|
||||
{
|
||||
close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
|
||||
close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user