abspath: convert real_path_internal() to strbuf
Use strbuf instead of fixed-sized buffers in real_path() in order to avoid the size limitations of the latter. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jul 28, 2014 at 20:28 UTC
2fdb9ce0673b9197214e0fc12a47a8b335561cd8
1 file changed
+25
-44
abspath.c
+25
-44
@@ -33,7 +33,7 @@ int is_directory(const char *path)
33
*/
34
static const char *real_path_internal(const char *path, int die_on_error)
35
{
36
- static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
36
+ static struct strbuf sb = STRBUF_INIT;
37
char *retval = NULL;
38
39
/*
@@ -43,14 +43,12 @@ static const char *real_path_internal(const char *path, int die_on_error)
43
*/
44
struct strbuf cwd = STRBUF_INIT;
45
46
- int buf_index = 1;
47
-
46
int depth = MAXDEPTH;
47
char *last_elem = NULL;
48
struct stat st;
49
50
/* We've already done it */
53
- if (path == buf || path == next_buf)
51
+ if (path == sb.buf)
52
return path;
53
54
if (!*path) {
@@ -60,26 +58,22 @@ static const char *real_path_internal(const char *path, int die_on_error)
58
goto error_out;
59
}
60
63
- if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) {
64
- if (die_on_error)
65
- die("Too long path: %.*s", 60, path);
66
- else
67
- goto error_out;
68
- }
61
+ strbuf_reset(&sb);
62
+ strbuf_addstr(&sb, path);
63
64
while (depth--) {
71
- if (!is_directory(buf)) {
72
- char *last_slash = find_last_dir_sep(buf);
65
+ if (!is_directory(sb.buf)) {
66
+ char *last_slash = find_last_dir_sep(sb.buf);
67
if (last_slash) {
68
last_elem = xstrdup(last_slash + 1);
75
- last_slash[1] = '\0';
69
+ strbuf_setlen(&sb, last_slash - sb.buf + 1);
70
} else {
77
- last_elem = xstrdup(buf);
78
- *buf = '\0';
71
+ last_elem = xmemdupz(sb.buf, sb.len);
72
+ strbuf_reset(&sb);
73
}
74
}
75
82
- if (*buf) {
76
+ if (sb.len) {
77
if (!cwd.len && strbuf_getcwd(&cwd)) {
78
if (die_on_error)
79
die_errno("Could not get current working directory");
@@ -87,14 +81,15 @@ static const char *real_path_internal(const char *path, int die_on_error)
81
goto error_out;
82
}
83
90
- if (chdir(buf)) {
84
+ if (chdir(sb.buf)) {
85
if (die_on_error)
92
- die_errno("Could not switch to '%s'", buf);
86
+ die_errno("Could not switch to '%s'",
87
+ sb.buf);
88
else
89
goto error_out;
90
}
91
}
97
- if (!getcwd(buf, PATH_MAX)) {
92
+ if (strbuf_getcwd(&sb)) {
93
if (die_on_error)
94
die_errno("Could not get current working directory");
95
else
@@ -102,44 +97,30 @@ static const char *real_path_internal(const char *path, int die_on_error)
97
}
98
99
if (last_elem) {
105
- size_t len = strlen(buf);
106
- if (len + strlen(last_elem) + 2 > PATH_MAX) {
107
- if (die_on_error)
108
- die("Too long path name: '%s/%s'",
109
- buf, last_elem);
110
- else
111
- goto error_out;
112
- }
113
- if (len && !is_dir_sep(buf[len - 1]))
114
- buf[len++] = '/';
115
- strcpy(buf + len, last_elem);
100
+ if (sb.len && !is_dir_sep(sb.buf[sb.len - 1]))
101
+ strbuf_addch(&sb, '/');
102
+ strbuf_addstr(&sb, last_elem);
103
free(last_elem);
104
last_elem = NULL;
105
}
106
120
- if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
121
- ssize_t len = readlink(buf, next_buf, PATH_MAX);
107
+ if (!lstat(sb.buf, &st) && S_ISLNK(st.st_mode)) {
108
+ struct strbuf next_sb = STRBUF_INIT;
109
+ ssize_t len = strbuf_readlink(&next_sb, sb.buf, 0);
110
if (len < 0) {
111
if (die_on_error)
124
- die_errno("Invalid symlink '%s'", buf);
125
- else
126
- goto error_out;
127
- }
128
- if (PATH_MAX <= len) {
129
- if (die_on_error)
130
- die("symbolic link too long: %s", buf);
112
+ die_errno("Invalid symlink '%s'",
113
+ sb.buf);
114
else
115
goto error_out;
116
}
134
- next_buf[len] = '\0';
135
- buf = next_buf;
136
- buf_index = 1 - buf_index;
137
- next_buf = bufs[buf_index];
117
+ strbuf_swap(&sb, &next_sb);
118
+ strbuf_release(&next_sb);
119
} else
120
break;
121
}
122
142
- retval = buf;
123
+ retval = sb.buf;
124
error_out:
125
free(last_elem);
126
if (cwd.len && chdir(cwd.buf))