Skip to content

Commit d3c2749

Browse files
committed
rerere: fix benign off-by-one non-bug and clarify code
rerere_io_putconflict() wants to use a limited fixed-sized buf[] on stack repeatedly to formulate a longer string, but its implementation is doubly confusing: * When it knows that the whole thing fits in buf[], it wants to fill early part of buf[] with conflict marker characters, followed by a LF and a NUL. It miscounts the size of the buffer by 1 and does not use the last byte of buf[]. * When it needs to show only the early part of a long conflict marker string (because the whole thing does not fit in buf[]), it adjusts the number of bytes shown in the current round in a strange-looking way. It makes sure that this round does not emit all bytes and leaves at least one byte to the next round, so that "it all fits" case will pick up the rest and show the terminating LF. While this is correct, one needs to stop and think for a while to realize why it is correct without an explanation. Fix the benign off-by-one, and add comments to explain the strange-looking size adjustment. Signed-off-by: Junio C Hamano <[email protected]>
1 parent a96847c commit d3c2749

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

rerere.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,20 @@ static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
125125
char buf[64];
126126

127127
while (size) {
128-
if (size < sizeof(buf) - 2) {
128+
if (size <= sizeof(buf) - 2) {
129129
memset(buf, ch, size);
130130
buf[size] = '\n';
131131
buf[size + 1] = '\0';
132132
size = 0;
133133
} else {
134134
int sz = sizeof(buf) - 1;
135+
136+
/*
137+
* Make sure we will not write everything out
138+
* in this round by leaving at least 1 byte
139+
* for the next round, giving the next round
140+
* a chance to add the terminating LF. Yuck.
141+
*/
135142
if (size <= sz)
136143
sz -= (sz - size) + 1;
137144
memset(buf, ch, sz);

0 commit comments

Comments
 (0)