Skip to content

Commit 980419b

Browse files
Ramsay Jonesgitster
authored andcommitted
pretty: Fix bug in truncation support for %>, %< and %><
Some systems experience failures in t4205-*.sh (tests 18-20, 27) which all relate to the use of truncation with the %< padding placeholder. This capability was added in the commit a7f01c6 ("pretty: support truncating in %>, %< and %><", 19-04-2013). The truncation support was implemented with the assistance of a new strbuf function (strbuf_utf8_replace). This function contains the following code: strbuf_attach(sb_src, strbuf_detach(&sb_dst, NULL), sb_dst.len, sb_dst.alloc); Unfortunately, this code is subject to unspecified behaviour. In particular, the order of evaluation of the argument expressions (along with the associated side effects) is not specified by the C standard. Note that the second argument expression is a call to strbuf_detach() which, as a side effect, sets the 'len' and 'alloc' fields of the sb_dst argument to zero. Depending on the order of evaluation of the argument expressions to the strbuf_attach call, this can lead to assigning an empty string to 'sb_src'. In order to remove the undesired behaviour, we replace the above line of code with: strbuf_swap(sb_src, &sb_dst); strbuf_release(&sb_dst); which achieves the desired effect without provoking unspecified behaviour. Signed-off-by: Ramsay Jones <[email protected]> Acked-by: Duy Nguyen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1640632 commit 980419b

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

utf8.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
463463
w += n;
464464
}
465465
strbuf_setlen(&sb_dst, dst - sb_dst.buf);
466-
strbuf_attach(sb_src, strbuf_detach(&sb_dst, NULL),
467-
sb_dst.len, sb_dst.alloc);
466+
strbuf_swap(sb_src, &sb_dst);
467+
strbuf_release(&sb_dst);
468468
}
469469

470470
int is_encoding_utf8(const char *name)

0 commit comments

Comments
 (0)