Skip to content

Commit e78b712

Browse files
j6tdscho
authored andcommitted
strbuf_vinsertf: provide the correct buffer size to vsnprintf
strbuf_vinsertf inserts a formatted string in the middle of an existing strbuf value. It makes room in the strbuf by moving existing string to the back, then formats the string to insert directly into the hole. It uses vsnprintf to format the string. The buffer size provided in the invocation is the number of characters available in the allocated space behind the final string. This does not make any sense at all. Fix it to pass the length of the inserted string plus one for the NUL. (The functions saves and restores the character that the NUL occupies.) Signed-off-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dd91bb5 commit e78b712

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

strbuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap)
270270
memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);
271271
/* vsnprintf() will append a NUL, overwriting one of our characters */
272272
save = sb->buf[pos + len];
273-
len2 = vsnprintf(sb->buf + pos, sb->alloc - sb->len, fmt, ap);
273+
len2 = vsnprintf(sb->buf + pos, len + 1, fmt, ap);
274274
sb->buf[pos + len] = save;
275275
if (len2 != len)
276276
BUG("your vsnprintf is broken (returns inconsistent lengths)");

0 commit comments

Comments
 (0)