• Tom Lane's avatar
    Fix use-after-free issue in regexp engine. · f42ea835
    Tom Lane authored
    Commit cebc1d34 taught parseqatom() to optimize cases where a branch
    contains only one, "messy", atom by getting rid of excess subRE nodes.
    The way we really should do that is to keep the subRE built for the
    "messy" child atom; but to avoid changing parseqatom's nominal API,
    I made it delete that node after copying its fields to the outer subRE
    made by parsebranch().  It seems that that actually worked at the time;
    but it became dangerous after ea1268f6, because that later commit
    allowed the lower invocation of parse() to return a subRE that was also
    pointed to by some v->subs[] entry.  This meant we could wind up with a
    dangling pointer in v->subs[], allowing a later backref to misbehave,
    but only if that subRE struct had been reused in between.  So the damage
    seems confined to cases like '((...))...(...\2'.
    
    To fix, do what I should have done before and modify parseqatom's API
    to make it possible for it to remove the caller's subRE instead of the
    callee's.  That's safer because we know that subRE isn't complete yet,
    so noplace else will have a pointer to it.
    
    Per report from Mark Dilger.  Back-patch to v14 where the problematic
    patches came in.
    
    Discussion: https://postgr.es/m/0203588E-E609-43AF-9F4F-902854231EE7@enterprisedb.com
    f42ea835
regcomp.c 67.4 KB