summaryrefslogtreecommitdiff
path: root/src/filebuf.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@microsoft.com>2015-02-11 11:20:05 -0500
committerEdward Thomson <ethomson@edwardthomson.com>2015-02-12 22:54:47 -0500
commitec3b4d35f636c26d3c9b5703c3b7f87683800af8 (patch)
treea1d4330c4efaa1b7097f64f4243224e0581a5b2e /src/filebuf.c
parent2884cc42de8b20a58cec8488d014a853d47c047e (diff)
downloadlibgit2-ec3b4d35f636c26d3c9b5703c3b7f87683800af8.tar.gz
Use `size_t` to hold size of arrays
Use `size_t` to hold the size of arrays to ease overflow checking, lest we check for overflow of a `size_t` then promptly truncate by packing the length into a smaller type.
Diffstat (limited to 'src/filebuf.c')
-rw-r--r--src/filebuf.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/src/filebuf.c b/src/filebuf.c
index 1a9157558..94f2bec32 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -408,8 +408,8 @@ int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len)
int git_filebuf_printf(git_filebuf *file, const char *format, ...)
{
va_list arglist;
- size_t space_left;
- int len, res;
+ size_t space_left, len;
+ int written, res;
char *tmp_buffer;
ENSURE_BUF_OK(file);
@@ -418,15 +418,16 @@ int git_filebuf_printf(git_filebuf *file, const char *format, ...)
do {
va_start(arglist, format);
- len = p_vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist);
+ written = p_vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist);
va_end(arglist);
- if (len < 0) {
+ if (written < 0) {
file->last_error = BUFERR_MEM;
return -1;
}
- if ((size_t)len + 1 <= space_left) {
+ len = written;
+ if (len + 1 <= space_left) {
file->buf_pos += len;
return 0;
}
@@ -436,7 +437,7 @@ int git_filebuf_printf(git_filebuf *file, const char *format, ...)
space_left = file->buf_size - file->buf_pos;
- } while ((size_t)len + 1 <= space_left);
+ } while (len + 1 <= space_left);
if (GIT_ALLOC_OVERFLOW_ADD(len, 1) ||
!(tmp_buffer = git__malloc(len + 1))) {