Skip to content

Commit

Permalink
entry.c: convert checkout_entry to use strbuf
Browse files Browse the repository at this point in the history
The old code does not do boundary check so any paths longer than
PATH_MAX can cause buffer overflow. Replace it with strbuf to handle
paths of arbitrary length.

The OS may reject if the path is too long though. But in that case we
report the cause (e.g. name too long) and usually move on to checking
out the next entry.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Nguyễn Thái Ngọc Duy authored and Junio C Hamano committed Oct 24, 2013
1 parent 5f737ac commit fd356f6
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,19 @@ static int check_path(const char *path, int len, struct stat *st, int skiplen)
int checkout_entry(struct cache_entry *ce,
const struct checkout *state, char *topath)
{
static char path[PATH_MAX + 1];
static struct strbuf path_buf = STRBUF_INIT;
char *path;
struct stat st;
int len = state->base_dir_len;
int len;

if (topath)
return write_entry(ce, topath, state, 1);

memcpy(path, state->base_dir, len);
strcpy(path + len, ce->name);
len += ce_namelen(ce);
strbuf_reset(&path_buf);
strbuf_add(&path_buf, state->base_dir, state->base_dir_len);
strbuf_add(&path_buf, ce->name, ce_namelen(ce));
path = path_buf.buf;
len = path_buf.len;

if (!check_path(path, len, &st, state->base_dir_len)) {
unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
Expand Down

0 comments on commit fd356f6

Please sign in to comment.