Rework unquote_c_style to work on a strbuf.
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>
Pierre Habouzit committed
Sep 20, 2007 at 00:42 UTC
7fb1011e610a28518959b1d2d48cea17ecc32048
7 files changed
+191
-188
builtin-apply.c
+62
-61
@@ -231,35 +231,33 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
231
{
232
int len;
233
const char *start = line;
234
- char *name;
234
235
if (*line == '"') {
236
+ struct strbuf name;
237
+
238
/* Proposed "new-style" GNU patch/diff format; see
239
* http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
240
*/
240
- name = unquote_c_style(line, NULL);
241
- if (name) {
242
- char *cp = name;
243
- while (p_value) {
241
+ strbuf_init(&name, 0);
242
+ if (!unquote_c_style(&name, line, NULL)) {
243
+ char *cp;
244
+
245
+ for (cp = name.buf; p_value; p_value--) {
246
cp = strchr(cp, '/');
247
if (!cp)
248
break;
249
cp++;
248
- p_value--;
250
}
251
if (cp) {
252
/* name can later be freed, so we need
253
* to memmove, not just return cp
254
*/
254
- memmove(name, cp, strlen(cp) + 1);
255
+ strbuf_remove(&name, 0, cp - name.buf);
256
free(def);
256
- return name;
257
- }
258
- else {
259
- free(name);
260
- name = NULL;
257
+ return name.buf;
258
}
259
}
260
+ strbuf_release(&name);
261
}
262
263
for (;;) {
@@ -567,29 +565,30 @@ static const char *stop_at_slash(const char *line, int llen)
565
*/
566
static char *git_header_name(char *line, int llen)
567
{
570
- int len;
568
const char *name;
569
const char *second = NULL;
570
+ size_t len;
571
572
line += strlen("diff --git ");
573
llen -= strlen("diff --git ");
574
575
if (*line == '"') {
576
const char *cp;
579
- char *first = unquote_c_style(line, &second);
580
- if (!first)
581
- return NULL;
577
+ struct strbuf first;
578
+ struct strbuf sp;
579
+
580
+ strbuf_init(&first, 0);
581
+ strbuf_init(&sp, 0);
582
+
583
+ if (unquote_c_style(&first, line, &second))
584
+ goto free_and_fail1;
585
586
/* advance to the first slash */
584
- cp = stop_at_slash(first, strlen(first));
585
- if (!cp || cp == first) {
586
- /* we do not accept absolute paths */
587
- free_first_and_fail:
588
- free(first);
589
- return NULL;
590
- }
591
- len = strlen(cp+1);
592
- memmove(first, cp+1, len+1); /* including NUL */
587
+ cp = stop_at_slash(first.buf, first.len);
588
+ /* we do not accept absolute paths */
589
+ if (!cp || cp == first.buf)
590
+ goto free_and_fail1;
591
+ strbuf_remove(&first, 0, cp + 1 - first.buf);
592
593
/* second points at one past closing dq of name.
594
* find the second name.
@@ -598,40 +597,40 @@ static char *git_header_name(char *line, int llen)
597
second++;
598
599
if (line + llen <= second)
601
- goto free_first_and_fail;
600
+ goto free_and_fail1;
601
if (*second == '"') {
603
- char *sp = unquote_c_style(second, NULL);
604
- if (!sp)
605
- goto free_first_and_fail;
606
- cp = stop_at_slash(sp, strlen(sp));
607
- if (!cp || cp == sp) {
608
- free_both_and_fail:
609
- free(sp);
610
- goto free_first_and_fail;
611
- }
602
+ if (unquote_c_style(&sp, second, NULL))
603
+ goto free_and_fail1;
604
+ cp = stop_at_slash(sp.buf, sp.len);
605
+ if (!cp || cp == sp.buf)
606
+ goto free_and_fail1;
607
/* They must match, otherwise ignore */
613
- if (strcmp(cp+1, first))
614
- goto free_both_and_fail;
615
- free(sp);
616
- return first;
608
+ if (strcmp(cp + 1, first.buf))
609
+ goto free_and_fail1;
610
+ strbuf_release(&sp);
611
+ return first.buf;
612
}
613
614
/* unquoted second */
615
cp = stop_at_slash(second, line + llen - second);
616
if (!cp || cp == second)
622
- goto free_first_and_fail;
617
+ goto free_and_fail1;
618
cp++;
624
- if (line + llen - cp != len + 1 ||
625
- memcmp(first, cp, len))
626
- goto free_first_and_fail;
627
- return first;
619
+ if (line + llen - cp != first.len + 1 ||
620
+ memcmp(first.buf, cp, first.len))
621
+ goto free_and_fail1;
622
+ return first.buf;
623
+
624
+ free_and_fail1:
625
+ strbuf_release(&first);
626
+ strbuf_release(&sp);
627
+ return NULL;
628
}
629
630
/* unquoted first name */
631
name = stop_at_slash(line, llen);
632
if (!name || name == line)
633
return NULL;
634
-
634
name++;
635
636
/* since the first name is unquoted, a dq if exists must be
@@ -639,28 +638,30 @@ static char *git_header_name(char *line, int llen)
638
*/
639
for (second = name; second < line + llen; second++) {
640
if (*second == '"') {
642
- const char *cp = second;
641
+ struct strbuf sp;
642
const char *np;
644
- char *sp = unquote_c_style(second, NULL);
645
-
646
- if (!sp)
647
- return NULL;
648
- np = stop_at_slash(sp, strlen(sp));
649
- if (!np || np == sp) {
650
- free_second_and_fail:
651
- free(sp);
652
- return NULL;
653
- }
643
+
644
+ strbuf_init(&sp, 0);
645
+ if (unquote_c_style(&sp, second, NULL))
646
+ goto free_and_fail2;
647
+
648
+ np = stop_at_slash(sp.buf, sp.len);
649
+ if (!np || np == sp.buf)
650
+ goto free_and_fail2;
651
np++;
655
- len = strlen(np);
656
- if (len < cp - name &&
652
+
653
+ len = sp.buf + sp.len - np;
654
+ if (len < second - name &&
655
!strncmp(np, name, len) &&
656
isspace(name[len])) {
657
/* Good */
660
- memmove(sp, np, len + 1);
661
- return sp;
658
+ strbuf_remove(&sp, 0, np - sp.buf);
659
+ return sp.buf;
660
}
663
- goto free_second_and_fail;
661
+
662
+ free_and_fail2:
663
+ strbuf_release(&sp);
664
+ return NULL;
665
}
666
}
667
builtin-checkout-index.c
+14
-13
@@ -270,26 +270,27 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
270
}
271
272
if (read_from_stdin) {
273
- struct strbuf buf;
273
+ struct strbuf buf, nbuf;
274
+
275
if (all)
276
die("git-checkout-index: don't mix '--all' and '--stdin'");
277
+
278
strbuf_init(&buf, 0);
277
- while (1) {
278
- char *path_name;
279
+ strbuf_init(&nbuf, 0);
280
+ while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
281
const char *p;
280
- if (strbuf_getline(&buf, stdin, line_termination) == EOF)
281
- break;
282
- if (line_termination && buf.buf[0] == '"')
283
- path_name = unquote_c_style(buf.buf, NULL);
284
- else
285
- path_name = buf.buf;
286
- p = prefix_path(prefix, prefix_length, path_name);
282
+ if (line_termination && buf.buf[0] == '"') {
283
+ strbuf_reset(&nbuf);
284
+ if (unquote_c_style(&nbuf, buf.buf, NULL))
285
+ die("line is badly quoted");
286
+ strbuf_swap(&buf, &nbuf);
287
+ }
288
+ p = prefix_path(prefix, prefix_length, buf.buf);
289
checkout_file(p, prefix_length);
288
- if (p < path_name || p > path_name + strlen(path_name))
290
+ if (p < buf.buf || p > buf.buf + buf.len)
291
free((char *)p);
290
- if (path_name != buf.buf)
291
- free(path_name);
292
}
293
+ strbuf_release(&nbuf);
294
strbuf_release(&buf);
295
}
296
builtin-update-index.c
+27
-24
@@ -295,8 +295,11 @@ static void update_one(const char *path, const char *prefix, int prefix_length)
295
static void read_index_info(int line_termination)
296
{
297
struct strbuf buf;
298
+ struct strbuf uq;
299
+
300
strbuf_init(&buf, 0);
299
- while (1) {
301
+ strbuf_init(&uq, 0);
302
+ while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
303
char *ptr, *tab;
304
char *path_name;
305
unsigned char sha1[20];
@@ -320,9 +323,6 @@ static void read_index_info(int line_termination)
323
* This format is to put higher order stages into the
324
* index file and matches git-ls-files --stage output.
325
*/
323
- if (strbuf_getline(&buf, stdin, line_termination) == EOF)
324
- break;
325
-
326
errno = 0;
327
ul = strtoul(buf.buf, &ptr, 8);
328
if (ptr == buf.buf || *ptr != ' '
@@ -347,15 +347,17 @@ static void read_index_info(int line_termination)
347
if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
348
goto bad_line;
349
350
- if (line_termination && ptr[0] == '"')
351
- path_name = unquote_c_style(ptr, NULL);
352
- else
353
- path_name = ptr;
350
+ path_name = ptr;
351
+ if (line_termination && path_name[0] == '"') {
352
+ strbuf_reset(&uq);
353
+ if (unquote_c_style(&uq, path_name, NULL)) {
354
+ die("git-update-index: bad quoting of path name");
355
+ }
356
+ path_name = uq.buf;
357
+ }
358
359
if (!verify_path(path_name)) {
360
fprintf(stderr, "Ignoring path %s\n", path_name);
357
- if (path_name != ptr)
358
- free(path_name);
361
continue;
362
}
363
@@ -383,6 +385,7 @@ static void read_index_info(int line_termination)
385
die("malformed index info %s", buf.buf);
386
}
387
strbuf_release(&buf);
388
+ strbuf_release(&uq);
389
}
390
391
static const char update_index_usage[] =
@@ -705,26 +708,26 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
708
free((char*)p);
709
}
710
if (read_from_stdin) {
708
- struct strbuf buf;
711
+ struct strbuf buf, nbuf;
712
+
713
strbuf_init(&buf, 0);
710
- while (1) {
711
- char *path_name;
714
+ strbuf_init(&nbuf, 0);
715
+ while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
716
const char *p;
713
- if (strbuf_getline(&buf, stdin, line_termination) == EOF)
714
- break;
715
- if (line_termination && buf.buf[0] == '"')
716
- path_name = unquote_c_style(buf.buf, NULL);
717
- else
718
- path_name = buf.buf;
719
- p = prefix_path(prefix, prefix_length, path_name);
717
+ if (line_termination && buf.buf[0] == '"') {
718
+ strbuf_reset(&nbuf);
719
+ if (unquote_c_style(&nbuf, buf.buf, NULL))
720
+ die("line is badly quoted");
721
+ strbuf_swap(&buf, &nbuf);
722
+ }
723
+ p = prefix_path(prefix, prefix_length, buf.buf);
724
update_one(p, NULL, 0);
725
if (set_executable_bit)
726
chmod_path(set_executable_bit, p);
723
- if (p < path_name || p > path_name + strlen(path_name))
724
- free((char*) p);
725
- if (path_name != buf.buf)
726
- free(path_name);
727
+ if (p < buf.buf || p > buf.buf + buf.len)
728
+ free((char *)p);
729
}
730
+ strbuf_release(&nbuf);
731
strbuf_release(&buf);
732
}
733
fast-import.c
+22
-25
@@ -1759,7 +1759,7 @@ static void load_branch(struct branch *b)
1759
static void file_change_m(struct branch *b)
1760
{
1761
const char *p = command_buf.buf + 2;
1762
- char *p_uq;
1762
+ static struct strbuf uq = STRBUF_INIT;
1763
const char *endp;
1764
struct object_entry *oe = oe;
1765
unsigned char sha1[20];
@@ -1797,18 +1797,20 @@ static void file_change_m(struct branch *b)
1797
if (*p++ != ' ')
1798
die("Missing space after SHA1: %s", command_buf.buf);
1799
1800
- p_uq = unquote_c_style(p, &endp);
1801
- if (p_uq) {
1800
+ strbuf_reset(&uq);
1801
+ if (!unquote_c_style(&uq, p, &endp)) {
1802
if (*endp)
1803
die("Garbage after path in: %s", command_buf.buf);
1804
- p = p_uq;
1804
+ p = uq.buf;
1805
}
1806
1807
if (inline_data) {
1808
static struct strbuf buf = STRBUF_INIT;
1809
1810
- if (!p_uq)
1811
- p = p_uq = xstrdup(p);
1810
+ if (p != uq.buf) {
1811
+ strbuf_addstr(&uq, p);
1812
+ p = uq.buf;
1813
+ }
1814
read_next_command();
1815
cmd_data(&buf);
1816
store_object(OBJ_BLOB, &buf, &last_blob, sha1, 0);
@@ -1826,56 +1828,54 @@ static void file_change_m(struct branch *b)
1828
}
1829
1830
tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
1829
- free(p_uq);
1831
}
1832
1833
static void file_change_d(struct branch *b)
1834
{
1835
const char *p = command_buf.buf + 2;
1835
- char *p_uq;
1836
+ static struct strbuf uq = STRBUF_INIT;
1837
const char *endp;
1838
1838
- p_uq = unquote_c_style(p, &endp);
1839
- if (p_uq) {
1839
+ strbuf_reset(&uq);
1840
+ if (!unquote_c_style(&uq, p, &endp)) {
1841
if (*endp)
1842
die("Garbage after path in: %s", command_buf.buf);
1842
- p = p_uq;
1843
+ p = uq.buf;
1844
}
1845
tree_content_remove(&b->branch_tree, p, NULL);
1845
- free(p_uq);
1846
}
1847
1848
static void file_change_cr(struct branch *b, int rename)
1849
{
1850
const char *s, *d;
1851
- char *s_uq, *d_uq;
1851
+ static struct strbuf s_uq = STRBUF_INIT;
1852
+ static struct strbuf d_uq = STRBUF_INIT;
1853
const char *endp;
1854
struct tree_entry leaf;
1855
1856
s = command_buf.buf + 2;
1856
- s_uq = unquote_c_style(s, &endp);
1857
- if (s_uq) {
1857
+ strbuf_reset(&s_uq);
1858
+ if (!unquote_c_style(&s_uq, s, &endp)) {
1859
if (*endp != ' ')
1860
die("Missing space after source: %s", command_buf.buf);
1860
- }
1861
- else {
1861
+ } else {
1862
endp = strchr(s, ' ');
1863
if (!endp)
1864
die("Missing space after source: %s", command_buf.buf);
1865
- s_uq = xmemdupz(s, endp - s);
1865
+ strbuf_add(&s_uq, s, endp - s);
1866
}
1867
- s = s_uq;
1867
+ s = s_uq.buf;
1868
1869
endp++;
1870
if (!*endp)
1871
die("Missing dest: %s", command_buf.buf);
1872
1873
d = endp;
1874
- d_uq = unquote_c_style(d, &endp);
1875
- if (d_uq) {
1874
+ strbuf_reset(&d_uq);
1875
+ if (!unquote_c_style(&d_uq, d, &endp)) {
1876
if (*endp)
1877
die("Garbage after dest in: %s", command_buf.buf);
1878
- d = d_uq;
1878
+ d = d_uq.buf;
1879
}
1880
1881
memset(&leaf, 0, sizeof(leaf));
@@ -1889,9 +1889,6 @@ static void file_change_cr(struct branch *b, int rename)
1889
leaf.versions[1].sha1,
1890
leaf.versions[1].mode,
1891
leaf.tree);
1892
-
1893
- free(s_uq);
1894
- free(d_uq);
1892
}
1893
1894
static void file_change_deleteall(struct branch *b)
mktree.c
+14
-11
@@ -66,6 +66,7 @@ static const char mktree_usage[] = "git-mktree [-z]";
66
int main(int ac, char **av)
67
{
68
struct strbuf sb;
69
+ struct strbuf p_uq;
70
unsigned char sha1[20];
71
int line_termination = '\n';
72
@@ -82,14 +83,13 @@ int main(int ac, char **av)
83
}
84
85
strbuf_init(&sb, 0);
85
- while (1) {
86
+ strbuf_init(&p_uq, 0);
87
+ while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
88
char *ptr, *ntr;
89
unsigned mode;
90
enum object_type type;
91
char *path;
92
91
- if (strbuf_getline(&sb, stdin, line_termination) == EOF)
92
- break;
93
ptr = sb.buf;
94
/* Input is non-recursive ls-tree output format
95
* mode SP type SP sha1 TAB name
@@ -109,18 +109,21 @@ int main(int ac, char **av)
109
*ntr++ = 0; /* now at the beginning of SHA1 */
110
if (type != type_from_string(ptr))
111
die("object type %s mismatch (%s)", ptr, typename(type));
112
- ntr += 41; /* at the beginning of name */
113
- if (line_termination && ntr[0] == '"')
114
- path = unquote_c_style(ntr, NULL);
115
- else
116
- path = ntr;
112
118
- append_to_tree(mode, sha1, path);
113
+ path = ntr + 41; /* at the beginning of name */
114
+ if (line_termination && path[0] == '"') {
115
+ strbuf_reset(&p_uq);
116
+ if (unquote_c_style(&p_uq, path, NULL)) {
117
+ die("invalid quoting");
118
+ }
119
+ path = p_uq.buf;
120
+ }
121
120
- if (path != ntr)
121
- free(path);
122
+ append_to_tree(mode, sha1, path);
123
}
124
+ strbuf_release(&p_uq);
125
strbuf_release(&sb);
126
+
127
write_tree(sha1);
128
puts(sha1_to_hex(sha1));
129
exit(0);
quote.c
+51
-53
@@ -239,73 +239,71 @@ int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
239
/*
240
* C-style name unquoting.
241
*
242
- * Quoted should point at the opening double quote. Returns
243
- * an allocated memory that holds unquoted name, which the caller
244
- * should free when done. Updates endp pointer to point at
245
- * one past the ending double quote if given.
242
+ * Quoted should point at the opening double quote.
243
+ * + Returns 0 if it was able to unquote the string properly, and appends the
244
+ * result in the strbuf `sb'.
245
+ * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
246
+ * that this function will allocate memory in the strbuf, so calling
247
+ * strbuf_release is mandatory whichever result unquote_c_style returns.
248
+ *
249
+ * Updates endp pointer to point at one past the ending double quote if given.
250
*/
247
-
248
-char *unquote_c_style(const char *quoted, const char **endp)
251
+int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
252
{
250
- const char *sp;
251
- char *name = NULL, *outp = NULL;
252
- int count = 0, ch, ac;
253
-
254
-#undef EMIT
255
-#define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
253
+ size_t oldlen = sb->len, len;
254
+ int ch, ac;
255
256
if (*quoted++ != '"')
258
- return NULL;
257
+ return -1;
258
+
259
+ for (;;) {
260
+ len = strcspn(quoted, "\"\\");
261
+ strbuf_add(sb, quoted, len);
262
+ quoted += len;
263
260
- while (1) {
261
- /* first pass counts and allocates, second pass fills */
262
- for (sp = quoted; (ch = *sp++) != '"'; ) {
263
- if (ch == '\\') {
264
- switch (ch = *sp++) {
265
- case 'a': ch = '\a'; break;
266
- case 'b': ch = '\b'; break;
267
- case 'f': ch = '\f'; break;
268
- case 'n': ch = '\n'; break;
269
- case 'r': ch = '\r'; break;
270
- case 't': ch = '\t'; break;
271
- case 'v': ch = '\v'; break;
272
-
273
- case '\\': case '"':
274
- break; /* verbatim */
275
-
276
- case '0':
277
- case '1':
278
- case '2':
279
- case '3':
280
- case '4':
281
- case '5':
282
- case '6':
283
- case '7':
284
- /* octal */
264
+ switch (*quoted++) {
265
+ case '"':
266
+ if (endp)
267
+ *endp = quoted + 1;
268
+ return 0;
269
+ case '\\':
270
+ break;
271
+ default:
272
+ goto error;
273
+ }
274
+
275
+ switch ((ch = *quoted++)) {
276
+ case 'a': ch = '\a'; break;
277
+ case 'b': ch = '\b'; break;
278
+ case 'f': ch = '\f'; break;
279
+ case 'n': ch = '\n'; break;
280
+ case 'r': ch = '\r'; break;
281
+ case 't': ch = '\t'; break;
282
+ case 'v': ch = '\v'; break;
283
+
284
+ case '\\': case '"':
285
+ break; /* verbatim */
286
+
287
+ /* octal values with first digit over 4 overflow */
288
+ case '0': case '1': case '2': case '3':
289
ac = ((ch - '0') << 6);
286
- if ((ch = *sp++) < '0' || '7' < ch)
287
- return NULL;
290
+ if ((ch = *quoted++) < '0' || '7' < ch)
291
+ goto error;
292
ac |= ((ch - '0') << 3);
289
- if ((ch = *sp++) < '0' || '7' < ch)
290
- return NULL;
293
+ if ((ch = *quoted++) < '0' || '7' < ch)
294
+ goto error;
295
ac |= (ch - '0');
296
ch = ac;
297
break;
298
default:
295
- return NULL; /* malformed */
296
- }
299
+ goto error;
300
}
298
- EMIT(ch);
301
+ strbuf_addch(sb, ch);
302
}
303
301
- if (name) {
302
- *outp = 0;
303
- if (endp)
304
- *endp = sp;
305
- return name;
306
- }
307
- outp = name = xmalloc(count + 1);
308
- }
304
+ error:
305
+ strbuf_setlen(sb, oldlen);
306
+ return -1;
307
}
308
309
void write_name_quoted(const char *prefix, int prefix_len,
quote.h
+1
-1
@@ -43,9 +43,9 @@ extern int add_to_string(char **ptrp, int *sizep, const char *str, int quote);
43
*/
44
extern char *sq_dequote(char *);
45
46
+extern int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
47
extern int quote_c_style(const char *name, char *outbuf, FILE *outfp,
48
int nodq);
48
-extern char *unquote_c_style(const char *quoted, const char **endp);
49
50
extern void write_name_quoted(const char *prefix, int prefix_len,
51
const char *name, int quote, FILE *out);