Unverified Commit c5448501 by Edward Thomson Committed by GitHub

Merge pull request #5078 from libgit2/ethomson/warnings

Remove warnings
parents a064920e e61b92e0
...@@ -239,10 +239,22 @@ ELSE () ...@@ -239,10 +239,22 @@ ELSE ()
ENABLE_WARNINGS(shift-count-overflow) ENABLE_WARNINGS(shift-count-overflow)
ENABLE_WARNINGS(unused-const-variable) ENABLE_WARNINGS(unused-const-variable)
ENABLE_WARNINGS(unused-function) ENABLE_WARNINGS(unused-function)
ENABLE_WARNINGS(int-conversion)
# MinGW uses gcc, which expects POSIX formatting for printf, but
# uses the Windows C library, which uses its own format specifiers.
# Disable format specifier warnings.
IF(MINGW)
DISABLE_WARNINGS(format)
DISABLE_WARNINGS(format-security)
ELSE()
ENABLE_WARNINGS(format) ENABLE_WARNINGS(format)
ENABLE_WARNINGS(format-security) ENABLE_WARNINGS(format-security)
ENABLE_WARNINGS(int-conversion) ENDIF()
IF("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
DISABLE_WARNINGS(documentation-deprecated-sync) DISABLE_WARNINGS(documentation-deprecated-sync)
ENDIF()
IF (PROFILE) IF (PROFILE)
SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}") SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
......
...@@ -7,5 +7,9 @@ MACRO(DISABLE_WARNINGS flag) ...@@ -7,5 +7,9 @@ MACRO(DISABLE_WARNINGS flag)
ENDMACRO() ENDMACRO()
IF(ENABLE_WERROR) IF(ENABLE_WERROR)
IF(MSVC)
ADD_COMPILE_OPTIONS(-WX)
ELSE()
ADD_C_FLAG_IF_SUPPORTED(-Werror) ADD_C_FLAG_IF_SUPPORTED(-Werror)
ENDIF()
ENDIF() ENDIF()
...@@ -1992,6 +1992,9 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) { ...@@ -1992,6 +1992,9 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
const char *p; const char *p;
size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len; size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
if (buflen > UINT16_MAX)
return 1;
u->field_data[UF_HOST].len = 0; u->field_data[UF_HOST].len = 0;
s = found_at ? s_http_userinfo_start : s_http_host_start; s = found_at ? s_http_userinfo_start : s_http_host_start;
...@@ -2006,21 +2009,21 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) { ...@@ -2006,21 +2009,21 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
switch(new_s) { switch(new_s) {
case s_http_host: case s_http_host:
if (s != s_http_host) { if (s != s_http_host) {
u->field_data[UF_HOST].off = p - buf; u->field_data[UF_HOST].off = (uint16_t)(p - buf);
} }
u->field_data[UF_HOST].len++; u->field_data[UF_HOST].len++;
break; break;
case s_http_host_v6: case s_http_host_v6:
if (s != s_http_host_v6) { if (s != s_http_host_v6) {
u->field_data[UF_HOST].off = p - buf; u->field_data[UF_HOST].off = (uint16_t)(p - buf);
} }
u->field_data[UF_HOST].len++; u->field_data[UF_HOST].len++;
break; break;
case s_http_host_port: case s_http_host_port:
if (s != s_http_host_port) { if (s != s_http_host_port) {
u->field_data[UF_PORT].off = p - buf; u->field_data[UF_PORT].off = (uint16_t)(p - buf);
u->field_data[UF_PORT].len = 0; u->field_data[UF_PORT].len = 0;
u->field_set |= (1 << UF_PORT); u->field_set |= (1 << UF_PORT);
} }
...@@ -2029,7 +2032,7 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) { ...@@ -2029,7 +2032,7 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
case s_http_userinfo: case s_http_userinfo:
if (s != s_http_userinfo) { if (s != s_http_userinfo) {
u->field_data[UF_USERINFO].off = p - buf ; u->field_data[UF_USERINFO].off = (uint16_t)(p - buf);
u->field_data[UF_USERINFO].len = 0; u->field_data[UF_USERINFO].len = 0;
u->field_set |= (1 << UF_USERINFO); u->field_set |= (1 << UF_USERINFO);
} }
...@@ -2066,6 +2069,9 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect, ...@@ -2066,6 +2069,9 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
enum http_parser_url_fields uf, old_uf; enum http_parser_url_fields uf, old_uf;
int found_at = 0; int found_at = 0;
if (buflen > UINT16_MAX)
return 1;
u->port = u->field_set = 0; u->port = u->field_set = 0;
s = is_connect ? s_req_server_start : s_req_spaces_before_url; s = is_connect ? s_req_server_start : s_req_spaces_before_url;
uf = old_uf = UF_MAX; uf = old_uf = UF_MAX;
...@@ -2121,7 +2127,7 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect, ...@@ -2121,7 +2127,7 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
continue; continue;
} }
u->field_data[uf].off = p - buf; u->field_data[uf].off = (uint16_t)(p - buf);
u->field_data[uf].len = 1; u->field_data[uf].len = 1;
u->field_set |= (1 << uf); u->field_set |= (1 << uf);
......
...@@ -177,10 +177,3 @@ uLong ZEXPORT adler32_combine(adler1, adler2, len2) ...@@ -177,10 +177,3 @@ uLong ZEXPORT adler32_combine(adler1, adler2, len2)
return adler32_combine_(adler1, adler2, len2); return adler32_combine_(adler1, adler2, len2);
} }
uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
uLong adler1;
uLong adler2;
z_off64_t len2;
{
return adler32_combine_(adler1, adler2, len2);
}
...@@ -433,10 +433,3 @@ uLong ZEXPORT crc32_combine(crc1, crc2, len2) ...@@ -433,10 +433,3 @@ uLong ZEXPORT crc32_combine(crc1, crc2, len2)
return crc32_combine_(crc1, crc2, len2); return crc32_combine_(crc1, crc2, len2);
} }
uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
uLong crc1;
uLong crc2;
z_off64_t len2;
{
return crc32_combine_(crc1, crc2, len2);
}
...@@ -267,7 +267,7 @@ GIT_EXTERN(void) git_treebuilder_clear(git_treebuilder *bld); ...@@ -267,7 +267,7 @@ GIT_EXTERN(void) git_treebuilder_clear(git_treebuilder *bld);
* @param bld a previously loaded treebuilder. * @param bld a previously loaded treebuilder.
* @return the number of entries in the treebuilder * @return the number of entries in the treebuilder
*/ */
GIT_EXTERN(unsigned int) git_treebuilder_entrycount(git_treebuilder *bld); GIT_EXTERN(size_t) git_treebuilder_entrycount(git_treebuilder *bld);
/** /**
* Free a tree builder * Free a tree builder
......
...@@ -314,12 +314,20 @@ ELSE() ...@@ -314,12 +314,20 @@ ELSE()
ENDIF() ENDIF()
FILE(GLOB SRC_OS unix/*.c unix/*.h) FILE(GLOB SRC_OS unix/*.c unix/*.h)
ENDIF() ENDIF()
FILE(GLOB SRC_GIT2 *.c *.h FILE(GLOB SRC_GIT2 *.c *.h
allocators/*.c allocators/*.h allocators/*.c allocators/*.h
streams/*.c streams/*.h streams/*.c streams/*.h
transports/*.c transports/*.h transports/*.c transports/*.h
xdiff/*.c xdiff/*.h) xdiff/*.c xdiff/*.h)
# the xdiff dependency is not (yet) warning-free, disable warnings as
# errors for the xdiff sources until we've sorted them out
IF(MSVC)
SET_SOURCE_FILES_PROPERTIES(xdiff/xdiffi.c PROPERTIES COMPILE_FLAGS -WX-)
SET_SOURCE_FILES_PROPERTIES(xdiff/xutils.c PROPERTIES COMPILE_FLAGS -WX-)
ENDIF()
# Determine architecture of the machine # Determine architecture of the machine
IF (CMAKE_SIZEOF_VOID_P EQUAL 8) IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
SET(GIT_ARCH_64 1) SET(GIT_ARCH_64 1)
......
...@@ -54,7 +54,7 @@ int git_attr_cache__alloc_file_entry( ...@@ -54,7 +54,7 @@ int git_attr_cache__alloc_file_entry(
cachesize++; cachesize++;
} }
ce = git_pool_mallocz(pool, (uint32_t)cachesize); ce = git_pool_mallocz(pool, cachesize);
GIT_ERROR_CHECK_ALLOC(ce); GIT_ERROR_CHECK_ALLOC(ce);
if (baselen) { if (baselen) {
......
...@@ -73,7 +73,7 @@ static git_commit_list_node **alloc_parents( ...@@ -73,7 +73,7 @@ static git_commit_list_node **alloc_parents(
return (git_commit_list_node **)((char *)commit + sizeof(git_commit_list_node)); return (git_commit_list_node **)((char *)commit + sizeof(git_commit_list_node));
return (git_commit_list_node **)git_pool_malloc( return (git_commit_list_node **)git_pool_malloc(
&walk->commit_pool, (uint32_t)(n_parents * sizeof(git_commit_list_node *))); &walk->commit_pool, (n_parents * sizeof(git_commit_list_node *)));
} }
......
...@@ -88,6 +88,11 @@ static int parse_subsection_header(git_config_parser *reader, const char *line, ...@@ -88,6 +88,11 @@ static int parse_subsection_header(git_config_parser *reader, const char *line,
last_quote = strrchr(line, '"'); last_quote = strrchr(line, '"');
quoted_len = last_quote - first_quote; quoted_len = last_quote - first_quote;
if ((last_quote - line) > INT_MAX) {
set_parse_error(reader, 0, "invalid section header, line too long");
goto end_error;
}
if (quoted_len == 0) { if (quoted_len == 0) {
set_parse_error(reader, 0, "missing closing quotation mark in section header"); set_parse_error(reader, 0, "missing closing quotation mark in section header");
goto end_error; goto end_error;
...@@ -146,7 +151,7 @@ end_parse: ...@@ -146,7 +151,7 @@ end_parse:
} }
*section_name = git_buf_detach(&buf); *section_name = git_buf_detach(&buf);
return &line[rpos + 2] - line_start; /* rpos is at the closing quote */ return (int)(&line[rpos + 2] - line_start); /* rpos is at the closing quote */
end_error: end_error:
git_buf_dispose(&buf); git_buf_dispose(&buf);
......
...@@ -636,9 +636,7 @@ retry_lstat: ...@@ -636,9 +636,7 @@ retry_lstat:
size_t alloc_size; size_t alloc_size;
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1); GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1);
if (!git__is_uint32(alloc_size)) cache_path = git_pool_malloc(opts->pool, alloc_size);
return -1;
cache_path = git_pool_malloc(opts->pool, (uint32_t)alloc_size);
GIT_ERROR_CHECK_ALLOC(cache_path); GIT_ERROR_CHECK_ALLOC(cache_path);
memcpy(cache_path, make_path.ptr, make_path.size + 1); memcpy(cache_path, make_path.ptr, make_path.size + 1);
......
...@@ -70,7 +70,8 @@ void git_idxmap_icase_clear(git_idxmap_icase *map) ...@@ -70,7 +70,8 @@ void git_idxmap_icase_clear(git_idxmap_icase *map)
int git_idxmap_resize(git_idxmap *map, size_t size) int git_idxmap_resize(git_idxmap *map, size_t size)
{ {
if (kh_resize(idx, map, size) < 0) { if (!git__is_uint32(size) ||
kh_resize(idx, map, (khiter_t)size) < 0) {
git_error_set_oom(); git_error_set_oom();
return -1; return -1;
} }
...@@ -79,7 +80,8 @@ int git_idxmap_resize(git_idxmap *map, size_t size) ...@@ -79,7 +80,8 @@ int git_idxmap_resize(git_idxmap *map, size_t size)
int git_idxmap_icase_resize(git_idxmap_icase *map, size_t size) int git_idxmap_icase_resize(git_idxmap_icase *map, size_t size)
{ {
if (kh_resize(idxicase, map, size) < 0) { if (!git__is_uint32(size) ||
kh_resize(idxicase, map, (khiter_t)size) < 0) {
git_error_set_oom(); git_error_set_oom();
return -1; return -1;
} }
......
...@@ -1474,6 +1474,11 @@ int git_index_add_from_buffer( ...@@ -1474,6 +1474,11 @@ int git_index_add_from_buffer(
return -1; return -1;
} }
if (len > UINT32_MAX) {
git_error_set(GIT_ERROR_INDEX, "buffer is too large");
return -1;
}
if (index_entry_dup(&entry, index, source_entry) < 0) if (index_entry_dup(&entry, index, source_entry) < 0)
return -1; return -1;
...@@ -1484,7 +1489,7 @@ int git_index_add_from_buffer( ...@@ -1484,7 +1489,7 @@ int git_index_add_from_buffer(
} }
git_oid_cpy(&entry->id, &id); git_oid_cpy(&entry->id, &id);
entry->file_size = len; entry->file_size = (uint32_t)len;
if ((error = index_insert(index, &entry, 1, true, true, true)) < 0) if ((error = index_insert(index, &entry, 1, true, true, true)) < 0)
return error; return error;
......
...@@ -867,7 +867,7 @@ static int merge_conflict_invoke_driver( ...@@ -867,7 +867,7 @@ static int merge_conflict_invoke_driver(
git_oid_cpy(&result->id, &oid); git_oid_cpy(&result->id, &oid);
result->mode = mode; result->mode = mode;
result->file_size = buf.size; result->file_size = (uint32_t)buf.size;
result->path = git_pool_strdup(&diff_list->pool, path); result->path = git_pool_strdup(&diff_list->pool, path);
GIT_ERROR_CHECK_ALLOC(result->path); GIT_ERROR_CHECK_ALLOC(result->path);
......
...@@ -37,14 +37,17 @@ void gitno_buffer_setup_callback( ...@@ -37,14 +37,17 @@ void gitno_buffer_setup_callback(
static int recv_stream(gitno_buffer *buf) static int recv_stream(gitno_buffer *buf)
{ {
git_stream *io = (git_stream *) buf->cb_data; git_stream *io = (git_stream *) buf->cb_data;
int ret; size_t readlen = buf->len - buf->offset;
ssize_t ret;
ret = git_stream_read(io, buf->data + buf->offset, buf->len - buf->offset); readlen = min(readlen, INT_MAX);
ret = git_stream_read(io, buf->data + buf->offset, (int)readlen);
if (ret < 0) if (ret < 0)
return -1; return -1;
buf->offset += ret; buf->offset += ret;
return ret; return (int)ret;
} }
void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data, size_t len) void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data, size_t len)
......
...@@ -408,7 +408,8 @@ done: ...@@ -408,7 +408,8 @@ done:
static int read_header_loose(git_rawobj *out, git_buf *loc) static int read_header_loose(git_rawobj *out, git_buf *loc)
{ {
unsigned char obj[1024]; unsigned char obj[1024];
int fd, obj_len, error; ssize_t obj_len;
int fd, error;
assert(out && loc); assert(out && loc);
...@@ -417,10 +418,14 @@ static int read_header_loose(git_rawobj *out, git_buf *loc) ...@@ -417,10 +418,14 @@ static int read_header_loose(git_rawobj *out, git_buf *loc)
out->data = NULL; out->data = NULL;
if ((error = fd = git_futils_open_ro(loc->ptr)) < 0 || if ((error = fd = git_futils_open_ro(loc->ptr)) < 0)
(error = obj_len = p_read(fd, obj, sizeof(obj))) < 0)
goto done; goto done;
if ((obj_len = p_read(fd, obj, sizeof(obj))) < 0) {
error = (int)obj_len;
goto done;
}
if (!is_zlib_compressed_data(obj, (size_t)obj_len)) if (!is_zlib_compressed_data(obj, (size_t)obj_len))
error = read_header_loose_packlike(out, obj, (size_t)obj_len); error = read_header_loose_packlike(out, obj, (size_t)obj_len);
else else
...@@ -871,6 +876,8 @@ static int loose_backend__readstream_read( ...@@ -871,6 +876,8 @@ static int loose_backend__readstream_read(
size_t start_remain = stream->start_len - stream->start_read; size_t start_remain = stream->start_len - stream->start_read;
int total = 0, error; int total = 0, error;
buffer_len = min(buffer_len, INT_MAX);
/* /*
* if we read more than just the header in the initial read, play * if we read more than just the header in the initial read, play
* that back for the caller. * that back for the caller.
...@@ -882,20 +889,20 @@ static int loose_backend__readstream_read( ...@@ -882,20 +889,20 @@ static int loose_backend__readstream_read(
buffer += chunk; buffer += chunk;
stream->start_read += chunk; stream->start_read += chunk;
total += chunk; total += (int)chunk;
buffer_len -= chunk; buffer_len -= chunk;
} }
if (buffer_len) { if (buffer_len) {
size_t chunk = min(buffer_len, INT_MAX); size_t chunk = buffer_len;
if ((error = git_zstream_get_output(buffer, &chunk, &stream->zstream)) < 0) if ((error = git_zstream_get_output(buffer, &chunk, &stream->zstream)) < 0)
return error; return error;
total += chunk; total += (int)chunk;
} }
return total; return (int)total;
} }
static void loose_backend__readstream_free(git_odb_stream *_stream) static void loose_backend__readstream_free(git_odb_stream *_stream)
......
...@@ -33,7 +33,7 @@ typedef struct { ...@@ -33,7 +33,7 @@ typedef struct {
char *old_prefix, *new_prefix; char *old_prefix, *new_prefix;
} git_patch_parsed; } git_patch_parsed;
static int header_path_len(git_patch_parse_ctx *ctx) static size_t header_path_len(git_patch_parse_ctx *ctx)
{ {
bool inquote = 0; bool inquote = 0;
bool quoted = git_parse_ctx_contains_s(&ctx->parse_ctx, "\""); bool quoted = git_parse_ctx_contains_s(&ctx->parse_ctx, "\"");
......
...@@ -160,7 +160,13 @@ int git_path_dirname_r(git_buf *buffer, const char *path) ...@@ -160,7 +160,13 @@ int git_path_dirname_r(git_buf *buffer, const char *path)
while (endp > path && *endp == '/') while (endp > path && *endp == '/')
endp--; endp--;
if ((len = win32_prefix_length(path, endp - path + 1)) > 0) { if (endp - path + 1 > INT_MAX) {
git_error_set(GIT_ERROR_INVALID, "path too long");
len = -1;
goto Exit;
}
if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) {
is_prefix = 1; is_prefix = 1;
goto Exit; goto Exit;
} }
...@@ -180,7 +186,13 @@ int git_path_dirname_r(git_buf *buffer, const char *path) ...@@ -180,7 +186,13 @@ int git_path_dirname_r(git_buf *buffer, const char *path)
endp--; endp--;
} while (endp > path && *endp == '/'); } while (endp > path && *endp == '/');
if ((len = win32_prefix_length(path, endp - path + 1)) > 0) { if (endp - path + 1 > INT_MAX) {
git_error_set(GIT_ERROR_INVALID, "path too long");
len = -1;
goto Exit;
}
if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) {
is_prefix = 1; is_prefix = 1;
goto Exit; goto Exit;
} }
......
...@@ -14,30 +14,30 @@ ...@@ -14,30 +14,30 @@
struct git_pool_page { struct git_pool_page {
git_pool_page *next; git_pool_page *next;
uint32_t size; size_t size;
uint32_t avail; size_t avail;
GIT_ALIGN(char data[GIT_FLEX_ARRAY], 8); GIT_ALIGN(char data[GIT_FLEX_ARRAY], 8);
}; };
static void *pool_alloc_page(git_pool *pool, uint32_t size); static void *pool_alloc_page(git_pool *pool, size_t size);
uint32_t git_pool__system_page_size(void) size_t git_pool__system_page_size(void)
{ {
static uint32_t size = 0; static size_t size = 0;
if (!size) { if (!size) {
size_t page_size; size_t page_size;
if (git__page_size(&page_size) < 0) if (git__page_size(&page_size) < 0)
page_size = 4096; page_size = 4096;
/* allow space for malloc overhead */ /* allow space for malloc overhead */
size = page_size - (2 * sizeof(void *)) - sizeof(git_pool_page); size = (page_size - (2 * sizeof(void *)) - sizeof(git_pool_page));
} }
return size; return size;
} }
#ifndef GIT_DEBUG_POOL #ifndef GIT_DEBUG_POOL
void git_pool_init(git_pool *pool, uint32_t item_size) void git_pool_init(git_pool *pool, size_t item_size)
{ {
assert(pool); assert(pool);
assert(item_size >= 1); assert(item_size >= 1);
...@@ -59,10 +59,10 @@ void git_pool_clear(git_pool *pool) ...@@ -59,10 +59,10 @@ void git_pool_clear(git_pool *pool)
pool->pages = NULL; pool->pages = NULL;
} }
static void *pool_alloc_page(git_pool *pool, uint32_t size) static void *pool_alloc_page(git_pool *pool, size_t size)
{ {
git_pool_page *page; git_pool_page *page;
const uint32_t new_page_size = (size <= pool->page_size) ? pool->page_size : size; const size_t new_page_size = (size <= pool->page_size) ? pool->page_size : size;
size_t alloc_size; size_t alloc_size;
if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, new_page_size, sizeof(git_pool_page)) || if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, new_page_size, sizeof(git_pool_page)) ||
...@@ -78,7 +78,7 @@ static void *pool_alloc_page(git_pool *pool, uint32_t size) ...@@ -78,7 +78,7 @@ static void *pool_alloc_page(git_pool *pool, uint32_t size)
return page->data; return page->data;
} }
static void *pool_alloc(git_pool *pool, uint32_t size) static void *pool_alloc(git_pool *pool, size_t size)
{ {
git_pool_page *page = pool->pages; git_pool_page *page = pool->pages;
void *ptr = NULL; void *ptr = NULL;
...@@ -125,7 +125,7 @@ static int git_pool__ptr_cmp(const void * a, const void * b) ...@@ -125,7 +125,7 @@ static int git_pool__ptr_cmp(const void * a, const void * b)
} }
} }
void git_pool_init(git_pool *pool, uint32_t item_size) void git_pool_init(git_pool *pool, size_t item_size)
{ {
assert(pool); assert(pool);
assert(item_size >= 1); assert(item_size >= 1);
...@@ -141,7 +141,7 @@ void git_pool_clear(git_pool *pool) ...@@ -141,7 +141,7 @@ void git_pool_clear(git_pool *pool)
git_vector_free_deep(&pool->allocations); git_vector_free_deep(&pool->allocations);
} }
static void *pool_alloc(git_pool *pool, uint32_t size) { static void *pool_alloc(git_pool *pool, size_t size) {
void *ptr = NULL; void *ptr = NULL;
if((ptr = git__malloc(size)) == NULL) { if((ptr = git__malloc(size)) == NULL) {
return NULL; return NULL;
...@@ -169,26 +169,26 @@ void git_pool_swap(git_pool *a, git_pool *b) ...@@ -169,26 +169,26 @@ void git_pool_swap(git_pool *a, git_pool *b)
memcpy(b, &temp, sizeof(temp)); memcpy(b, &temp, sizeof(temp));
} }
static uint32_t alloc_size(git_pool *pool, uint32_t count) static size_t alloc_size(git_pool *pool, size_t count)
{ {
const uint32_t align = sizeof(void *) - 1; const size_t align = sizeof(void *) - 1;
if (pool->item_size > 1) { if (pool->item_size > 1) {
const uint32_t item_size = (pool->item_size + align) & ~align; const size_t item_size = (pool->item_size + align) & ~align;
return item_size * count; return item_size * count;
} }
return (count + align) & ~align; return (count + align) & ~align;
} }
void *git_pool_malloc(git_pool *pool, uint32_t items) void *git_pool_malloc(git_pool *pool, size_t items)
{ {
return pool_alloc(pool, alloc_size(pool, items)); return pool_alloc(pool, alloc_size(pool, items));
} }
void *git_pool_mallocz(git_pool *pool, uint32_t items) void *git_pool_mallocz(git_pool *pool, size_t items)
{ {
const uint32_t size = alloc_size(pool, items); const size_t size = alloc_size(pool, items);
void *ptr = pool_alloc(pool, size); void *ptr = pool_alloc(pool, size);
if (ptr) if (ptr)
memset(ptr, 0x0, size); memset(ptr, 0x0, size);
...@@ -201,10 +201,10 @@ char *git_pool_strndup(git_pool *pool, const char *str, size_t n) ...@@ -201,10 +201,10 @@ char *git_pool_strndup(git_pool *pool, const char *str, size_t n)
assert(pool && str && pool->item_size == sizeof(char)); assert(pool && str && pool->item_size == sizeof(char));
if ((uint32_t)(n + 1) < n) if (n == SIZE_MAX)
return NULL; return NULL;
if ((ptr = git_pool_malloc(pool, (uint32_t)(n + 1))) != NULL) { if ((ptr = git_pool_malloc(pool, (n + 1))) != NULL) {
memcpy(ptr, str, n); memcpy(ptr, str, n);
ptr[n] = '\0'; ptr[n] = '\0';
} }
...@@ -226,14 +226,18 @@ char *git_pool_strdup_safe(git_pool *pool, const char *str) ...@@ -226,14 +226,18 @@ char *git_pool_strdup_safe(git_pool *pool, const char *str)
char *git_pool_strcat(git_pool *pool, const char *a, const char *b) char *git_pool_strcat(git_pool *pool, const char *a, const char *b)
{ {
void *ptr; void *ptr;
size_t len_a, len_b; size_t len_a, len_b, total;
assert(pool && pool->item_size == sizeof(char)); assert(pool && pool->item_size == sizeof(char));
len_a = a ? strlen(a) : 0; len_a = a ? strlen(a) : 0;
len_b = b ? strlen(b) : 0; len_b = b ? strlen(b) : 0;
if ((ptr = git_pool_malloc(pool, (uint32_t)(len_a + len_b + 1))) != NULL) { if (GIT_ADD_SIZET_OVERFLOW(&total, len_a, len_b) ||
GIT_ADD_SIZET_OVERFLOW(&total, total, 1))
return NULL;
if ((ptr = git_pool_malloc(pool, total)) != NULL) {
if (len_a) if (len_a)
memcpy(ptr, a, len_a); memcpy(ptr, a, len_a);
if (len_b) if (len_b)
......
...@@ -32,8 +32,8 @@ typedef struct git_pool_page git_pool_page; ...@@ -32,8 +32,8 @@ typedef struct git_pool_page git_pool_page;
*/ */
typedef struct { typedef struct {
git_pool_page *pages; /* allocated pages */ git_pool_page *pages; /* allocated pages */
uint32_t item_size; /* size of single alloc unit in bytes */ size_t item_size; /* size of single alloc unit in bytes */
uint32_t page_size; /* size of page in bytes */ size_t page_size; /* size of page in bytes */
} git_pool; } git_pool;
#define GIT_POOL_INIT { NULL, 0, 0 } #define GIT_POOL_INIT { NULL, 0, 0 }
...@@ -57,8 +57,8 @@ typedef struct { ...@@ -57,8 +57,8 @@ typedef struct {
*/ */
typedef struct { typedef struct {
git_vector allocations; git_vector allocations;
uint32_t item_size; size_t item_size;
uint32_t page_size; size_t page_size;
} git_pool; } git_pool;
#define GIT_POOL_INIT { GIT_VECTOR_INIT, 0, 0 } #define GIT_POOL_INIT { GIT_VECTOR_INIT, 0, 0 }
...@@ -81,7 +81,7 @@ typedef struct { ...@@ -81,7 +81,7 @@ typedef struct {
* Of course, you can use this in other ways, but those are the * Of course, you can use this in other ways, but those are the
* two most common patterns. * two most common patterns.
*/ */
extern void git_pool_init(git_pool *pool, uint32_t item_size); extern void git_pool_init(git_pool *pool, size_t item_size);
/** /**
* Free all items in pool * Free all items in pool
...@@ -96,8 +96,8 @@ extern void git_pool_swap(git_pool *a, git_pool *b); ...@@ -96,8 +96,8 @@ extern void git_pool_swap(git_pool *a, git_pool *b);
/** /**
* Allocate space for one or more items from a pool. * Allocate space for one or more items from a pool.
*/ */
extern void *git_pool_malloc(git_pool *pool, uint32_t items); extern void *git_pool_malloc(git_pool *pool, size_t items);
extern void *git_pool_mallocz(git_pool *pool, uint32_t items); extern void *git_pool_mallocz(git_pool *pool, size_t items);
/** /**
* Allocate space and duplicate string data into it. * Allocate space and duplicate string data into it.
......
...@@ -298,7 +298,8 @@ int git_rebase_open( ...@@ -298,7 +298,8 @@ int git_rebase_open(
git_rebase *rebase; git_rebase *rebase;
git_buf path = GIT_BUF_INIT, orig_head_name = GIT_BUF_INIT, git_buf path = GIT_BUF_INIT, orig_head_name = GIT_BUF_INIT,
orig_head_id = GIT_BUF_INIT, onto_id = GIT_BUF_INIT; orig_head_id = GIT_BUF_INIT, onto_id = GIT_BUF_INIT;
int state_path_len, error; size_t state_path_len;
int error;
assert(repo); assert(repo);
......
...@@ -878,7 +878,8 @@ int git_repository_open_from_worktree(git_repository **repo_out, git_worktree *w ...@@ -878,7 +878,8 @@ int git_repository_open_from_worktree(git_repository **repo_out, git_worktree *w
{ {
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
git_repository *repo = NULL; git_repository *repo = NULL;
int len, err; size_t len;
int err;
assert(repo_out && wt); assert(repo_out && wt);
......
...@@ -282,7 +282,7 @@ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key) ...@@ -282,7 +282,7 @@ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key)
itemlen = sc->item_path_offset + keylen + 1; itemlen = sc->item_path_offset + keylen + 1;
itemlen = (itemlen + 7) & ~7; itemlen = (itemlen + 7) & ~7;
if ((item = git_pool_mallocz(&sc->pool, (uint32_t)itemlen)) == NULL) { if ((item = git_pool_mallocz(&sc->pool, itemlen)) == NULL) {
/* don't use GIT_ERROR_CHECK_ALLOC b/c of lock */ /* don't use GIT_ERROR_CHECK_ALLOC b/c of lock */
error = -1; error = -1;
goto done; goto done;
......
...@@ -42,15 +42,19 @@ static const char *next_line(const char *str) ...@@ -42,15 +42,19 @@ static const char *next_line(const char *str)
} }
/* /*
* Return the position of the start of the last line. If len is 0, return -1. * Return the position of the start of the last line. If len is 0, return 0.
*/ */
static int last_line(const char *buf, size_t len) static bool last_line(size_t *out, const char *buf, size_t len)
{ {
int i; size_t i;
*out = 0;
if (len == 0) if (len == 0)
return -1; return false;
if (len == 1) if (len == 1)
return 0; return true;
/* /*
* Skip the last character (in addition to the null terminator), * Skip the last character (in addition to the null terminator),
* because if the last character is a newline, it is considered as part * because if the last character is a newline, it is considered as part
...@@ -58,31 +62,37 @@ static int last_line(const char *buf, size_t len) ...@@ -58,31 +62,37 @@ static int last_line(const char *buf, size_t len)
*/ */
i = len - 2; i = len - 2;
for (; i >= 0; i--) { for (; i > 0; i--) {
if (buf[i] == '\n') if (buf[i] == '\n') {
return i + 1; *out = i + 1;
return true;
} }
return 0; }
return true;
} }
/* /*
* If the given line is of the form * If the given line is of the form
* "<token><optional whitespace><separator>..." or "<separator>...", return the * "<token><optional whitespace><separator>..." or "<separator>...", sets out
* location of the separator. Otherwise, return -1. The optional whitespace * to the location of the separator and returns true. Otherwise, returns
* is allowed there primarily to allow things like "Bug #43" where <token> is * false. The optional whitespace is allowed there primarily to allow things
* "Bug" and <separator> is "#". * like "Bug #43" where <token> is "Bug" and <separator> is "#".
* *
* The separator-starts-line case (in which this function returns 0) is * The separator-starts-line case (in which this function returns true and
* distinguished from the non-well-formed-line case (in which this function * sets out to 0) is distinguished from the non-well-formed-line case (in
* returns -1) because some callers of this function need such a distinction. * which this function returns false) because some callers of this function
* need such a distinction.
*/ */
static int find_separator(const char *line, const char *separators) static bool find_separator(size_t *out, const char *line, const char *separators)
{ {
int whitespace_found = 0; int whitespace_found = 0;
const char *c; const char *c;
for (c = line; *c; c++) { for (c = line; *c; c++) {
if (strchr(separators, *c)) if (strchr(separators, *c)) {
return c - line; *out = c - line;
return true;
}
if (!whitespace_found && (isalnum(*c) || *c == '-')) if (!whitespace_found && (isalnum(*c) || *c == '-'))
continue; continue;
if (c != line && (*c == ' ' || *c == '\t')) { if (c != line && (*c == ' ' || *c == '\t')) {
...@@ -91,7 +101,7 @@ static int find_separator(const char *line, const char *separators) ...@@ -91,7 +101,7 @@ static int find_separator(const char *line, const char *separators)
} }
break; break;
} }
return -1; return false;
} }
/* /*
...@@ -104,10 +114,9 @@ static int find_separator(const char *line, const char *separators) ...@@ -104,10 +114,9 @@ static int find_separator(const char *line, const char *separators)
* Returns the number of bytes from the tail to ignore, to be fed as * Returns the number of bytes from the tail to ignore, to be fed as
* the second parameter to append_signoff(). * the second parameter to append_signoff().
*/ */
static int ignore_non_trailer(const char *buf, size_t len) static size_t ignore_non_trailer(const char *buf, size_t len)
{ {
int boc = 0; size_t boc = 0, bol = 0;
size_t bol = 0;
int in_old_conflicts_block = 0; int in_old_conflicts_block = 0;
size_t cutoff = len; size_t cutoff = len;
...@@ -144,7 +153,7 @@ static int ignore_non_trailer(const char *buf, size_t len) ...@@ -144,7 +153,7 @@ static int ignore_non_trailer(const char *buf, size_t len)
* Return the position of the start of the patch or the length of str if there * Return the position of the start of the patch or the length of str if there
* is no patch in the message. * is no patch in the message.
*/ */
static int find_patch_start(const char *str) static size_t find_patch_start(const char *str)
{ {
const char *s; const char *s;
...@@ -160,10 +169,11 @@ static int find_patch_start(const char *str) ...@@ -160,10 +169,11 @@ static int find_patch_start(const char *str)
* Return the position of the first trailer line or len if there are no * Return the position of the first trailer line or len if there are no
* trailers. * trailers.
*/ */
static int find_trailer_start(const char *buf, size_t len) static size_t find_trailer_start(const char *buf, size_t len)
{ {
const char *s; const char *s;
int end_of_title, l, only_spaces = 1; size_t end_of_title, l;
int only_spaces = 1;
int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0; int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
/* /*
* Number of possible continuation lines encountered. This will be * Number of possible continuation lines encountered. This will be
...@@ -189,12 +199,11 @@ static int find_trailer_start(const char *buf, size_t len) ...@@ -189,12 +199,11 @@ static int find_trailer_start(const char *buf, size_t len)
* trailers, or (ii) contains at least one Git-generated trailer and * trailers, or (ii) contains at least one Git-generated trailer and
* consists of at least 25% trailers. * consists of at least 25% trailers.
*/ */
for (l = last_line(buf, len); l = len;
l >= end_of_title; while (last_line(&l, buf, l) && l >= end_of_title) {
l = last_line(buf, l)) {
const char *bol = buf + l; const char *bol = buf + l;
const char *const *p; const char *const *p;
int separator_pos; size_t separator_pos = 0;
if (bol[0] == COMMENT_LINE_CHAR) { if (bol[0] == COMMENT_LINE_CHAR) {
non_trailer_lines += possible_continuation_lines; non_trailer_lines += possible_continuation_lines;
...@@ -223,7 +232,7 @@ static int find_trailer_start(const char *buf, size_t len) ...@@ -223,7 +232,7 @@ static int find_trailer_start(const char *buf, size_t len)
} }
} }
separator_pos = find_separator(bol, TRAILER_SEPARATORS); find_separator(&separator_pos, bol, TRAILER_SEPARATORS);
if (separator_pos >= 1 && !isspace(bol[0])) { if (separator_pos >= 1 && !isspace(bol[0])) {
trailer_lines++; trailer_lines++;
possible_continuation_lines = 0; possible_continuation_lines = 0;
...@@ -244,7 +253,7 @@ continue_outer_loop: ...@@ -244,7 +253,7 @@ continue_outer_loop:
} }
/* Return the position of the end of the trailers. */ /* Return the position of the end of the trailers. */
static int find_trailer_end(const char *buf, size_t len) static size_t find_trailer_end(const char *buf, size_t len)
{ {
return len - ignore_non_trailer(buf, len); return len - ignore_non_trailer(buf, len);
} }
......
...@@ -501,7 +501,7 @@ static int local_counting(int stage, unsigned int current, unsigned int total, v ...@@ -501,7 +501,7 @@ static int local_counting(int stage, unsigned int current, unsigned int total, v
if (git_buf_oom(&progress_info)) if (git_buf_oom(&progress_info))
return -1; return -1;
error = t->progress_cb(git_buf_cstr(&progress_info), git_buf_len(&progress_info), t->message_cb_payload); error = t->progress_cb(git_buf_cstr(&progress_info), (int)git_buf_len(&progress_info), t->message_cb_payload);
git_buf_dispose(&progress_info); git_buf_dispose(&progress_info);
return error; return error;
...@@ -588,7 +588,7 @@ static int local_download_pack( ...@@ -588,7 +588,7 @@ static int local_download_pack(
goto cleanup; goto cleanup;
if (t->progress_cb && if (t->progress_cb &&
(error = t->progress_cb(git_buf_cstr(&progress_info), git_buf_len(&progress_info), t->message_cb_payload)) < 0) (error = t->progress_cb(git_buf_cstr(&progress_info), (int)git_buf_len(&progress_info), t->message_cb_payload)) < 0)
goto cleanup; goto cleanup;
/* Walk the objects, building a packfile */ /* Walk the objects, building a packfile */
...@@ -602,7 +602,7 @@ static int local_download_pack( ...@@ -602,7 +602,7 @@ static int local_download_pack(
goto cleanup; goto cleanup;
if (t->progress_cb && if (t->progress_cb &&
(error = t->progress_cb(git_buf_cstr(&progress_info), git_buf_len(&progress_info), t->message_cb_payload)) < 0) (error = t->progress_cb(git_buf_cstr(&progress_info), (int)git_buf_len(&progress_info), t->message_cb_payload)) < 0)
goto cleanup; goto cleanup;
if ((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) != 0) if ((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) != 0)
......
...@@ -73,7 +73,7 @@ static int git_smart__set_callbacks( ...@@ -73,7 +73,7 @@ static int git_smart__set_callbacks(
return 0; return 0;
} }
static int http_header_name_length(const char *http_header) static size_t http_header_name_length(const char *http_header)
{ {
const char *colon = strchr(http_header, ':'); const char *colon = strchr(http_header, ':');
if (!colon) if (!colon)
...@@ -84,7 +84,7 @@ static int http_header_name_length(const char *http_header) ...@@ -84,7 +84,7 @@ static int http_header_name_length(const char *http_header)
static bool is_malformed_http_header(const char *http_header) static bool is_malformed_http_header(const char *http_header)
{ {
const char *c; const char *c;
int name_len; size_t name_len;
/* Disallow \r and \n */ /* Disallow \r and \n */
c = strchr(http_header, '\r'); c = strchr(http_header, '\r');
...@@ -114,7 +114,7 @@ static char *forbidden_custom_headers[] = { ...@@ -114,7 +114,7 @@ static char *forbidden_custom_headers[] = {
static bool is_forbidden_custom_header(const char *custom_header) static bool is_forbidden_custom_header(const char *custom_header)
{ {
unsigned long i; unsigned long i;
int name_len = http_header_name_length(custom_header); size_t name_len = http_header_name_length(custom_header);
/* Disallow headers that we set */ /* Disallow headers that we set */
for (i = 0; i < ARRAY_SIZE(forbidden_custom_headers); i++) for (i = 0; i < ARRAY_SIZE(forbidden_custom_headers); i++)
......
...@@ -604,7 +604,14 @@ int git_smart__download_pack( ...@@ -604,7 +604,14 @@ int git_smart__download_pack(
} else if (pkt->type == GIT_PKT_PROGRESS) { } else if (pkt->type == GIT_PKT_PROGRESS) {
if (t->progress_cb) { if (t->progress_cb) {
git_pkt_progress *p = (git_pkt_progress *) pkt; git_pkt_progress *p = (git_pkt_progress *) pkt;
error = t->progress_cb(p->data, p->len, t->message_cb_payload);
if (p->len > INT_MAX) {
git_error_set(GIT_ERROR_NET, "oversized progress message");
error = GIT_ERROR;
goto done;
}
error = t->progress_cb(p->data, (int)p->len, t->message_cb_payload);
} }
} else if (pkt->type == GIT_PKT_DATA) { } else if (pkt->type == GIT_PKT_DATA) {
git_pkt_data *p = (git_pkt_data *) pkt; git_pkt_data *p = (git_pkt_data *) pkt;
...@@ -839,7 +846,14 @@ static int parse_report(transport_smart *transport, git_push *push) ...@@ -839,7 +846,14 @@ static int parse_report(transport_smart *transport, git_push *push)
case GIT_PKT_PROGRESS: case GIT_PKT_PROGRESS:
if (transport->progress_cb) { if (transport->progress_cb) {
git_pkt_progress *p = (git_pkt_progress *) pkt; git_pkt_progress *p = (git_pkt_progress *) pkt;
error = transport->progress_cb(p->data, p->len, transport->message_cb_payload);
if (p->len > INT_MAX) {
git_error_set(GIT_ERROR_NET, "oversized progress message");
error = GIT_ERROR;
goto done;
}
error = transport->progress_cb(p->data, (int)p->len, transport->message_cb_payload);
} }
break; break;
default: default:
......
...@@ -48,6 +48,10 @@ ...@@ -48,6 +48,10 @@
# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800
#endif #endif
#ifndef DWORD_MAX
# define DWORD_MAX 0xffffffff
#endif
static const char *prefix_https = "https://"; static const char *prefix_https = "https://";
static const char *upload_pack_service = "upload-pack"; static const char *upload_pack_service = "upload-pack";
static const char *upload_pack_ls_service_url = "/info/refs?service=git-upload-pack"; static const char *upload_pack_ls_service_url = "/info/refs?service=git-upload-pack";
...@@ -339,6 +343,8 @@ static int apply_credentials( ...@@ -339,6 +343,8 @@ static int apply_credentials(
{ {
int error = 0; int error = 0;
GIT_UNUSED(url);
/* If we have creds, just apply them */ /* If we have creds, just apply them */
if (creds && creds->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT) if (creds && creds->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT)
error = apply_userpass_credentials(request, target, mechanisms, creds); error = apply_userpass_credentials(request, target, mechanisms, creds);
...@@ -686,6 +692,10 @@ static void CALLBACK winhttp_status( ...@@ -686,6 +692,10 @@ static void CALLBACK winhttp_status(
{ {
DWORD status; DWORD status;
GIT_UNUSED(connection);
GIT_UNUSED(ctx);
GIT_UNUSED(info_len);
if (code != WINHTTP_CALLBACK_STATUS_SECURE_FAILURE) if (code != WINHTTP_CALLBACK_STATUS_SECURE_FAILURE)
return; return;
...@@ -816,6 +826,11 @@ static int do_send_request(winhttp_stream *s, size_t len, int ignore_length) ...@@ -816,6 +826,11 @@ static int do_send_request(winhttp_stream *s, size_t len, int ignore_length)
int attempts; int attempts;
bool success; bool success;
if (len > DWORD_MAX) {
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return -1;
}
for (attempts = 0; attempts < 5; attempts++) { for (attempts = 0; attempts < 5; attempts++) {
if (ignore_length) { if (ignore_length) {
success = WinHttpSendRequest(s->request, success = WinHttpSendRequest(s->request,
...@@ -826,7 +841,7 @@ static int do_send_request(winhttp_stream *s, size_t len, int ignore_length) ...@@ -826,7 +841,7 @@ static int do_send_request(winhttp_stream *s, size_t len, int ignore_length)
success = WinHttpSendRequest(s->request, success = WinHttpSendRequest(s->request,
WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0, WINHTTP_NO_REQUEST_DATA, 0,
len, 0); (DWORD)len, 0);
} }
if (success || GetLastError() != (DWORD)SEC_E_BUFFER_TOO_SMALL) if (success || GetLastError() != (DWORD)SEC_E_BUFFER_TOO_SMALL)
......
...@@ -120,12 +120,14 @@ static int read_tree_internal(git_tree_cache **out, ...@@ -120,12 +120,14 @@ static int read_tree_internal(git_tree_cache **out,
/* Parse children: */ /* Parse children: */
if (tree->children_count > 0) { if (tree->children_count > 0) {
unsigned int i; size_t i, bufsize;
tree->children = git_pool_malloc(pool, tree->children_count * sizeof(git_tree_cache *)); GIT_ERROR_CHECK_ALLOC_MULTIPLY(&bufsize, tree->children_count, sizeof(git_tree_cache*));
tree->children = git_pool_malloc(pool, bufsize);
GIT_ERROR_CHECK_ALLOC(tree->children); GIT_ERROR_CHECK_ALLOC(tree->children);
memset(tree->children, 0x0, tree->children_count * sizeof(git_tree_cache *)); memset(tree->children, 0x0, bufsize);
for (i = 0; i < tree->children_count; ++i) { for (i = 0; i < tree->children_count; ++i) {
if (read_tree_internal(&tree->children[i], &buffer, buffer_end, pool) < 0) if (read_tree_internal(&tree->children[i], &buffer, buffer_end, pool) < 0)
...@@ -160,7 +162,7 @@ int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer ...@@ -160,7 +162,7 @@ int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer
static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_pool *pool) static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_pool *pool)
{ {
git_repository *repo; git_repository *repo;
size_t i, j, nentries, ntrees; size_t i, j, nentries, ntrees, alloc_size;
int error; int error;
repo = git_tree_owner(tree); repo = git_tree_owner(tree);
...@@ -182,8 +184,10 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_ ...@@ -182,8 +184,10 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_
ntrees++; ntrees++;
} }
GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_size, ntrees, sizeof(git_tree_cache *));
cache->children_count = ntrees; cache->children_count = ntrees;
cache->children = git_pool_mallocz(pool, ntrees * sizeof(git_tree_cache *)); cache->children = git_pool_mallocz(pool, alloc_size);
GIT_ERROR_CHECK_ALLOC(cache->children); GIT_ERROR_CHECK_ALLOC(cache->children);
j = 0; j = 0;
...@@ -232,11 +236,14 @@ int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_poo ...@@ -232,11 +236,14 @@ int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_poo
int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool) int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool)
{ {
size_t name_len; size_t name_len, alloc_size;
git_tree_cache *tree; git_tree_cache *tree;
name_len = strlen(name); name_len = strlen(name);
tree = git_pool_malloc(pool, sizeof(git_tree_cache) + name_len + 1);
GIT_ERROR_CHECK_ALLOC_ADD3(&alloc_size, sizeof(git_tree_cache), name_len, 1);
tree = git_pool_malloc(pool, alloc_size);
GIT_ERROR_CHECK_ALLOC(tree); GIT_ERROR_CHECK_ALLOC(tree);
memset(tree, 0x0, sizeof(git_tree_cache)); memset(tree, 0x0, sizeof(git_tree_cache));
......
...@@ -340,7 +340,7 @@ size_t git_tree_entrycount(const git_tree *tree) ...@@ -340,7 +340,7 @@ size_t git_tree_entrycount(const git_tree *tree)
return tree->entries.size; return tree->entries.size;
} }
unsigned int git_treebuilder_entrycount(git_treebuilder *bld) size_t git_treebuilder_entrycount(git_treebuilder *bld)
{ {
assert(bld); assert(bld);
......
...@@ -192,7 +192,7 @@ int git__strntol32(int32_t *result, const char *nptr, size_t nptr_len, const cha ...@@ -192,7 +192,7 @@ int git__strntol32(int32_t *result, const char *nptr, size_t nptr_len, const cha
tmp_int = tmp_long & 0xFFFFFFFF; tmp_int = tmp_long & 0xFFFFFFFF;
if (tmp_int != tmp_long) { if (tmp_int != tmp_long) {
int len = tmp_endptr - nptr; int len = (int)(tmp_endptr - nptr);
git_error_set(GIT_ERROR_INVALID, "failed to convert: '%.*s' is too large", len, nptr); git_error_set(GIT_ERROR_INVALID, "failed to convert: '%.*s' is too large", len, nptr);
return -1; return -1;
} }
...@@ -802,23 +802,23 @@ static const int8_t utf8proc_utf8class[256] = { ...@@ -802,23 +802,23 @@ static const int8_t utf8proc_utf8class[256] = {
4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0
}; };
int git__utf8_charlen(const uint8_t *str, int str_len) int git__utf8_charlen(const uint8_t *str, size_t str_len)
{ {
int length, i; size_t length, i;
length = utf8proc_utf8class[str[0]]; length = utf8proc_utf8class[str[0]];
if (!length) if (!length)
return -1; return -1;
if (str_len >= 0 && length > str_len) if (str_len > 0 && length > str_len)
return -str_len; return -1;
for (i = 1; i < length; i++) { for (i = 1; i < length; i++) {
if ((str[i] & 0xC0) != 0x80) if ((str[i] & 0xC0) != 0x80)
return -i; return -1;
} }
return length; return (int)length;
} }
int git__utf8_iterate(const uint8_t *str, int str_len, int32_t *dst) int git__utf8_iterate(const uint8_t *str, int str_len, int32_t *dst)
......
...@@ -250,7 +250,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags) ...@@ -250,7 +250,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/ for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
if (!p_ch) if (!p_ch)
return WM_ABORT_ALL; return WM_ABORT_ALL;
i = p - s - 1; i = (int)(p - s - 1);
if (i < 0 || p[-1] != ':') { if (i < 0 || p[-1] != ':') {
/* Didn't find ":]", so treat like a normal set. */ /* Didn't find ":]", so treat like a normal set. */
p = s - 2; p = s - 2;
......
...@@ -140,14 +140,24 @@ int git_win32_path_canonicalize(git_win32_path path) ...@@ -140,14 +140,24 @@ int git_win32_path_canonicalize(git_win32_path path)
*to = L'\0'; *to = L'\0';
return (to - path); if ((to - path) > INT_MAX) {
SetLastError(ERROR_FILENAME_EXCED_RANGE);
return -1;
}
return (int)(to - path);
} }
int git_win32_path__cwd(wchar_t *out, size_t len) int git_win32_path__cwd(wchar_t *out, size_t len)
{ {
int cwd_len; int cwd_len;
if ((cwd_len = path__cwd(out, len)) < 0) if (len > INT_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if ((cwd_len = path__cwd(out, (int)len)) < 0)
return -1; return -1;
/* UNC paths */ /* UNC paths */
......
...@@ -42,15 +42,15 @@ int git_threads_init(void) ...@@ -42,15 +42,15 @@ int git_threads_init(void)
HMODULE hModule = GetModuleHandleW(L"kernel32"); HMODULE hModule = GetModuleHandleW(L"kernel32");
if (hModule) { if (hModule) {
win32_srwlock_initialize = (win32_srwlock_fn) win32_srwlock_initialize = (win32_srwlock_fn)(void *)
GetProcAddress(hModule, "InitializeSRWLock"); GetProcAddress(hModule, "InitializeSRWLock");
win32_srwlock_acquire_shared = (win32_srwlock_fn) win32_srwlock_acquire_shared = (win32_srwlock_fn)(void *)
GetProcAddress(hModule, "AcquireSRWLockShared"); GetProcAddress(hModule, "AcquireSRWLockShared");
win32_srwlock_release_shared = (win32_srwlock_fn) win32_srwlock_release_shared = (win32_srwlock_fn)(void *)
GetProcAddress(hModule, "ReleaseSRWLockShared"); GetProcAddress(hModule, "ReleaseSRWLockShared");
win32_srwlock_acquire_exclusive = (win32_srwlock_fn) win32_srwlock_acquire_exclusive = (win32_srwlock_fn)(void *)
GetProcAddress(hModule, "AcquireSRWLockExclusive"); GetProcAddress(hModule, "AcquireSRWLockExclusive");
win32_srwlock_release_exclusive = (win32_srwlock_fn) win32_srwlock_release_exclusive = (win32_srwlock_fn)(void *)
GetProcAddress(hModule, "ReleaseSRWLockExclusive"); GetProcAddress(hModule, "ReleaseSRWLockExclusive");
} }
......
...@@ -25,13 +25,17 @@ int git_buf_put_w(git_buf *buf, const wchar_t *string_w, size_t len_w) ...@@ -25,13 +25,17 @@ int git_buf_put_w(git_buf *buf, const wchar_t *string_w, size_t len_w)
int utf8_len, utf8_write_len; int utf8_len, utf8_write_len;
size_t new_size; size_t new_size;
if (!len_w) if (!len_w) {
return 0; return 0;
} else if (len_w > INT_MAX) {
git_error_set_oom();
return -1;
}
assert(string_w); assert(string_w);
/* Measure the string necessary for conversion */ /* Measure the string necessary for conversion */
if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string_w, len_w, NULL, 0, NULL, NULL)) == 0) if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string_w, (int)len_w, NULL, 0, NULL, NULL)) == 0)
return 0; return 0;
assert(utf8_len > 0); assert(utf8_len > 0);
...@@ -43,7 +47,7 @@ int git_buf_put_w(git_buf *buf, const wchar_t *string_w, size_t len_w) ...@@ -43,7 +47,7 @@ int git_buf_put_w(git_buf *buf, const wchar_t *string_w, size_t len_w)
return -1; return -1;
if ((utf8_write_len = WideCharToMultiByte( if ((utf8_write_len = WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, string_w, len_w, &buf->ptr[buf->size], utf8_len, NULL, NULL)) == 0) CP_UTF8, WC_ERR_INVALID_CHARS, string_w, (int)len_w, &buf->ptr[buf->size], utf8_len, NULL, NULL)) == 0)
return handle_wc_error(); return handle_wc_error();
assert(utf8_write_len == utf8_len); assert(utf8_write_len == utf8_len);
......
...@@ -13,11 +13,6 @@ ...@@ -13,11 +13,6 @@
#include "win32/posix.h" #include "win32/posix.h"
#include "hash.h" #include "hash.h"
/**
* This is supposedly defined in WinBase.h (from Windows.h) but there were linker issues.
*/
USHORT WINAPI RtlCaptureStackBackTrace(ULONG, ULONG, PVOID*, PULONG);
static bool g_win32_stack_initialized = false; static bool g_win32_stack_initialized = false;
static HANDLE g_win32_stack_process = INVALID_HANDLE_VALUE; static HANDLE g_win32_stack_process = INVALID_HANDLE_VALUE;
static git_win32__stack__aux_cb_alloc g_aux_cb_alloc = NULL; static git_win32__stack__aux_cb_alloc g_aux_cb_alloc = NULL;
......
...@@ -93,3 +93,34 @@ int git_win32__hidden(bool *out, const char *path) ...@@ -93,3 +93,34 @@ int git_win32__hidden(bool *out, const char *path)
*out = (attrs & FILE_ATTRIBUTE_HIDDEN) ? true : false; *out = (attrs & FILE_ATTRIBUTE_HIDDEN) ? true : false;
return 0; return 0;
} }
int git_win32__file_attribute_to_stat(
struct stat *st,
const WIN32_FILE_ATTRIBUTE_DATA *attrdata,
const wchar_t *path)
{
git_win32__stat_init(st,
attrdata->dwFileAttributes,
attrdata->nFileSizeHigh,
attrdata->nFileSizeLow,
attrdata->ftCreationTime,
attrdata->ftLastAccessTime,
attrdata->ftLastWriteTime);
if (attrdata->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && path) {
git_win32_path target;
if (git_win32_path_readlink_w(target, path) >= 0) {
st->st_mode = (st->st_mode & ~S_IFMT) | S_IFLNK;
/* st_size gets the UTF-8 length of the target name, in bytes,
* not counting the NULL terminator */
if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) {
git_error_set(GIT_ERROR_OS, "could not convert reparse point name for '%ls'", path);
return -1;
}
}
}
return 0;
}
...@@ -59,6 +59,11 @@ extern int git_win32__set_hidden(const char *path, bool hidden); ...@@ -59,6 +59,11 @@ extern int git_win32__set_hidden(const char *path, bool hidden);
*/ */
extern int git_win32__hidden(bool *hidden, const char *path); extern int git_win32__hidden(bool *hidden, const char *path);
extern int git_win32__file_attribute_to_stat(
struct stat *st,
const WIN32_FILE_ATTRIBUTE_DATA *attrdata,
const wchar_t *path);
/** /**
* Converts a FILETIME structure to a struct timespec. * Converts a FILETIME structure to a struct timespec.
* *
...@@ -136,35 +141,4 @@ GIT_INLINE(void) git_win32__file_information_to_stat( ...@@ -136,35 +141,4 @@ GIT_INLINE(void) git_win32__file_information_to_stat(
fileinfo->ftLastWriteTime); fileinfo->ftLastWriteTime);
} }
GIT_INLINE(int) git_win32__file_attribute_to_stat(
struct stat *st,
const WIN32_FILE_ATTRIBUTE_DATA *attrdata,
const wchar_t *path)
{
git_win32__stat_init(st,
attrdata->dwFileAttributes,
attrdata->nFileSizeHigh,
attrdata->nFileSizeLow,
attrdata->ftCreationTime,
attrdata->ftLastAccessTime,
attrdata->ftLastWriteTime);
if (attrdata->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && path) {
git_win32_path target;
if (git_win32_path_readlink_w(target, path) >= 0) {
st->st_mode = (st->st_mode & ~S_IFMT) | S_IFLNK;
/* st_size gets the UTF-8 length of the target name, in bytes,
* not counting the NULL terminator */
if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) {
git_error_set(GIT_ERROR_OS, "could not convert reparse point name for '%ls'", path);
return -1;
}
}
}
return 0;
}
#endif #endif
...@@ -34,7 +34,7 @@ int git_worktree_list(git_strarray *wts, git_repository *repo) ...@@ -34,7 +34,7 @@ int git_worktree_list(git_strarray *wts, git_repository *repo)
git_vector worktrees = GIT_VECTOR_INIT; git_vector worktrees = GIT_VECTOR_INIT;
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
char *worktree; char *worktree;
unsigned i, len; size_t i, len;
int error; int error;
assert(wts && repo); assert(wts && repo);
......
...@@ -67,7 +67,7 @@ ...@@ -67,7 +67,7 @@
# define PRIxZ "Ix" # define PRIxZ "Ix"
# endif # endif
# ifdef _MSC_VER # if defined(_MSC_VER) || defined(__MINGW32__)
typedef struct stat STAT_T; typedef struct stat STAT_T;
# else # else
typedef struct _stat STAT_T; typedef struct _stat STAT_T;
......
...@@ -54,7 +54,7 @@ static void do_symlink(const char *old, const char *new, int is_dir) ...@@ -54,7 +54,7 @@ static void do_symlink(const char *old, const char *new, int is_dir)
create_symlink_func pCreateSymbolicLink; create_symlink_func pCreateSymbolicLink;
cl_assert(module = GetModuleHandle("kernel32")); cl_assert(module = GetModuleHandle("kernel32"));
cl_assert(pCreateSymbolicLink = (create_symlink_func)GetProcAddress(module, "CreateSymbolicLinkA")); cl_assert(pCreateSymbolicLink = (create_symlink_func)(void *)GetProcAddress(module, "CreateSymbolicLinkA"));
cl_win32_pass(pCreateSymbolicLink(new, old, is_dir)); cl_win32_pass(pCreateSymbolicLink(new, old, is_dir));
#endif #endif
...@@ -70,7 +70,7 @@ static void do_hardlink(const char *old, const char *new) ...@@ -70,7 +70,7 @@ static void do_hardlink(const char *old, const char *new)
create_hardlink_func pCreateHardLink; create_hardlink_func pCreateHardLink;
cl_assert(module = GetModuleHandle("kernel32")); cl_assert(module = GetModuleHandle("kernel32"));
cl_assert(pCreateHardLink = (create_hardlink_func)GetProcAddress(module, "CreateHardLinkA")); cl_assert(pCreateHardLink = (create_hardlink_func)(void *)GetProcAddress(module, "CreateHardLinkA"));
cl_win32_pass(pCreateHardLink(new, old, 0)); cl_win32_pass(pCreateHardLink(new, old, 0));
#endif #endif
......
...@@ -75,8 +75,9 @@ void test_object_tree_read__two(void) ...@@ -75,8 +75,9 @@ void test_object_tree_read__two(void)
} }
#define BIGFILE "bigfile" #define BIGFILE "bigfile"
#ifdef GIT_ARCH_64 #ifdef GIT_ARCH_64
# define BIGFILE_SIZE (size_t)4 * 1024 * 1024 * 1024 /* 4 GiB */ #define BIGFILE_SIZE (off_t)4294967296
#else #else
# define BIGFILE_SIZE SIZE_MAX # define BIGFILE_SIZE SIZE_MAX
#endif #endif
......
...@@ -35,10 +35,10 @@ static void fetchhead_test_clone(void) ...@@ -35,10 +35,10 @@ static void fetchhead_test_clone(void)
cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options)); cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
} }
static int count_references(void) static size_t count_references(void)
{ {
git_strarray array; git_strarray array;
int refs; size_t refs;
cl_git_pass(git_reference_list(&array, g_repo)); cl_git_pass(git_reference_list(&array, g_repo));
refs = array.count; refs = array.count;
...@@ -118,7 +118,7 @@ void test_online_fetchhead__no_merges(void) ...@@ -118,7 +118,7 @@ void test_online_fetchhead__no_merges(void)
void test_online_fetchhead__explicit_dst_refspec_creates_branch(void) void test_online_fetchhead__explicit_dst_refspec_creates_branch(void)
{ {
git_reference *ref; git_reference *ref;
int refs; size_t refs;
fetchhead_test_clone(); fetchhead_test_clone();
refs = count_references(); refs = count_references();
...@@ -133,7 +133,7 @@ void test_online_fetchhead__explicit_dst_refspec_creates_branch(void) ...@@ -133,7 +133,7 @@ void test_online_fetchhead__explicit_dst_refspec_creates_branch(void)
void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void) void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void)
{ {
git_reference *ref; git_reference *ref;
int refs; size_t refs;
fetchhead_test_clone(); fetchhead_test_clone();
refs = count_references(); refs = count_references();
...@@ -146,7 +146,7 @@ void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void) ...@@ -146,7 +146,7 @@ void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void)
void test_online_fetchhead__colon_only_dst_refspec_creates_no_branch(void) void test_online_fetchhead__colon_only_dst_refspec_creates_no_branch(void)
{ {
int refs; size_t refs;
fetchhead_test_clone(); fetchhead_test_clone();
refs = count_references(); refs = count_references();
......
...@@ -42,7 +42,7 @@ void test_worktree_worktree__list_with_invalid_worktree_dirs(void) ...@@ -42,7 +42,7 @@ void test_worktree_worktree__list_with_invalid_worktree_dirs(void)
}; };
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
git_strarray wts; git_strarray wts;
unsigned i, j, len; size_t i, j, len;
cl_git_pass(git_buf_printf(&path, "%s/worktrees/invalid", cl_git_pass(git_buf_printf(&path, "%s/worktrees/invalid",
fixture.repo->commondir)); fixture.repo->commondir));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment