Skip to content

Commit

Permalink
Rework unquote_c_style to work on a strbuf.
Browse files Browse the repository at this point in the history
If the gain is not obvious in the diffstat, the resulting code is more
readable, _and_ in checkout-index/update-index we now reuse the same buffer
to unquote strings instead of always freeing/mallocing.

This also is more coherent with the next patch that reworks quoting
functions.

The quoting function is also made more efficient scanning for backslashes
and treating portions of strings without a backslash at once.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
  • Loading branch information
Pierre Habouzit authored and Junio C Hamano committed Sep 21, 2007
1 parent c76689d commit 7fb1011
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 188 deletions.
123 changes: 62 additions & 61 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,35 +231,33 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
{
int len;
const char *start = line;
char *name;

if (*line == '"') {
struct strbuf name;

/* Proposed "new-style" GNU patch/diff format; see
* http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
*/
name = unquote_c_style(line, NULL);
if (name) {
char *cp = name;
while (p_value) {
strbuf_init(&name, 0);
if (!unquote_c_style(&name, line, NULL)) {
char *cp;

for (cp = name.buf; p_value; p_value--) {
cp = strchr(cp, '/');
if (!cp)
break;
cp++;
p_value--;
}
if (cp) {
/* name can later be freed, so we need
* to memmove, not just return cp
*/
memmove(name, cp, strlen(cp) + 1);
strbuf_remove(&name, 0, cp - name.buf);
free(def);
return name;
}
else {
free(name);
name = NULL;
return name.buf;
}
}
strbuf_release(&name);
}

for (;;) {
Expand Down Expand Up @@ -567,29 +565,30 @@ static const char *stop_at_slash(const char *line, int llen)
*/
static char *git_header_name(char *line, int llen)
{
int len;
const char *name;
const char *second = NULL;
size_t len;

line += strlen("diff --git ");
llen -= strlen("diff --git ");

if (*line == '"') {
const char *cp;
char *first = unquote_c_style(line, &second);
if (!first)
return NULL;
struct strbuf first;
struct strbuf sp;

strbuf_init(&first, 0);
strbuf_init(&sp, 0);

if (unquote_c_style(&first, line, &second))
goto free_and_fail1;

/* advance to the first slash */
cp = stop_at_slash(first, strlen(first));
if (!cp || cp == first) {
/* we do not accept absolute paths */
free_first_and_fail:
free(first);
return NULL;
}
len = strlen(cp+1);
memmove(first, cp+1, len+1); /* including NUL */
cp = stop_at_slash(first.buf, first.len);
/* we do not accept absolute paths */
if (!cp || cp == first.buf)
goto free_and_fail1;
strbuf_remove(&first, 0, cp + 1 - first.buf);

/* second points at one past closing dq of name.
* find the second name.
Expand All @@ -598,69 +597,71 @@ static char *git_header_name(char *line, int llen)
second++;

if (line + llen <= second)
goto free_first_and_fail;
goto free_and_fail1;
if (*second == '"') {
char *sp = unquote_c_style(second, NULL);
if (!sp)
goto free_first_and_fail;
cp = stop_at_slash(sp, strlen(sp));
if (!cp || cp == sp) {
free_both_and_fail:
free(sp);
goto free_first_and_fail;
}
if (unquote_c_style(&sp, second, NULL))
goto free_and_fail1;
cp = stop_at_slash(sp.buf, sp.len);
if (!cp || cp == sp.buf)
goto free_and_fail1;
/* They must match, otherwise ignore */
if (strcmp(cp+1, first))
goto free_both_and_fail;
free(sp);
return first;
if (strcmp(cp + 1, first.buf))
goto free_and_fail1;
strbuf_release(&sp);
return first.buf;
}

/* unquoted second */
cp = stop_at_slash(second, line + llen - second);
if (!cp || cp == second)
goto free_first_and_fail;
goto free_and_fail1;
cp++;
if (line + llen - cp != len + 1 ||
memcmp(first, cp, len))
goto free_first_and_fail;
return first;
if (line + llen - cp != first.len + 1 ||
memcmp(first.buf, cp, first.len))
goto free_and_fail1;
return first.buf;

free_and_fail1:
strbuf_release(&first);
strbuf_release(&sp);
return NULL;
}

/* unquoted first name */
name = stop_at_slash(line, llen);
if (!name || name == line)
return NULL;

name++;

/* since the first name is unquoted, a dq if exists must be
* the beginning of the second name.
*/
for (second = name; second < line + llen; second++) {
if (*second == '"') {
const char *cp = second;
struct strbuf sp;
const char *np;
char *sp = unquote_c_style(second, NULL);

if (!sp)
return NULL;
np = stop_at_slash(sp, strlen(sp));
if (!np || np == sp) {
free_second_and_fail:
free(sp);
return NULL;
}

strbuf_init(&sp, 0);
if (unquote_c_style(&sp, second, NULL))
goto free_and_fail2;

np = stop_at_slash(sp.buf, sp.len);
if (!np || np == sp.buf)
goto free_and_fail2;
np++;
len = strlen(np);
if (len < cp - name &&

len = sp.buf + sp.len - np;
if (len < second - name &&
!strncmp(np, name, len) &&
isspace(name[len])) {
/* Good */
memmove(sp, np, len + 1);
return sp;
strbuf_remove(&sp, 0, np - sp.buf);
return sp.buf;
}
goto free_second_and_fail;

free_and_fail2:
strbuf_release(&sp);
return NULL;
}
}

Expand Down
27 changes: 14 additions & 13 deletions builtin-checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,26 +270,27 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
}

if (read_from_stdin) {
struct strbuf buf;
struct strbuf buf, nbuf;

if (all)
die("git-checkout-index: don't mix '--all' and '--stdin'");

strbuf_init(&buf, 0);
while (1) {
char *path_name;
strbuf_init(&nbuf, 0);
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
const char *p;
if (strbuf_getline(&buf, stdin, line_termination) == EOF)
break;
if (line_termination && buf.buf[0] == '"')
path_name = unquote_c_style(buf.buf, NULL);
else
path_name = buf.buf;
p = prefix_path(prefix, prefix_length, path_name);
if (line_termination && buf.buf[0] == '"') {
strbuf_reset(&nbuf);
if (unquote_c_style(&nbuf, buf.buf, NULL))
die("line is badly quoted");
strbuf_swap(&buf, &nbuf);
}
p = prefix_path(prefix, prefix_length, buf.buf);
checkout_file(p, prefix_length);
if (p < path_name || p > path_name + strlen(path_name))
if (p < buf.buf || p > buf.buf + buf.len)
free((char *)p);
if (path_name != buf.buf)
free(path_name);
}
strbuf_release(&nbuf);
strbuf_release(&buf);
}

Expand Down
51 changes: 27 additions & 24 deletions builtin-update-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,11 @@ static void update_one(const char *path, const char *prefix, int prefix_length)
static void read_index_info(int line_termination)
{
struct strbuf buf;
struct strbuf uq;

strbuf_init(&buf, 0);
while (1) {
strbuf_init(&uq, 0);
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
char *ptr, *tab;
char *path_name;
unsigned char sha1[20];
Expand All @@ -320,9 +323,6 @@ static void read_index_info(int line_termination)
* This format is to put higher order stages into the
* index file and matches git-ls-files --stage output.
*/
if (strbuf_getline(&buf, stdin, line_termination) == EOF)
break;

errno = 0;
ul = strtoul(buf.buf, &ptr, 8);
if (ptr == buf.buf || *ptr != ' '
Expand All @@ -347,15 +347,17 @@ static void read_index_info(int line_termination)
if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
goto bad_line;

if (line_termination && ptr[0] == '"')
path_name = unquote_c_style(ptr, NULL);
else
path_name = ptr;
path_name = ptr;
if (line_termination && path_name[0] == '"') {
strbuf_reset(&uq);
if (unquote_c_style(&uq, path_name, NULL)) {
die("git-update-index: bad quoting of path name");
}
path_name = uq.buf;
}

if (!verify_path(path_name)) {
fprintf(stderr, "Ignoring path %s\n", path_name);
if (path_name != ptr)
free(path_name);
continue;
}

Expand Down Expand Up @@ -383,6 +385,7 @@ static void read_index_info(int line_termination)
die("malformed index info %s", buf.buf);
}
strbuf_release(&buf);
strbuf_release(&uq);
}

static const char update_index_usage[] =
Expand Down Expand Up @@ -705,26 +708,26 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
free((char*)p);
}
if (read_from_stdin) {
struct strbuf buf;
struct strbuf buf, nbuf;

strbuf_init(&buf, 0);
while (1) {
char *path_name;
strbuf_init(&nbuf, 0);
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
const char *p;
if (strbuf_getline(&buf, stdin, line_termination) == EOF)
break;
if (line_termination && buf.buf[0] == '"')
path_name = unquote_c_style(buf.buf, NULL);
else
path_name = buf.buf;
p = prefix_path(prefix, prefix_length, path_name);
if (line_termination && buf.buf[0] == '"') {
strbuf_reset(&nbuf);
if (unquote_c_style(&nbuf, buf.buf, NULL))
die("line is badly quoted");
strbuf_swap(&buf, &nbuf);
}
p = prefix_path(prefix, prefix_length, buf.buf);
update_one(p, NULL, 0);
if (set_executable_bit)
chmod_path(set_executable_bit, p);
if (p < path_name || p > path_name + strlen(path_name))
free((char*) p);
if (path_name != buf.buf)
free(path_name);
if (p < buf.buf || p > buf.buf + buf.len)
free((char *)p);
}
strbuf_release(&nbuf);
strbuf_release(&buf);
}

Expand Down
Loading

0 comments on commit 7fb1011

Please sign in to comment.