Skip to content
  • Michael Haggerty's avatar
    lockfile: avoid transitory invalid states · 707103fd
    Michael Haggerty authored and Junio C Hamano's avatar Junio C Hamano committed
    
    
    Because remove_lock_file() can be called any time by the signal
    handler, it is important that any lock_file objects that are in the
    lock_file_list are always in a valid state.  And since lock_file
    objects are often reused (but are never removed from lock_file_list),
    that means we have to be careful whenever mutating a lock_file object
    to always keep it in a well-defined state.
    
    This was formerly not the case, because part of the state was encoded
    by setting lk->filename to the empty string vs. a valid filename.  It
    is wrong to assume that this string can be updated atomically; for
    example, even
    
        strcpy(lk->filename, value)
    
    is unsafe.  But the old code was even more reckless; for example,
    
        strcpy(lk->filename, path);
        if (!(flags & LOCK_NODEREF))
                resolve_symlink(lk->filename, max_path_len);
        strcat(lk->filename, ".lock");
    
    During the call to resolve_symlink(), lk->filename contained the name
    of the file that was being locked, not the name of the lockfile.  If a
    signal were raised during that interval, then the signal handler would
    have deleted the valuable file!
    
    We could probably continue to use the filename field to encode the
    state by being careful to write characters 1..N-1 of the filename
    first, and then overwrite the NUL at filename[0] with the first
    character of the filename, but that would be awkward and error-prone.
    
    So, instead of using the filename field to determine whether the
    lock_file object is active, add a new field "lock_file::active" for
    this purpose.  Be careful to set this field only when filename really
    contains the name of a file that should be deleted on cleanup.
    
    Helped-by: default avatarJohannes Sixt <j6t@kdbg.org>
    Signed-off-by: default avatarMichael Haggerty <mhagger@alum.mit.edu>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    707103fd