Skip to content
  • Martin Ågren's avatar
    regex: do not call `regfree()` if compilation fails · 17154b15
    Martin Ågren authored and Junio C Hamano's avatar Junio C Hamano committed
    It is apparently undefined behavior to call `regfree()` on a regex where
    `regcomp()` failed. The language in [1] is a bit muddy, at least to me,
    but the clearest hint is this (`preg` is the `regex_t *`):
    
        Upon successful completion, the regcomp() function shall return 0.
        Otherwise, it shall return an integer value indicating an error as
        described in <regex.h>, and the content of preg is undefined.
    
    Funnily enough, there is also the `regerror()` function which should be
    given a pointer to such a "failed" `regex_t` -- the content of which
    would supposedly be undefined -- and which may investigate it to come up
    with a detailed error message.
    
    In any case, the example in that document shows how `regfree()` is not
    called after `regcomp()` fails.
    
    We have quite a few users of this API and most get this right. These
    three users do not.
    
    Several implementations can handle this just fine [2] and these code paths
    supposedly have not wreaked havoc or we'd have heard about it. (These
    are all in code paths where git got bad input and is just about to die
    anyway.) But let's just avoid the issue altogether.
    
    [1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/regcomp.html
    
    [2] https://www.redhat.com/archives/libvir-list/2013-September/msg00262.html
    
    
    
    Researched-by: default avatarEric Sunshine <sunshine@sunshineco.com>
    Signed-off-byi Martin Ågren <martin.agren@gmail.com>
    
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    17154b15