bfd_check_format_matches preserving matches vs. cleanups
authorAlan Modra <amodra@gmail.com>
Tue, 3 Mar 2020 09:57:36 +0000 (20:27 +1030)
committerAlan Modra <amodra@gmail.com>
Tue, 3 Mar 2020 11:15:01 +0000 (21:45 +1030)
commitf57140990f9be3232ffbc708fb1aade032052c80
treed189ac35c8ddb88c775e61a68addf7507469340b
parent478e490a4df79436d678ca5f5f1d7c7ac7befa79
bfd_check_format_matches preserving matches vs. cleanups

It didn't take long for oss-fuzz to find double frees due to a bug in
the cleanup logic.  It's seen when reading in any alpha-vms object
file except when alpha_vms_vec is the default.  But alpha_vms_vec is
of course the default when building for --target=alpha-dec-vms (and
naturally what I used to  test the cleanup support since that is the
only target with a cleanup that does anything currently).

Anyway, the bug is that if bfd_check_format_matches is to preserve a
match the cleanup for that match can't be run.  Quite obviously that
would destroy part of the match state.

* format.c (struct bfd_preserve): Add cleanup field.
(bfd_preserve_save): Add cleanup param and save.
(bfd_preserve_restore): Return cleanup.
(bfd_preserve_finish): Call the cleanup for the discarded match.
(bfd_check_format_matches): Pass cleanup to bfd_preserve_save,
and clear when preserving a match.  Restore cleanup too when
restoring that match.
bfd/ChangeLog
bfd/format.c