analyzer: fix ICE adding note to disabled diagnostic [PR104997]

gcc/analyzer/ChangeLog:
	PR analyzer/104997
	* diagnostic-manager.cc (diagnostic_manager::add_diagnostic):
	Convert return type from "void" to "bool", reporting success vs
	failure to caller, for both overloads.
	* diagnostic-manager.h (diagnostic_manager::add_diagnostic):
	Likewise.
	* engine.cc (impl_region_model_context::warn): Propagate return
	value from diagnostic_manager::add_diagnostic.

gcc/testsuite/ChangeLog:
	PR analyzer/104997
	* gcc.dg/analyzer/write-to-string-literal-4-disabled.c: New test,
	adapted from write-to-string-literal-4.c.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
This commit is contained in:
David Malcolm 2022-03-23 08:32:17 -04:00
parent 4894d69a1f
commit 160b095fc9
4 changed files with 45 additions and 14 deletions

View File

@ -874,9 +874,11 @@ diagnostic_manager::diagnostic_manager (logger *logger, engine *eng,
{
}
/* Queue pending_diagnostic D at ENODE for later emission. */
/* Queue pending_diagnostic D at ENODE for later emission.
Return true/false signifying if the diagnostic was actually added.
Take ownership of D (or delete it). */
void
bool
diagnostic_manager::add_diagnostic (const state_machine *sm,
exploded_node *enode,
const supernode *snode, const gimple *stmt,
@ -907,7 +909,7 @@ diagnostic_manager::add_diagnostic (const state_machine *sm,
d->get_kind ());
delete d;
m_num_disabled_diagnostics++;
return;
return false;
}
}
@ -920,18 +922,22 @@ diagnostic_manager::add_diagnostic (const state_machine *sm,
log ("adding saved diagnostic %i at SN %i to EN %i: %qs",
sd->get_index (),
snode->m_index, enode->m_index, d->get_kind ());
return true;
}
/* Queue pending_diagnostic D at ENODE for later emission. */
/* Queue pending_diagnostic D at ENODE for later emission.
Return true/false signifying if the diagnostic was actually added.
Take ownership of D (or delete it). */
void
bool
diagnostic_manager::add_diagnostic (exploded_node *enode,
const supernode *snode, const gimple *stmt,
stmt_finder *finder,
pending_diagnostic *d)
{
gcc_assert (enode);
add_diagnostic (NULL, enode, snode, stmt, finder, NULL_TREE, NULL, 0, d);
return add_diagnostic (NULL, enode, snode, stmt, finder, NULL_TREE,
NULL, 0, d);
}
/* Add PN to the most recent saved_diagnostic. */

View File

@ -107,7 +107,7 @@ public:
json::object *to_json () const;
void add_diagnostic (const state_machine *sm,
bool add_diagnostic (const state_machine *sm,
exploded_node *enode,
const supernode *snode, const gimple *stmt,
stmt_finder *finder,
@ -116,7 +116,7 @@ public:
state_machine::state_t state,
pending_diagnostic *d);
void add_diagnostic (exploded_node *enode,
bool add_diagnostic (exploded_node *enode,
const supernode *snode, const gimple *stmt,
stmt_finder *finder,
pending_diagnostic *d);

View File

@ -129,12 +129,9 @@ impl_region_model_context::warn (pending_diagnostic *d)
return false;
}
if (m_eg)
{
m_eg->get_diagnostic_manager ().add_diagnostic
(m_enode_for_diag, m_enode_for_diag->get_supernode (),
m_stmt, m_stmt_finder, d);
return true;
}
return m_eg->get_diagnostic_manager ().add_diagnostic
(m_enode_for_diag, m_enode_for_diag->get_supernode (),
m_stmt, m_stmt_finder, d);
else
{
delete d;

View File

@ -0,0 +1,28 @@
/* Verify that we can disable warnings that have notes added to them via
region_model_context_decorator::add_note. */
/* { dg-additional-options "-Wno-analyzer-write-to-string-literal" } */
typedef __SIZE_TYPE__ size_t;
int getrandom (void *__buffer, size_t __length, /* { dg-bogus "parameter 1 of 'getrandom' marked with attribute 'access \\(write_only, 1, 2\\)'" } */
unsigned int __flags)
__attribute__ ((access (__write_only__, 1, 2)));
#define GRND_RANDOM 0x02
void test (int flag)
{
char *buf;
if (flag)
buf = __builtin_malloc (1024);
else
buf = (char *)""; /* { dg-bogus "here" } */
if (getrandom(buf, 16, GRND_RANDOM)) /* { dg-bogus "write to string literal" } */
__builtin_printf("%s\n", buf);
if (flag)
__builtin_free (buf);
}