[Bug rtl-optimization/77425] Pointer test follows dereference in sched-int.h

2022-11-10 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77425

--- Comment #7 from CVS Commits  ---
The master branch has been updated by David Malcolm :

https://gcc.gnu.org/g:5c6546ca7d8cab1f1c129f5b55f709e2ceee0f94

commit r13-3884-g5c6546ca7d8cab1f1c129f5b55f709e2ceee0f94
Author: David Malcolm 
Date:   Thu Nov 10 13:23:56 2022 -0500

analyzer: new warning: -Wanalyzer-deref-before-check [PR99671]

This patch implements a new -Wanalyzer-deref-before-check within
-fanalyzer.  It complains about code paths in which a pointer is checked
for NULL after it has already been dereferenced.

For example, for the testcase in PR 77432 the diagnostic emits:
deref-before-check-1.c: In function 'test_from_pr77432':
deref-before-check-1.c:6:8: warning: check of 'a' for NULL after already
dereferencing it [-Wanalyzer-deref-before-check]
6 | if (a)
  |^
  'test_from_pr77432': events 1-2
|
|5 | int b = *a;
|  | ^
|  | |
|  | (1) pointer 'a' is dereferenced here
|6 | if (a)
|  |~
|  ||
|  |(2) pointer 'a' is checked for NULL here but it was
already dereferenced at (1)
|

and in PR 77425 we had an instance of this hidden behind a
macro, which the diagnostic complains about as follows:

deref-before-check-pr77425.c: In function 'get_odr_type':
deref-before-check-pr77425.c:35:10: warning: check of 'odr_types_ptr' for
NULL after already dereferencing it [-Wanalyzer-deref-before-check]
   35 |   if (odr_types_ptr)
  |  ^
  'get_odr_type': events 1-3
|
|   27 |   if (cond)
|  |  ^
|  |  |
|  |  (1) following 'false' branch...
|..
|   31 |   else if (other_cond)
|  |   ~~~
|  |   ||
|  |   |(2) ...to here
|  |   (3) following 'true' branch...
|
  'get_odr_type': event 4
|
|   11 | #define odr_types (*odr_types_ptr)
|  |   ~^~~
|  ||
|  |(4) ...to here
deref-before-check-pr77425.c:33:7: note: in expansion of macro 'odr_types'
|   33 |   odr_types[val->id] = 0;
|  |   ^
|
  'get_odr_type': event 5
|
|   11 | #define odr_types (*odr_types_ptr)
|  |   ~^~~
|  ||
|  |(5) pointer 'odr_types_ptr' is dereferenced
here
deref-before-check-pr77425.c:33:7: note: in expansion of macro 'odr_types'
|   33 |   odr_types[val->id] = 0;
|  |   ^
|
  'get_odr_type': event 6
|
|   35 |   if (odr_types_ptr)
|  |  ^
|  |  |
|  |  (6) pointer 'odr_types_ptr' is checked for NULL here
but it was already dereferenced at (5)
|

gcc/analyzer/ChangeLog:
PR analyzer/99671
* analyzer.opt (Wanalyzer-deref-before-check): New warning.
* diagnostic-manager.cc
(null_assignment_sm_context::set_next_state): Only add state
change events for transition to "null" state.
(null_assignment_sm_context::is_transition_to_null): New.
* engine.cc (impl_region_model_context::on_pop_frame): New.
* exploded-graph.h (impl_region_model_context::on_pop_frame): New
decl.
* program-state.cc (sm_state_map::clear_any_state): New.
(sm_state_map::can_merge_with_p): New.
(program_state::can_merge_with_p): Replace requirement that
sm-states be equal in favor of an attempt to merge them.
* program-state.h (sm_state_map::clear_any_state): New decl.
(sm_state_map::can_merge_with_p): New decl.
* region-model.cc (region_model::eval_condition): Make const.
(region_model::pop_frame): Call ctxt->on_pop_frame.
* region-model.h (region_model::eval_condition): Make const.
(region_model_context::on_pop_frame): New vfunc.
(noop_region_model_context::on_pop_frame): New.
(region_model_context_decorator::on_pop_frame): New.
* sm-malloc.cc (enum resource_state): Add RS_ASSUMED_NON_NULL.
(allocation_state::dump_to_pp): Drop "final".
(struct assumed_non_null_state): New subclass.
(malloc_state_machine::m_assumed_non_null): New.
(assumed_non_null_p): New.
(class deref_before_check): New.
(assumed_non_null_state::dump_to_pp): New.
   
(malloc_state_machine::get_or_create_assumed_non_null_state_for_frame):
  

[Bug rtl-optimization/77425] Pointer test follows dereference in sched-int.h

2016-09-16 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77425

Jakub Jelinek  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |FIXED

--- Comment #6 from Jakub Jelinek  ---
Fixed.

[Bug rtl-optimization/77425] Pointer test follows dereference in sched-int.h

2016-09-15 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77425

--- Comment #5 from Jakub Jelinek  ---
Author: jakub
Date: Thu Sep 15 12:27:35 2016
New Revision: 240163

URL: https://gcc.gnu.org/viewcvs?rev=240163=gcc=rev
Log:
PR rtl-optimization/77425
* sched-int.h (sd_iterator_cond): Don't update it_ptr->linkp if list
is NULL.

Modified:
trunk/gcc/ChangeLog
trunk/gcc/sched-int.h

[Bug rtl-optimization/77425] Pointer test follows dereference in sched-int.h

2016-09-05 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77425

--- Comment #4 from Jakub Jelinek  ---
Author: jakub
Date: Mon Sep  5 08:49:41 2016
New Revision: 239986

URL: https://gcc.gnu.org/viewcvs?rev=239986=gcc=rev
Log:
PR rtl-optimization/77425
* ipa-devirt.c (get_odr_type): Set val->id unconditionally.

Modified:
trunk/gcc/ChangeLog
trunk/gcc/ipa-devirt.c

[Bug rtl-optimization/77425] Pointer test follows dereference in sched-int.h

2016-09-01 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77425

--- Comment #3 from Jonathan Wakely  ---
(In reply to Manuel López-Ibáñez from comment #1)
> There is also this example:
> 
> odr_type
> get_odr_type (tree type, bool insert)
> {
>   
>   odr_types[val->id] = 0;
>   gcc_assert (val->derived_types.length() == 0);
>   if (odr_types_ptr)
> val->id = odr_types.length ();
>   
> }
> 
> which is even more insidious since it is hidden behind macros.

Yes that obfuscating macro seems like a horrible idea.

[Bug rtl-optimization/77425] Pointer test follows dereference in sched-int.h

2016-09-01 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77425

Jakub Jelinek  changed:

   What|Removed |Added

 CC||jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek  ---
Created attachment 39534
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=39534=edit
gcc7-pr77425.patch

Untested fix.  I believe if we are going to return false, then nothing
is/should be looking at the iterator object anymore, at least all other uses of
linkp field dereference it anyway and for NULL list >first gives an UB
NULL pointer.

[Bug rtl-optimization/77425] Pointer test follows dereference in sched-int.h

2016-08-31 Thread manu at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77425

Manuel López-Ibáñez  changed:

   What|Removed |Added

 CC||manu at gcc dot gnu.org

--- Comment #1 from Manuel López-Ibáñez  ---
There is also this example:

odr_type
get_odr_type (tree type, bool insert)
{
  
  odr_types[val->id] = 0;
  gcc_assert (val->derived_types.length() == 0);
  if (odr_types_ptr)
val->id = odr_types.length ();
  
}

which is even more insidious since it is hidden behind macros.