git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Potential problem in git-add may corrupt the index file

From
GHGang Hu <ganghu@cs.columbia.edu>
Date
Apr 23, 2012, 23:30 UTC
Message-ID
<CAEJt7hYYJh5dT1Tk99Oze6Hg4QaOqX5ERi0faWjyifQ6dr-pWw@mail.gmail.com>
Hi,

We are developing a tool to find bugs, and it flagged a potential problem in git-add.

We are checking git 1.7.9.4, and this problem still exists in the latest release, git 1.7.10. At the end of cmd_add() in builtin/add.c, there is a call to write_cache(), followed by commit_locked_index().

474: if (write_cache(newfd, active_cache, active_nr) || 475: commit_locked_index(&lock_file))

In write_cache(), the program wrote to the locked index file, and in commit_locked_index(), it did an atomic rename to rename the locked index file to the real index file. However, the locked index file is not synced to the disk before being renamed to the real index file. This may cause data loss, which has been pointed out in this post:

http://stackoverflow.com/questions/7433057/is-rename-without-fsync-safe

If a crash happens after the rename(), the data may have not been completely flushed into the index file, so the user may face an empty or corrupted index file. Although the user may just recreate the index file, he would need to add the files to be committed again, which may be inconvenient and disturbing.

To fix this problem, we may just do a fsync() before renaming the
file. We can add this to close_lock_file() in lockfile.c:
223: int close_lock_file(struct lock_file *lk)
224: {
225:        int fd = lk->fd;
226:        lk->fd = -1;
          // we may add
          // fsync(fd);
          // here
227:        return close(fd);
228:}

Is this a real problem? Any clarification or confirmation is appreciated. Thank you.

-- 
Cheers,
Gang
Next: Junio C Hamano
Message 1 of 3 in “Potential problem in git-add may corrupt the index file”
  1. Gang HuApr 23, 2012
  2. Junio C HamanoApr 24, 2012
  3. Gang HuApr 29, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.