Skip to content

Commit fd356f6

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f737ac commit fd356f6

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

entry.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,19 @@ static int check_path(const char *path, int len, struct stat *st, int skiplen)
237237
int checkout_entry(struct cache_entry *ce,
238238
const struct checkout *state, char *topath)
239239
{
240-
static char path[PATH_MAX + 1];
240+
static struct strbuf path_buf = STRBUF_INIT;
241+
char *path;
241242
struct stat st;
242-
int len = state->base_dir_len;
243+
int len;
243244

244245
if (topath)
245246
return write_entry(ce, topath, state, 1);
246247

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;
250253

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

0 commit comments

Comments
 (0)