Skip to content

Commit 3305300

Browse files
committed
Merge branch 'ps/format-padding-fix' into maint-2.30
2 parents abd4d67 + 304a50a commit 3305300

File tree

7 files changed

+185
-47
lines changed

7 files changed

+185
-47
lines changed

column.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct column_data {
2323
/* return length of 's' in letters, ANSI escapes stripped */
2424
static int item_length(const char *s)
2525
{
26-
return utf8_strnwidth(s, -1, 1);
26+
return utf8_strnwidth(s, strlen(s), 1);
2727
}
2828

2929
/*

git-compat-util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,14 @@ static inline size_t st_sub(size_t a, size_t b)
918918
return a - b;
919919
}
920920

921+
static inline int cast_size_t_to_int(size_t a)
922+
{
923+
if (a > INT_MAX)
924+
die("number too large to represent as int on this platform: %"PRIuMAX,
925+
(uintmax_t)a);
926+
return (int)a;
927+
}
928+
921929
#ifdef HAVE_ALLOCA_H
922930
# include <alloca.h>
923931
# define xalloca(size) (alloca(size))

pretty.c

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
#include "gpg-interface.h"
1414
#include "trailer.h"
1515

16+
/*
17+
* The limit for formatting directives, which enable the caller to append
18+
* arbitrarily many bytes to the formatted buffer. This includes padding
19+
* and wrapping formatters.
20+
*/
21+
#define FORMATTING_LIMIT (16 * 1024)
22+
1623
static char *user_format;
1724
static struct cmt_fmt_map {
1825
const char *name;
@@ -915,7 +922,9 @@ static void strbuf_wrap(struct strbuf *sb, size_t pos,
915922
if (pos)
916923
strbuf_add(&tmp, sb->buf, pos);
917924
strbuf_add_wrapped_text(&tmp, sb->buf + pos,
918-
(int) indent1, (int) indent2, (int) width);
925+
cast_size_t_to_int(indent1),
926+
cast_size_t_to_int(indent2),
927+
cast_size_t_to_int(width));
919928
strbuf_swap(&tmp, sb);
920929
strbuf_release(&tmp);
921930
}
@@ -1041,9 +1050,18 @@ static size_t parse_padding_placeholder(const char *placeholder,
10411050
const char *end = start + strcspn(start, ",)");
10421051
char *next;
10431052
int width;
1044-
if (!end || end == start)
1053+
if (!*end || end == start)
10451054
return 0;
10461055
width = strtol(start, &next, 10);
1056+
1057+
/*
1058+
* We need to limit the amount of padding, or otherwise this
1059+
* would allow the user to pad the buffer by arbitrarily many
1060+
* bytes and thus cause resource exhaustion.
1061+
*/
1062+
if (width < -FORMATTING_LIMIT || width > FORMATTING_LIMIT)
1063+
return 0;
1064+
10471065
if (next == start || width == 0)
10481066
return 0;
10491067
if (width < 0) {
@@ -1203,6 +1221,16 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
12031221
if (*next != ')')
12041222
return 0;
12051223
}
1224+
1225+
/*
1226+
* We need to limit the format here as it allows the
1227+
* user to prepend arbitrarily many bytes to the buffer
1228+
* when rewrapping.
1229+
*/
1230+
if (width > FORMATTING_LIMIT ||
1231+
indent1 > FORMATTING_LIMIT ||
1232+
indent2 > FORMATTING_LIMIT)
1233+
return 0;
12061234
rewrap_message_tail(sb, c, width, indent1, indent2);
12071235
return end - placeholder + 1;
12081236
} else
@@ -1473,19 +1501,21 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
14731501
struct format_commit_context *c)
14741502
{
14751503
struct strbuf local_sb = STRBUF_INIT;
1476-
int total_consumed = 0, len, padding = c->padding;
1504+
size_t total_consumed = 0;
1505+
int len, padding = c->padding;
1506+
14771507
if (padding < 0) {
14781508
const char *start = strrchr(sb->buf, '\n');
14791509
int occupied;
14801510
if (!start)
14811511
start = sb->buf;
1482-
occupied = utf8_strnwidth(start, -1, 1);
1512+
occupied = utf8_strnwidth(start, strlen(start), 1);
14831513
occupied += c->pretty_ctx->graph_width;
14841514
padding = (-padding) - occupied;
14851515
}
14861516
while (1) {
14871517
int modifier = *placeholder == 'C';
1488-
int consumed = format_commit_one(&local_sb, placeholder, c);
1518+
size_t consumed = format_commit_one(&local_sb, placeholder, c);
14891519
total_consumed += consumed;
14901520

14911521
if (!modifier)
@@ -1497,7 +1527,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
14971527
placeholder++;
14981528
total_consumed++;
14991529
}
1500-
len = utf8_strnwidth(local_sb.buf, -1, 1);
1530+
len = utf8_strnwidth(local_sb.buf, local_sb.len, 1);
15011531

15021532
if (c->flush_type == flush_left_and_steal) {
15031533
const char *ch = sb->buf + sb->len - 1;
@@ -1512,7 +1542,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
15121542
if (*ch != 'm')
15131543
break;
15141544
p = ch - 1;
1515-
while (ch - p < 10 && *p != '\033')
1545+
while (p > sb->buf && ch - p < 10 && *p != '\033')
15161546
p--;
15171547
if (*p != '\033' ||
15181548
ch + 1 - p != display_mode_esc_sequence_len(p))
@@ -1551,7 +1581,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
15511581
}
15521582
strbuf_addbuf(sb, &local_sb);
15531583
} else {
1554-
int sb_len = sb->len, offset = 0;
1584+
size_t sb_len = sb->len, offset = 0;
15551585
if (c->flush_type == flush_left)
15561586
offset = padding - len;
15571587
else if (c->flush_type == flush_both)
@@ -1574,8 +1604,7 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
15741604
const char *placeholder,
15751605
void *context)
15761606
{
1577-
int consumed;
1578-
size_t orig_len;
1607+
size_t consumed, orig_len;
15791608
enum {
15801609
NO_MAGIC,
15811610
ADD_LF_BEFORE_NON_EMPTY,
@@ -1596,9 +1625,21 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
15961625
default:
15971626
break;
15981627
}
1599-
if (magic != NO_MAGIC)
1628+
if (magic != NO_MAGIC) {
16001629
placeholder++;
16011630

1631+
switch (placeholder[0]) {
1632+
case 'w':
1633+
/*
1634+
* `%+w()` cannot ever expand to a non-empty string,
1635+
* and it potentially changes the layout of preceding
1636+
* contents. We're thus not able to handle the magic in
1637+
* this combination and refuse the pattern.
1638+
*/
1639+
return 0;
1640+
};
1641+
}
1642+
16021643
orig_len = sb->len;
16031644
if (((struct format_commit_context *)context)->flush_type != no_flush)
16041645
consumed = format_and_pad_commit(sb, placeholder, context);

t/t4205-log-pretty-formats.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,4 +867,80 @@ test_expect_success 'log --pretty=reference is colored appropriately' '
867867
test_cmp expect actual
868868
'
869869

870+
test_expect_success 'log --pretty with space stealing' '
871+
printf mm0 >expect &&
872+
git log -1 --pretty="format:mm%>>|(1)%x30" >actual &&
873+
test_cmp expect actual
874+
'
875+
876+
test_expect_success 'log --pretty with invalid padding format' '
877+
printf "%s%%<(20" "$(git rev-parse HEAD)" >expect &&
878+
git log -1 --pretty="format:%H%<(20" >actual &&
879+
test_cmp expect actual
880+
'
881+
882+
test_expect_success 'log --pretty with magical wrapping directives' '
883+
commit_id=$(git commit-tree HEAD^{tree} -m "describe me") &&
884+
git tag describe-me $commit_id &&
885+
printf "\n(tag:\ndescribe-me)%%+w(2)" >expect &&
886+
git log -1 --pretty="format:%w(1)%+d%+w(2)" $commit_id >actual &&
887+
test_cmp expect actual
888+
'
889+
890+
test_expect_success SIZE_T_IS_64BIT 'log --pretty with overflowing wrapping directive' '
891+
printf "%%w(2147483649,1,1)0" >expect &&
892+
git log -1 --pretty="format:%w(2147483649,1,1)%x30" >actual &&
893+
test_cmp expect actual &&
894+
printf "%%w(1,2147483649,1)0" >expect &&
895+
git log -1 --pretty="format:%w(1,2147483649,1)%x30" >actual &&
896+
test_cmp expect actual &&
897+
printf "%%w(1,1,2147483649)0" >expect &&
898+
git log -1 --pretty="format:%w(1,1,2147483649)%x30" >actual &&
899+
test_cmp expect actual
900+
'
901+
902+
test_expect_success SIZE_T_IS_64BIT 'log --pretty with overflowing padding directive' '
903+
printf "%%<(2147483649)0" >expect &&
904+
git log -1 --pretty="format:%<(2147483649)%x30" >actual &&
905+
test_cmp expect actual
906+
'
907+
908+
test_expect_success 'log --pretty with padding and preceding control chars' '
909+
printf "\20\20 0" >expect &&
910+
git log -1 --pretty="format:%x10%x10%>|(4)%x30" >actual &&
911+
test_cmp expect actual
912+
'
913+
914+
test_expect_success 'log --pretty truncation with control chars' '
915+
test_commit "$(printf "\20\20\20\20xxxx")" file contents commit-with-control-chars &&
916+
printf "\20\20\20\20x.." >expect &&
917+
git log -1 --pretty="format:%<(3,trunc)%s" commit-with-control-chars >actual &&
918+
test_cmp expect actual
919+
'
920+
921+
test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'log --pretty with huge commit message' '
922+
# We only assert that this command does not crash. This needs to be
923+
# executed with the address sanitizer to demonstrate failure.
924+
git log -1 --pretty="format:%>(2147483646)%x41%41%>(2147483646)%x41" >/dev/null
925+
'
926+
927+
test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'set up huge commit' '
928+
test-tool genzeros 2147483649 | tr "\000" "1" >expect &&
929+
huge_commit=$(git commit-tree -F expect HEAD^{tree})
930+
'
931+
932+
test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'log --pretty with huge commit message' '
933+
git log -1 --format="%B%<(1)%x30" $huge_commit >actual &&
934+
echo 0 >>expect &&
935+
test_cmp expect actual
936+
'
937+
938+
test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'log --pretty with huge commit message does not cause allocation failure' '
939+
test_must_fail git log -1 --format="%<(1)%B" $huge_commit 2>error &&
940+
cat >expect <<-EOF &&
941+
fatal: number too large to represent as int on this platform: 2147483649
942+
EOF
943+
test_cmp expect error
944+
'
945+
870946
test_done

t/test-lib.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,10 @@ build_option () {
16861686
sed -ne "s/^$1: //p"
16871687
}
16881688

1689+
test_lazy_prereq SIZE_T_IS_64BIT '
1690+
test 8 -eq "$(build_option sizeof-size_t)"
1691+
'
1692+
16891693
test_lazy_prereq LONG_IS_64BIT '
16901694
test 8 -le "$(build_option sizeof-long)"
16911695
'

utf8.c

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -206,26 +206,34 @@ int utf8_width(const char **start, size_t *remainder_p)
206206
* string, assuming that the string is utf8. Returns strlen() instead
207207
* if the string does not look like a valid utf8 string.
208208
*/
209-
int utf8_strnwidth(const char *string, int len, int skip_ansi)
209+
int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
210210
{
211-
int width = 0;
212211
const char *orig = string;
212+
size_t width = 0;
213213

214-
if (len == -1)
215-
len = strlen(string);
216214
while (string && string < orig + len) {
217-
int skip;
215+
int glyph_width;
216+
size_t skip;
217+
218218
while (skip_ansi &&
219219
(skip = display_mode_esc_sequence_len(string)) != 0)
220220
string += skip;
221-
width += utf8_width(&string, NULL);
221+
222+
glyph_width = utf8_width(&string, NULL);
223+
if (glyph_width > 0)
224+
width += glyph_width;
222225
}
223-
return string ? width : len;
226+
227+
/*
228+
* TODO: fix the interface of this function and `utf8_strwidth()` to
229+
* return `size_t` instead of `int`.
230+
*/
231+
return cast_size_t_to_int(string ? width : len);
224232
}
225233

226234
int utf8_strwidth(const char *string)
227235
{
228-
return utf8_strnwidth(string, -1, 0);
236+
return utf8_strnwidth(string, strlen(string), 0);
229237
}
230238

231239
int is_utf8(const char *text)
@@ -357,51 +365,52 @@ void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,
357365
void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
358366
const char *subst)
359367
{
360-
struct strbuf sb_dst = STRBUF_INIT;
361-
char *src = sb_src->buf;
362-
char *end = src + sb_src->len;
363-
char *dst;
364-
int w = 0, subst_len = 0;
368+
const char *src = sb_src->buf, *end = sb_src->buf + sb_src->len;
369+
struct strbuf dst;
370+
int w = 0;
365371

366-
if (subst)
367-
subst_len = strlen(subst);
368-
strbuf_grow(&sb_dst, sb_src->len + subst_len);
369-
dst = sb_dst.buf;
372+
strbuf_init(&dst, sb_src->len);
370373

371374
while (src < end) {
372-
char *old;
375+
const char *old;
376+
int glyph_width;
373377
size_t n;
374378

375379
while ((n = display_mode_esc_sequence_len(src))) {
376-
memcpy(dst, src, n);
380+
strbuf_add(&dst, src, n);
377381
src += n;
378-
dst += n;
379382
}
380383

381384
if (src >= end)
382385
break;
383386

384387
old = src;
385-
n = utf8_width((const char**)&src, NULL);
386-
if (!src) /* broken utf-8, do nothing */
388+
glyph_width = utf8_width((const char**)&src, NULL);
389+
if (!src) /* broken utf-8, do nothing */
387390
goto out;
388-
if (n && w >= pos && w < pos + width) {
391+
392+
/*
393+
* In case we see a control character we copy it into the
394+
* buffer, but don't add it to the width.
395+
*/
396+
if (glyph_width < 0)
397+
glyph_width = 0;
398+
399+
if (glyph_width && w >= pos && w < pos + width) {
389400
if (subst) {
390-
memcpy(dst, subst, subst_len);
391-
dst += subst_len;
401+
strbuf_addstr(&dst, subst);
392402
subst = NULL;
393403
}
394-
w += n;
395-
continue;
404+
} else {
405+
strbuf_add(&dst, old, src - old);
396406
}
397-
memcpy(dst, old, src - old);
398-
dst += src - old;
399-
w += n;
407+
408+
w += glyph_width;
400409
}
401-
strbuf_setlen(&sb_dst, dst - sb_dst.buf);
402-
strbuf_swap(sb_src, &sb_dst);
410+
411+
strbuf_swap(sb_src, &dst);
403412
out:
404-
strbuf_release(&sb_dst);
413+
strbuf_release(&dst);
405414
}
406415

407416
/*
@@ -791,7 +800,7 @@ int skip_utf8_bom(char **text, size_t len)
791800
void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
792801
const char *s)
793802
{
794-
int slen = strlen(s);
803+
size_t slen = strlen(s);
795804
int display_len = utf8_strnwidth(s, slen, 0);
796805
int utf8_compensation = slen - display_len;
797806

0 commit comments

Comments
 (0)