Skip to content
  • Jeff King's avatar
    files_read_raw_ref: avoid infinite loop on broken symlinks · 3f7bd767
    Jeff King authored and Junio C Hamano's avatar Junio C Hamano committed
    Our ref resolution first runs lstat() on any path we try to
    look up, because we want to treat symlinks specially (by
    resolving them manually and considering them symrefs). But
    if the results of `readlink` do _not_ look like a ref, we
    fall through to treating it like a normal file, and just
    read the contents of the linked path.
    
    Since fcb7c762
    
     (resolve_ref_unsafe(): close race condition
    reading loose refs, 2013-06-19), that "normal file" code
    path will stat() the file and if we see ENOENT, will jump
    back to the lstat(), thinking we've seen inconsistent
    results between the two calls. But for a symbolic ref, this
    isn't a race: the lstat() found the symlink, and the stat()
    is looking at the path it points to. We end up in an
    infinite loop calling lstat() and stat().
    
    We can fix this by avoiding the retry-on-inconsistent jump
    when we know that we found a symlink. While we're at it,
    let's add a comment explaining why the symlink case gets to
    this code in the first place; without that, it is not
    obvious that the correct solution isn't to avoid the stat()
    code path entirely.
    
    Signed-off-by: default avatarJeff King <peff@peff.net>
    Reviewed-by: default avatarMichael Haggerty <mhagger@alum.mit.edu>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    3f7bd767