]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
diff: Fix a use after free as well as a memory leak in change().
authorJohn Baldwin <jhb@FreeBSD.org>
Mon, 3 Oct 2022 23:10:43 +0000 (16:10 -0700)
committerJohn Baldwin <jhb@FreeBSD.org>
Fri, 11 Nov 2022 18:18:53 +0000 (10:18 -0800)
commit1f9f319919d7f8b76c8d8a7edb71ca2f39fb40e1
tree8b23f428766584e846eb2aa53fe1751cfaa95f67
parent78a211c5eb5492ef76ae320747b80f59a456b010
diff: Fix a use after free as well as a memory leak in change().

When -B or -I are used, change() evaluates the lines in a hunk to
determine if it is a hunk that should be ignored.  It does this by
reading each candidate line into a mallocated buffer via preadline()
and then calling ignoreline().  Previously the buffer was freed as a
side effect of ignoreline_pattern() called from ignoreline().
However, if only -B was specified, then ignoreline_pattern() was not
called and the lines were leaked.  If both options were specified,
then ignoreline_pattern() was called before checking for a blank line
so that the second check was a use after free.

To fix, pull the free() out of ignoreline_pattern() and instead do it
up in change() so that is paired with preadline().

While here, simplify ignoreline() by checking for the -B and -I cases
individually without a separate clause for when both are set.  Also,
do the cheaper check (-B) first, and remove a false comment (this
function is only called if at least one of -I or -B are specified).

Reviewed by: emaste
Reported by: GCC 12 -Wuse-after-free
Differential Revision: https://reviews.freebsd.org/D36822

(cherry picked from commit 3736b2dd327050d2e6c925964b210eccbaac51ab)
usr.bin/diff/diffreg.c