Skip to content

Commit e7c7305

Browse files
kbleesgitster
authored andcommitted
symlinks: remove PATH_MAX limitation
'git checkout' fails if a directory is longer than PATH_MAX, because the lstat_cache in symlinks.c checks if the leading directory exists using PATH_MAX-bounded string operations. Remove the limitation by using strbuf instead. Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c2f7b10 commit e7c7305

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

cache.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -1068,12 +1068,16 @@ struct checkout {
10681068
extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath);
10691069

10701070
struct cache_def {
1071-
char path[PATH_MAX + 1];
1072-
int len;
1071+
struct strbuf path;
10731072
int flags;
10741073
int track_flags;
10751074
int prefix_len_stat_func;
10761075
};
1076+
#define CACHE_DEF_INIT { STRBUF_INIT, 0, 0, 0 }
1077+
static inline void cache_def_free(struct cache_def *cache)
1078+
{
1079+
strbuf_release(&cache->path);
1080+
}
10771081

10781082
extern int has_symlink_leading_path(const char *name, int len);
10791083
extern int threaded_has_symlink_leading_path(struct cache_def *, const char *, int);

preload-index.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ static void *preload_thread(void *_data)
3737
struct thread_data *p = _data;
3838
struct index_state *index = p->index;
3939
struct cache_entry **cep = index->cache + p->offset;
40-
struct cache_def cache;
40+
struct cache_def cache = CACHE_DEF_INIT;
4141

42-
memset(&cache, 0, sizeof(cache));
4342
nr = p->nr;
4443
if (nr + p->offset > index->cache_nr)
4544
nr = index->cache_nr - p->offset;
@@ -64,6 +63,7 @@ static void *preload_thread(void *_data)
6463
continue;
6564
ce_mark_uptodate(ce);
6665
} while (--nr > 0);
66+
cache_def_free(&cache);
6767
return NULL;
6868
}
6969

symlinks.c

+28-35
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ static int longest_path_match(const char *name_a, int len_a,
3535
return match_len;
3636
}
3737

38-
static struct cache_def default_cache;
38+
static struct cache_def default_cache = CACHE_DEF_INIT;
3939

4040
static inline void reset_lstat_cache(struct cache_def *cache)
4141
{
42-
cache->path[0] = '\0';
43-
cache->len = 0;
42+
strbuf_reset(&cache->path);
4443
cache->flags = 0;
4544
/*
4645
* The track_flags and prefix_len_stat_func members is only
@@ -73,7 +72,7 @@ static int lstat_cache_matchlen(struct cache_def *cache,
7372
int prefix_len_stat_func)
7473
{
7574
int match_len, last_slash, last_slash_dir, previous_slash;
76-
int save_flags, max_len, ret;
75+
int save_flags, ret;
7776
struct stat st;
7877

7978
if (cache->track_flags != track_flags ||
@@ -93,14 +92,14 @@ static int lstat_cache_matchlen(struct cache_def *cache,
9392
* the 2 "excluding" path types.
9493
*/
9594
match_len = last_slash =
96-
longest_path_match(name, len, cache->path, cache->len,
97-
&previous_slash);
95+
longest_path_match(name, len, cache->path.buf,
96+
cache->path.len, &previous_slash);
9897
*ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
9998

10099
if (!(track_flags & FL_FULLPATH) && match_len == len)
101100
match_len = last_slash = previous_slash;
102101

103-
if (*ret_flags && match_len == cache->len)
102+
if (*ret_flags && match_len == cache->path.len)
104103
return match_len;
105104
/*
106105
* If we now have match_len > 0, we would know that
@@ -121,21 +120,22 @@ static int lstat_cache_matchlen(struct cache_def *cache,
121120
*/
122121
*ret_flags = FL_DIR;
123122
last_slash_dir = last_slash;
124-
max_len = len < PATH_MAX ? len : PATH_MAX;
125-
while (match_len < max_len) {
123+
if (len > cache->path.len)
124+
strbuf_grow(&cache->path, len - cache->path.len);
125+
while (match_len < len) {
126126
do {
127-
cache->path[match_len] = name[match_len];
127+
cache->path.buf[match_len] = name[match_len];
128128
match_len++;
129-
} while (match_len < max_len && name[match_len] != '/');
130-
if (match_len >= max_len && !(track_flags & FL_FULLPATH))
129+
} while (match_len < len && name[match_len] != '/');
130+
if (match_len >= len && !(track_flags & FL_FULLPATH))
131131
break;
132132
last_slash = match_len;
133-
cache->path[last_slash] = '\0';
133+
cache->path.buf[last_slash] = '\0';
134134

135135
if (last_slash <= prefix_len_stat_func)
136-
ret = stat(cache->path, &st);
136+
ret = stat(cache->path.buf, &st);
137137
else
138-
ret = lstat(cache->path, &st);
138+
ret = lstat(cache->path.buf, &st);
139139

140140
if (ret) {
141141
*ret_flags = FL_LSTATERR;
@@ -158,12 +158,11 @@ static int lstat_cache_matchlen(struct cache_def *cache,
158158
* for the moment!
159159
*/
160160
save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
161-
if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
162-
cache->path[last_slash] = '\0';
163-
cache->len = last_slash;
161+
if (save_flags && last_slash > 0) {
162+
cache->path.buf[last_slash] = '\0';
163+
cache->path.len = last_slash;
164164
cache->flags = save_flags;
165-
} else if ((track_flags & FL_DIR) &&
166-
last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
165+
} else if ((track_flags & FL_DIR) && last_slash_dir > 0) {
167166
/*
168167
* We have a separate test for the directory case,
169168
* since it could be that we have found a symlink or a
@@ -175,8 +174,8 @@ static int lstat_cache_matchlen(struct cache_def *cache,
175174
* can still cache the path components before the last
176175
* one (the found symlink or non-existing component).
177176
*/
178-
cache->path[last_slash_dir] = '\0';
179-
cache->len = last_slash_dir;
177+
cache->path.buf[last_slash_dir] = '\0';
178+
cache->path.len = last_slash_dir;
180179
cache->flags = FL_DIR;
181180
} else {
182181
reset_lstat_cache(cache);
@@ -273,21 +272,18 @@ static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name
273272
FL_DIR;
274273
}
275274

276-
static struct removal_def {
277-
char path[PATH_MAX];
278-
int len;
279-
} removal;
275+
static struct strbuf removal = STRBUF_INIT;
280276

281277
static void do_remove_scheduled_dirs(int new_len)
282278
{
283279
while (removal.len > new_len) {
284-
removal.path[removal.len] = '\0';
285-
if (rmdir(removal.path))
280+
removal.buf[removal.len] = '\0';
281+
if (rmdir(removal.buf))
286282
break;
287283
do {
288284
removal.len--;
289285
} while (removal.len > new_len &&
290-
removal.path[removal.len] != '/');
286+
removal.buf[removal.len] != '/');
291287
}
292288
removal.len = new_len;
293289
}
@@ -297,7 +293,7 @@ void schedule_dir_for_removal(const char *name, int len)
297293
int match_len, last_slash, i, previous_slash;
298294

299295
match_len = last_slash = i =
300-
longest_path_match(name, len, removal.path, removal.len,
296+
longest_path_match(name, len, removal.buf, removal.len,
301297
&previous_slash);
302298
/* Find last slash inside 'name' */
303299
while (i < len) {
@@ -317,11 +313,8 @@ void schedule_dir_for_removal(const char *name, int len)
317313
* If we go deeper down the directory tree, we only need to
318314
* save the new path components as we go down.
319315
*/
320-
if (match_len < last_slash) {
321-
memcpy(&removal.path[match_len], &name[match_len],
322-
last_slash - match_len);
323-
removal.len = last_slash;
324-
}
316+
if (match_len < last_slash)
317+
strbuf_add(&removal, &name[match_len], last_slash - match_len);
325318
}
326319

327320
void remove_scheduled_dirs(void)

0 commit comments

Comments
 (0)