Skip to content

Commit 751eb46

Browse files
committed
delta: validate sizes and cast safely
Quiet down a warning from MSVC about how we're potentially losing data. Validate that our data will fit into the type provided then cast.
1 parent 4947216 commit 751eb46

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/delta.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static int lookup_index_alloc(
138138
*out = git__malloc(index_len);
139139
GIT_ERROR_CHECK_ALLOC(*out);
140140

141-
*out_len = index_len;
141+
*out_len = (unsigned long)index_len;
142142
return 0;
143143
}
144144

@@ -286,6 +286,13 @@ int git_delta_create_from_index(
286286
if (!trg_buf || !trg_size)
287287
return 0;
288288

289+
if (index->src_size > UINT_MAX ||
290+
trg_size > UINT_MAX ||
291+
max_size > (UINT_MAX - MAX_OP_SIZE - 1)) {
292+
git_error_set(GIT_ERROR_INVALID, "buffer sizes too large for delta processing");
293+
return -1;
294+
}
295+
289296
bufpos = 0;
290297
bufsize = 8192;
291298
if (max_size && bufsize >= max_size)
@@ -294,15 +301,15 @@ int git_delta_create_from_index(
294301
GIT_ERROR_CHECK_ALLOC(buf);
295302

296303
/* store reference buffer size */
297-
i = index->src_size;
304+
i = (unsigned int)index->src_size;
298305
while (i >= 0x80) {
299306
buf[bufpos++] = i | 0x80;
300307
i >>= 7;
301308
}
302309
buf[bufpos++] = i;
303310

304311
/* store target buffer size */
305-
i = trg_size;
312+
i = (unsigned int)trg_size;
306313
while (i >= 0x80) {
307314
buf[bufpos++] = i | 0x80;
308315
i >>= 7;
@@ -423,7 +430,7 @@ int git_delta_create_from_index(
423430
void *tmp = buf;
424431
bufsize = bufsize * 3 / 2;
425432
if (max_size && bufsize >= max_size)
426-
bufsize = max_size + MAX_OP_SIZE + 1;
433+
bufsize = (unsigned int)(max_size + MAX_OP_SIZE + 1);
427434
if (max_size && bufpos > max_size)
428435
break;
429436
buf = git__realloc(buf, bufsize);

0 commit comments

Comments
 (0)