entry.c: convert checkout_entry to use strbuf

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>

Nguyễn Thái Ngọc Duy committed Oct 24, 2013 at 08:55 UTC fd356f6aa8bb75ebef56fbc61caf7e02517fa6d3
1 file changed +8 -5
entry.c
+8 -5
@@ -237,16 +237,19 @@ static int check_path(const char *path, int len, struct stat *st, int skiplen)
237 int checkout_entry(struct cache_entry *ce,
238 const struct checkout *state, char *topath)
239 {
240 - static char path[PATH_MAX + 1];
240 + static struct strbuf path_buf = STRBUF_INIT;
241 + char *path;
242 struct stat st;
242 - int len = state->base_dir_len;
243 + int len;
244
245 if (topath)
246 return write_entry(ce, topath, state, 1);
247
247 - memcpy(path, state->base_dir, len);
248 - strcpy(path + len, ce->name);
249 - len += ce_namelen(ce);
248 + strbuf_reset(&path_buf);
249 + strbuf_add(&path_buf, state->base_dir, state->base_dir_len);
250 + strbuf_add(&path_buf, ce->name, ce_namelen(ce));
251 + path = path_buf.buf;
252 + len = path_buf.len;
253
254 if (!check_path(path, len, &st, state->base_dir_len)) {
255 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);