Skip to content

Commit ffeaf2f

Browse files
rscharfegitster
authored andcommitted
mem-pool: use st_add() in mem_pool_strvfmt()
If len is INT_MAX in mem_pool_strvfmt(), then len + 1 overflows. Casting it to size_t would prevent that. Use st_add() to go a step further and make the addition *obviously* safe. The compiler can optimize the check away on platforms where SIZE_MAX > INT_MAX, i.e. basically everywhere. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f39addd commit ffeaf2f

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

mem-pool.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ static char *mem_pool_strvfmt(struct mem_pool *pool, const char *fmt,
115115
size_t available = block ? block->end - block->next_free : 0;
116116
va_list cp;
117117
int len, len2;
118+
size_t size;
118119
char *ret;
119120

120121
va_copy(cp, ap);
@@ -123,13 +124,14 @@ static char *mem_pool_strvfmt(struct mem_pool *pool, const char *fmt,
123124
if (len < 0)
124125
BUG("your vsnprintf is broken (returned %d)", len);
125126

126-
ret = mem_pool_alloc(pool, len + 1); /* 1 for NUL */
127+
size = st_add(len, 1); /* 1 for NUL */
128+
ret = mem_pool_alloc(pool, size);
127129

128130
/* Shortcut; relies on mem_pool_alloc() not touching buffer contents. */
129131
if (ret == next_free)
130132
return ret;
131133

132-
len2 = vsnprintf(ret, len + 1, fmt, ap);
134+
len2 = vsnprintf(ret, size, fmt, ap);
133135
if (len2 != len)
134136
BUG("your vsnprintf is broken (returns inconsistent lengths)");
135137
return ret;

0 commit comments

Comments
 (0)