Skip to content

Commit 493b7a0

Browse files
Clemens Buchachergitster
authored andcommitted
grep: accept relative paths outside current working directory
"git grep" would barf at relative paths pointing outside the current working directory (or subdirectories thereof). Use quote_path_relative(), which can handle such cases just fine. [jc: added tests.] Signed-off-by: Clemens Buchacher <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 929e37d commit 493b7a0

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

builtin-grep.c

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "tree-walk.h"
1212
#include "builtin.h"
1313
#include "grep.h"
14+
#include "quote.h"
1415

1516
#ifndef NO_EXTERNAL_GREP
1617
#ifdef __unix__
@@ -114,35 +115,22 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char
114115
unsigned long size;
115116
char *data;
116117
enum object_type type;
117-
char *to_free = NULL;
118118
int hit;
119+
struct strbuf pathbuf = STRBUF_INIT;
119120

120121
data = read_sha1_file(sha1, &type, &size);
121122
if (!data) {
122123
error("'%s': unable to read %s", name, sha1_to_hex(sha1));
123124
return 0;
124125
}
125126
if (opt->relative && opt->prefix_length) {
126-
static char name_buf[PATH_MAX];
127-
char *cp;
128-
int name_len = strlen(name) - opt->prefix_length + 1;
129-
130-
if (!tree_name_len)
131-
name += opt->prefix_length;
132-
else {
133-
if (ARRAY_SIZE(name_buf) <= name_len)
134-
cp = to_free = xmalloc(name_len);
135-
else
136-
cp = name_buf;
137-
memcpy(cp, name, tree_name_len);
138-
strcpy(cp + tree_name_len,
139-
name + tree_name_len + opt->prefix_length);
140-
name = cp;
141-
}
127+
quote_path_relative(name + tree_name_len, -1, &pathbuf, opt->prefix);
128+
strbuf_insert(&pathbuf, 0, name, tree_name_len);
129+
name = pathbuf.buf;
142130
}
143131
hit = grep_buffer(opt, name, data, size);
132+
strbuf_release(&pathbuf);
144133
free(data);
145-
free(to_free);
146134
return hit;
147135
}
148136

@@ -152,6 +140,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
152140
int i;
153141
char *data;
154142
size_t sz;
143+
struct strbuf buf = STRBUF_INIT;
155144

156145
if (lstat(filename, &st) < 0) {
157146
err_ret:
@@ -176,8 +165,9 @@ static int grep_file(struct grep_opt *opt, const char *filename)
176165
}
177166
close(i);
178167
if (opt->relative && opt->prefix_length)
179-
filename += opt->prefix_length;
168+
filename = quote_path_relative(filename, -1, &buf, opt->prefix);
180169
i = grep_buffer(opt, filename, data, sz);
170+
strbuf_release(&buf);
181171
free(data);
182172
return i;
183173
}
@@ -582,6 +572,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
582572
int i;
583573

584574
memset(&opt, 0, sizeof(opt));
575+
opt.prefix = prefix;
585576
opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
586577
opt.relative = 1;
587578
opt.pathname = 1;
@@ -857,15 +848,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
857848
verify_filename(prefix, argv[j]);
858849
}
859850

860-
if (i < argc) {
851+
if (i < argc)
861852
paths = get_pathspec(prefix, argv + i);
862-
if (opt.prefix_length && opt.relative) {
863-
/* Make sure we do not get outside of paths */
864-
for (i = 0; paths[i]; i++)
865-
if (strncmp(prefix, paths[i], opt.prefix_length))
866-
die("git grep: cannot generate relative filenames containing '..'");
867-
}
868-
}
869853
else if (prefix) {
870854
paths = xcalloc(2, sizeof(const char *));
871855
paths[0] = prefix;

grep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct grep_opt {
5959
struct grep_pat *pattern_list;
6060
struct grep_pat **pattern_tail;
6161
struct grep_expr *pattern_expression;
62+
const char *prefix;
6263
int prefix_length;
6364
regex_t regexp;
6465
unsigned linenum:1;

t/t7002-grep.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,21 @@ test_expect_success 'grep with CE_VALID file' '
212212
git checkout t/t
213213
'
214214

215+
test_expect_success 'grep from a subdirectory to search wider area (1)' '
216+
mkdir -p s &&
217+
(
218+
cd s && git grep "x x x" ..
219+
)
220+
'
221+
222+
test_expect_success 'grep from a subdirectory to search wider area (2)' '
223+
mkdir -p s &&
224+
(
225+
cd s || exit 1
226+
( git grep xxyyzz .. >out ; echo $? >status )
227+
! test -s out &&
228+
test 1 = $(cat status)
229+
)
230+
'
231+
215232
test_done

0 commit comments

Comments
 (0)