{"thread":{"id":"7127","subject":"[PATCH 9/9] Cast 64 bit off_t to 32 bit size_t","startedAt":"2007-03-07T01:44:37Z","lastAt":"2007-03-07T01:44:37Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":9},"messages":[{"id":"36517","messageId":"20070307014437.GJ26482@spearce.org","threadId":"7127","inReplyTo":null,"subject":"[PATCH 9/9] Cast 64 bit off_t to 32 bit size_t","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-03-07T01:44:37Z","receivedAt":"2007-03-07T01:44:37Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Some systems have sizeof(off_t) == 8 while sizeof(size_t) == 4.\nThis implies that we are able to access and work on files whose\nmaximum length is around 2^63-1 bytes, but we can only malloc or\nmmap somewhat less than 2^32-1 bytes of memory.\n\nOn such a system an implicit conversion of off_t to size_t can cause\nthe size_t to wrap, resulting in unexpected and exciting behavior.\nRight now we are working around all gcc warnings generated by the\n-Wshorten-64-to-32 option by passing the off_t through xsize_t().\n\nIn the future we should make xsize_t on such problematic platforms\ndetect the wrapping and die if such a file is accessed.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n builtin-apply.c         |    2 +-\n builtin-blame.c         |    2 +-\n builtin-count-objects.c |    2 +-\n builtin-grep.c          |    9 ++++++---\n combine-diff.c          |    4 ++--\n config.c                |   28 +++++++++++++++-------------\n diff.c                  |    9 +++++----\n diffcore-break.c        |    2 +-\n diffcore-order.c        |    6 ++++--\n diffcore-rename.c       |    7 ++++---\n dir.c                   |    4 ++--\n git-compat-util.h       |    5 +++++\n read-cache.c            |    6 +++---\n refs.c                  |    8 +++++---\n sha1_file.c             |   40 +++++++++++++++++++++++-----------------\n xdiff-interface.c       |    8 +++++---\n 16 files changed, 83 insertions(+), 59 deletions(-)\n\ndiff --git a/builtin-apply.c b/builtin-apply.c\nindex 5393510..dfa1716 100644\n--- a/builtin-apply.c\n+++ b/builtin-apply.c\n@@ -1981,7 +1981,7 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *\n \t\t}\n \t}\n \telse if (patch->old_name) {\n-\t\tsize = st->st_size;\n+\t\tsize = xsize_t(st->st_size);\n \t\talloc = size + 8192;\n \t\tbuf = xmalloc(alloc);\n \t\tif (read_old_data(st, patch->old_name, &buf, &alloc, &size))\ndiff --git a/builtin-blame.c b/builtin-blame.c\nindex 20966b9..b51cdc7 100644\n--- a/builtin-blame.c\n+++ b/builtin-blame.c\n@@ -1963,7 +1963,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con\n \t\t\t\tdie(\"Cannot lstat %s\", path);\n \t\t\tread_from = path;\n \t\t}\n-\t\tfin_size = st.st_size;\n+\t\tfin_size = xsize_t(st.st_size);\n \t\tbuf = xmalloc(fin_size+1);\n \t\tmode = canon_mode(st.st_mode);\n \t\tswitch (st.st_mode & S_IFMT) {\ndiff --git a/builtin-count-objects.c b/builtin-count-objects.c\nindex f5b22bb..6263d8a 100644\n--- a/builtin-count-objects.c\n+++ b/builtin-count-objects.c\n@@ -44,7 +44,7 @@ static void count_objects(DIR *d, char *path, int len, int verbose,\n \t\t\tif (lstat(path, &st) || !S_ISREG(st.st_mode))\n \t\t\t\tbad = 1;\n \t\t\telse\n-\t\t\t\t(*loose_size) += st.st_blocks;\n+\t\t\t\t(*loose_size) += xsize_t(st.st_blocks);\n \t\t}\n \t\tif (bad) {\n \t\t\tif (verbose) {\ndiff --git a/builtin-grep.c b/builtin-grep.c\nindex e4f06f2..694da5b 100644\n--- a/builtin-grep.c\n+++ b/builtin-grep.c\n@@ -122,6 +122,8 @@ static int grep_file(struct grep_opt *opt, const char *filename)\n \tstruct stat st;\n \tint i;\n \tchar *data;\n+\tsize_t sz;\n+\n \tif (lstat(filename, &st) < 0) {\n \terr_ret:\n \t\tif (errno != ENOENT)\n@@ -132,11 +134,12 @@ static int grep_file(struct grep_opt *opt, const char *filename)\n \t\treturn 0; /* empty file -- no grep hit */\n \tif (!S_ISREG(st.st_mode))\n \t\treturn 0;\n+\tsz = xsize_t(st.st_size);\n \ti = open(filename, O_RDONLY);\n \tif (i < 0)\n \t\tgoto err_ret;\n-\tdata = xmalloc(st.st_size + 1);\n-\tif (st.st_size != read_in_full(i, data, st.st_size)) {\n+\tdata = xmalloc(sz + 1);\n+\tif (st.st_size != read_in_full(i, data, sz)) {\n \t\terror(\"'%s': short read %s\", filename, strerror(errno));\n \t\tclose(i);\n \t\tfree(data);\n@@ -145,7 +148,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)\n \tclose(i);\n \tif (opt->relative && opt->prefix_length)\n \t\tfilename += opt->prefix_length;\n-\ti = grep_buffer(opt, filename, data, st.st_size);\n+\ti = grep_buffer(opt, filename, data, sz);\n \tfree(data);\n \treturn i;\n }\ndiff --git a/combine-diff.c b/combine-diff.c\nindex 6d928f2..3a9b32f 100644\n--- a/combine-diff.c\n+++ b/combine-diff.c\n@@ -684,7 +684,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,\n \t\t\tgoto deleted_file;\n \n \t\tif (S_ISLNK(st.st_mode)) {\n-\t\t\tsize_t len = st.st_size;\n+\t\t\tsize_t len = xsize_t(st.st_size);\n \t\t\tresult_size = len;\n \t\t\tresult = xmalloc(len + 1);\n \t\t\tif (result_size != readlink(elem->path, result, len)) {\n@@ -697,7 +697,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,\n \t\t}\n \t\telse if (0 <= (fd = open(elem->path, O_RDONLY)) &&\n \t\t\t !fstat(fd, &st)) {\n-\t\t\tsize_t len = st.st_size;\n+\t\t\tsize_t len = xsize_t(st.st_size);\n \t\t\tsize_t sz = 0;\n \t\t\tint is_file, i;\n \ndiff --git a/config.c b/config.c\nindex 7ac3947..8fc4f11 100644\n--- a/config.c\n+++ b/config.c\n@@ -431,7 +431,7 @@ static struct {\n \tint do_not_match;\n \tregex_t* value_regex;\n \tint multi_replace;\n-\toff_t offset[MAX_MATCHES];\n+\tsize_t offset[MAX_MATCHES];\n \tenum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;\n \tint seen;\n } store;\n@@ -579,11 +579,11 @@ static int store_write_pair(int fd, const char* key, const char* value)\n \treturn 1;\n }\n \n-static int find_beginning_of_line(const char* contents, int size,\n-\tint offset_, int* found_bracket)\n+static ssize_t find_beginning_of_line(const char* contents, size_t size,\n+\tsize_t offset_, int* found_bracket)\n {\n-\tint equal_offset = size, bracket_offset = size;\n-\tint offset;\n+\tsize_t equal_offset = size, bracket_offset = size;\n+\tssize_t offset;\n \n \tfor (offset = offset_-2; offset > 0 \n \t\t\t&& contents[offset] != '\\n'; offset--)\n@@ -727,7 +727,8 @@ int git_config_set_multivar(const char* key, const char* value,\n \t} else {\n \t\tstruct stat st;\n \t\tchar* contents;\n-\t\tint i, copy_begin, copy_end, new_line = 0;\n+\t\tsize_t contents_sz, copy_begin, copy_end;\n+\t\tint i, new_line = 0;\n \n \t\tif (value_regex == NULL)\n \t\t\tstore.value_regex = NULL;\n@@ -784,7 +785,8 @@ int git_config_set_multivar(const char* key, const char* value,\n \t\t}\n \n \t\tfstat(in_fd, &st);\n-\t\tcontents = xmmap(NULL, st.st_size, PROT_READ,\n+\t\tcontents_sz = xsize_t(st.st_size);\n+\t\tcontents = xmmap(NULL, contents_sz, PROT_READ,\n \t\t\tMAP_PRIVATE, in_fd, 0);\n \t\tclose(in_fd);\n \n@@ -793,12 +795,12 @@ int git_config_set_multivar(const char* key, const char* value,\n \n \t\tfor (i = 0, copy_begin = 0; i < store.seen; i++) {\n \t\t\tif (store.offset[i] == 0) {\n-\t\t\t\tstore.offset[i] = copy_end = st.st_size;\n+\t\t\t\tstore.offset[i] = copy_end = contents_sz;\n \t\t\t} else if (store.state != KEY_SEEN) {\n \t\t\t\tcopy_end = store.offset[i];\n \t\t\t} else\n \t\t\t\tcopy_end = find_beginning_of_line(\n-\t\t\t\t\tcontents, st.st_size,\n+\t\t\t\t\tcontents, contents_sz,\n \t\t\t\t\tstore.offset[i]-2, &new_line);\n \n \t\t\t/* write the first part of the config */\n@@ -825,13 +827,13 @@ int git_config_set_multivar(const char* key, const char* value,\n \t\t}\n \n \t\t/* write the rest of the config */\n-\t\tif (copy_begin < st.st_size)\n+\t\tif (copy_begin < contents_sz)\n \t\t\tif (write_in_full(fd, contents + copy_begin,\n-\t\t\t\t\t  st.st_size - copy_begin) <\n-\t\t\t    st.st_size - copy_begin)\n+\t\t\t\t\t  contents_sz - copy_begin) <\n+\t\t\t    contents_sz - copy_begin)\n \t\t\t\tgoto write_err_out;\n \n-\t\tmunmap(contents, st.st_size);\n+\t\tmunmap(contents, contents_sz);\n \t\tunlink(config_filename);\n \t}\n \ndiff --git a/diff.c b/diff.c\nindex e225de2..8f7a7d1 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -1399,7 +1399,7 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)\n \t\t\t\treturn err;\n \t\t\t}\n \t\t}\n-\t\ts->size = st.st_size;\n+\t\ts->size = xsize_t(st.st_size);\n \t\tif (!s->size)\n \t\t\tgoto empty;\n \t\tif (size_only)\n@@ -1515,12 +1515,13 @@ static void prepare_temp_file(const char *name,\n \t\tif (S_ISLNK(st.st_mode)) {\n \t\t\tint ret;\n \t\t\tchar buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */\n+\t\t\tsize_t sz = xsize_t(st.st_size);\n \t\t\tif (sizeof(buf) <= st.st_size)\n \t\t\t\tdie(\"symlink too long: %s\", name);\n-\t\t\tret = readlink(name, buf, st.st_size);\n+\t\t\tret = readlink(name, buf, sz);\n \t\t\tif (ret < 0)\n \t\t\t\tdie(\"readlink(%s)\", name);\n-\t\t\tprep_temp_blob(temp, buf, st.st_size,\n+\t\t\tprep_temp_blob(temp, buf, sz,\n \t\t\t\t       (one->sha1_valid ?\n \t\t\t\t\tone->sha1 : null_sha1),\n \t\t\t\t       (one->sha1_valid ?\n@@ -2138,7 +2139,7 @@ static int parse_num(const char **cp_p)\n \t/* user says num divided by scale and we say internally that\n \t * is MAX_SCORE * num / scale.\n \t */\n-\treturn (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);\n+\treturn (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));\n }\n \n int diff_scoreopt_parse(const char *opt)\ndiff --git a/diffcore-break.c b/diffcore-break.c\nindex acb18db..9c19b8c 100644\n--- a/diffcore-break.c\n+++ b/diffcore-break.c\n@@ -89,7 +89,7 @@ static int should_break(struct diff_filespec *src,\n \t * merge the surviving pair together if the score is\n \t * less than the minimum, after rename/copy runs.\n \t */\n-\t*merge_score_p = src_removed * MAX_SCORE / src->size;\n+\t*merge_score_p = (int)(src_removed * MAX_SCORE / src->size);\n \n \t/* Extent of damage, which counts both inserts and\n \t * deletes.\ndiff --git a/diffcore-order.c b/diffcore-order.c\nindex 7ad0946..2a4bd82 100644\n--- a/diffcore-order.c\n+++ b/diffcore-order.c\n@@ -14,6 +14,7 @@ static void prepare_order(const char *orderfile)\n \tvoid *map;\n \tchar *cp, *endp;\n \tstruct stat st;\n+\tsize_t sz;\n \n \tif (order)\n \t\treturn;\n@@ -25,11 +26,12 @@ static void prepare_order(const char *orderfile)\n \t\tclose(fd);\n \t\treturn;\n \t}\n-\tmap = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);\n+\tsz = xsize_t(st.st_size);\n+\tmap = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);\n \tclose(fd);\n \tif (map == MAP_FAILED)\n \t\treturn;\n-\tendp = (char *) map + st.st_size;\n+\tendp = (char *) map + sz;\n \tfor (pass = 0; pass < 2; pass++) {\n \t\tcnt = 0;\n \t\tcp = map;\ndiff --git a/diffcore-rename.c b/diffcore-rename.c\nindex 91fa2be..7903041 100644\n--- a/diffcore-rename.c\n+++ b/diffcore-rename.c\n@@ -172,7 +172,8 @@ static int estimate_similarity(struct diff_filespec *src,\n \t\treturn 0; /* error but caught downstream */\n \n \n-\tdelta_limit = base_size * (MAX_SCORE-minimum_score) / MAX_SCORE;\n+\tdelta_limit = (unsigned long)\n+\t\t(base_size * (MAX_SCORE-minimum_score) / MAX_SCORE);\n \tif (diffcore_count_changes(src->data, src->size,\n \t\t\t\t   dst->data, dst->size,\n \t\t\t\t   &src->cnt_data, &dst->cnt_data,\n@@ -186,7 +187,7 @@ static int estimate_similarity(struct diff_filespec *src,\n \tif (!dst->size)\n \t\tscore = 0; /* should not happen */\n \telse\n-\t\tscore = src_copied * MAX_SCORE / max_size;\n+\t\tscore = (int)(src_copied * MAX_SCORE / max_size);\n \treturn score;\n }\n \n@@ -297,7 +298,7 @@ void diffcore_rename(struct diff_options *options)\n \t\t\t\tstruct diff_filespec *one = rename_src[j].one;\n \t\t\t\tif (!is_exact_match(one, two, contents_too))\n \t\t\t\t\tcontinue;\n-\t\t\t\trecord_rename_pair(i, j, MAX_SCORE);\n+\t\t\t\trecord_rename_pair(i, j, (int)MAX_SCORE);\n \t\t\t\trename_count++;\n \t\t\t\tbreak; /* we are done with this entry */\n \t\t\t}\ndiff --git a/dir.c b/dir.c\nindex 32b57f0..b48e19d 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -130,13 +130,13 @@ static int add_excludes_from_file_1(const char *fname,\n {\n \tstruct stat st;\n \tint fd, i;\n-\tlong size;\n+\tsize_t size;\n \tchar *buf, *entry;\n \n \tfd = open(fname, O_RDONLY);\n \tif (fd < 0 || fstat(fd, &st) < 0)\n \t\tgoto err;\n-\tsize = st.st_size;\n+\tsize = xsize_t(st.st_size);\n \tif (size == 0) {\n \t\tclose(fd);\n \t\treturn 0;\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex 33b68e4..7534db1 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -258,6 +258,11 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)\n \t}\n }\n \n+static inline size_t xsize_t(off_t len)\n+{\n+\treturn (size_t)len;\n+}\n+\n static inline int has_extension(const char *filename, const char *ext)\n {\n \tsize_t len = strlen(filename);\ndiff --git a/read-cache.c b/read-cache.c\nindex 4a972b4..6339a27 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -66,7 +66,7 @@ static int ce_compare_data(struct cache_entry *ce, struct stat *st)\n \treturn match;\n }\n \n-static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)\n+static int ce_compare_link(struct cache_entry *ce, size_t expected_size)\n {\n \tint match = -1;\n \tchar *target;\n@@ -101,7 +101,7 @@ static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)\n \t\t\treturn DATA_CHANGED;\n \t\tbreak;\n \tcase S_IFLNK:\n-\t\tif (ce_compare_link(ce, st->st_size))\n+\t\tif (ce_compare_link(ce, xsize_t(st->st_size)))\n \t\t\treturn DATA_CHANGED;\n \t\tbreak;\n \tdefault:\n@@ -797,7 +797,7 @@ int read_cache_from(const char *path)\n \t}\n \n \tif (!fstat(fd, &st)) {\n-\t\tcache_mmap_size = st.st_size;\n+\t\tcache_mmap_size = xsize_t(st.st_size);\n \t\terrno = EINVAL;\n \t\tif (cache_mmap_size >= sizeof(struct cache_header) + 20)\n \t\t\tcache_mmap = xmmap(NULL, cache_mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);\ndiff --git a/refs.c b/refs.c\nindex 7a1f89c..76c08d0 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1075,6 +1075,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *\n \tunsigned long date;\n \tunsigned char logged_sha1[20];\n \tvoid *log_mapped;\n+\tsize_t mapsz;\n \n \tlogfile = git_path(\"logs/%s\", ref);\n \tlogfd = open(logfile, O_RDONLY, 0);\n@@ -1083,7 +1084,8 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *\n \tfstat(logfd, &st);\n \tif (!st.st_size)\n \t\tdie(\"Log %s is empty.\", logfile);\n-\tlog_mapped = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);\n+\tmapsz = xsize_t(st.st_size);\n+\tlog_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0);\n \tlogdata = log_mapped;\n \tclose(logfd);\n \n@@ -1136,7 +1138,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *\n \t\t\t\t\t\tlogfile, show_rfc2822_date(date, tz));\n \t\t\t\t}\n \t\t\t}\n-\t\t\tmunmap(log_mapped, st.st_size);\n+\t\t\tmunmap(log_mapped, mapsz);\n \t\t\treturn 0;\n \t\t}\n \t\tlastrec = rec;\n@@ -1155,7 +1157,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *\n \t\tdie(\"Log %s is corrupt.\", logfile);\n \tif (msg)\n \t\t*msg = ref_msg(logdata, logend);\n-\tmunmap(log_mapped, st.st_size);\n+\tmunmap(log_mapped, mapsz);\n \n \tif (cutoff_time)\n \t\t*cutoff_time = date;\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 50d800e..219a10f 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -349,6 +349,7 @@ static void link_alt_odb_entries(const char *alt, const char *ep, int sep,\n static void read_info_alternates(const char * relative_base, int depth)\n {\n \tchar *map;\n+\tsize_t mapsz;\n \tstruct stat st;\n \tchar path[PATH_MAX];\n \tint fd;\n@@ -361,12 +362,13 @@ static void read_info_alternates(const char * relative_base, int depth)\n \t\tclose(fd);\n \t\treturn;\n \t}\n-\tmap = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);\n+\tmapsz = xsize_t(st.st_size);\n+\tmap = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0);\n \tclose(fd);\n \n-\tlink_alt_odb_entries(map, map + st.st_size, '\\n', relative_base, depth);\n+\tlink_alt_odb_entries(map, map + mapsz, '\\n', relative_base, depth);\n \n-\tmunmap(map, st.st_size);\n+\tmunmap(map, mapsz);\n }\n \n void prepare_alt_odb(void)\n@@ -436,7 +438,7 @@ static int check_packed_git_idx(const char *path,\n {\n \tvoid *idx_map;\n \tuint32_t *index;\n-\tunsigned long idx_size;\n+\tsize_t idx_size;\n \tuint32_t nr, i;\n \tint fd = open(path, O_RDONLY);\n \tstruct stat st;\n@@ -446,7 +448,7 @@ static int check_packed_git_idx(const char *path,\n \t\tclose(fd);\n \t\treturn -1;\n \t}\n-\tidx_size = st.st_size;\n+\tidx_size = xsize_t(st.st_size);\n \tif (idx_size < 4 * 256 + 20 + 20) {\n \t\tclose(fd);\n \t\treturn error(\"index file %s is too small\", path);\n@@ -669,11 +671,13 @@ unsigned char* use_pack(struct packed_git *p,\n \t\t}\n \t\tif (!win) {\n \t\t\tsize_t window_align = packed_git_window_size / 2;\n+\t\t\toff_t len;\n \t\t\twin = xcalloc(1, sizeof(*win));\n \t\t\twin->offset = (offset / window_align) * window_align;\n-\t\t\twin->len = p->pack_size - win->offset;\n-\t\t\tif (win->len > packed_git_window_size)\n-\t\t\t\twin->len = packed_git_window_size;\n+\t\t\tlen = p->pack_size - win->offset;\n+\t\t\tif (len > packed_git_window_size)\n+\t\t\t\tlen = packed_git_window_size;\n+\t\t\twin->len = (size_t)len;\n \t\t\tpack_mapped += win->len;\n \t\t\twhile (packed_git_limit < pack_mapped\n \t\t\t\t&& unuse_one_window(p))\n@@ -702,7 +706,7 @@ unsigned char* use_pack(struct packed_git *p,\n \t}\n \toffset -= win->offset;\n \tif (left)\n-\t\t*left = win->len - offset;\n+\t\t*left = win->len - xsize_t(offset);\n \treturn win->base + offset;\n }\n \n@@ -878,9 +882,9 @@ void *map_sha1_file(const unsigned char *sha1, unsigned long *size)\n \t\t */\n \t\tsha1_file_open_flag = 0;\n \t}\n-\tmap = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);\n+\t*size = xsize_t(st.st_size);\n+\tmap = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);\n \tclose(fd);\n-\t*size = st.st_size;\n \treturn map;\n }\n \n@@ -1346,7 +1350,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,\n uint32_t num_packed_objects(const struct packed_git *p)\n {\n \t/* See check_packed_git_idx() */\n-\treturn (p->index_size - 20 - 20 - 4*256) / 24;\n+\treturn (uint32_t)((p->index_size - 20 - 20 - 4*256) / 24);\n }\n \n int nth_packed_object_sha1(const struct packed_git *p, uint32_t n,\n@@ -2068,7 +2072,7 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)\n int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,\n \t     enum object_type type, const char *path)\n {\n-\tunsigned long size = st->st_size;\n+\tsize_t size = xsize_t(st->st_size);\n \tvoid *buf = NULL;\n \tint ret, re_allocated = 0;\n \n@@ -2111,6 +2115,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write\n {\n \tint fd;\n \tchar *target;\n+\tsize_t len;\n \n \tswitch (st->st_mode & S_IFMT) {\n \tcase S_IFREG:\n@@ -2123,16 +2128,17 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write\n \t\t\t\t     path);\n \t\tbreak;\n \tcase S_IFLNK:\n-\t\ttarget = xmalloc(st->st_size+1);\n-\t\tif (readlink(path, target, st->st_size+1) != st->st_size) {\n+\t\tlen = xsize_t(st->st_size);\n+\t\ttarget = xmalloc(len + 1);\n+\t\tif (readlink(path, target, len + 1) != st->st_size) {\n \t\t\tchar *errstr = strerror(errno);\n \t\t\tfree(target);\n \t\t\treturn error(\"readlink(\\\"%s\\\"): %s\", path,\n \t\t\t             errstr);\n \t\t}\n \t\tif (!write_object)\n-\t\t\thash_sha1_file(target, st->st_size, blob_type, sha1);\n-\t\telse if (write_sha1_file(target, st->st_size, blob_type, sha1))\n+\t\t\thash_sha1_file(target, len, blob_type, sha1);\n+\t\telse if (write_sha1_file(target, len, blob_type, sha1))\n \t\t\treturn error(\"%s: failed to insert into database\",\n \t\t\t\t     path);\n \t\tfree(target);\ndiff --git a/xdiff-interface.c b/xdiff-interface.c\nindex 6c1f99b..10816e9 100644\n--- a/xdiff-interface.c\n+++ b/xdiff-interface.c\n@@ -107,16 +107,18 @@ int read_mmfile(mmfile_t *ptr, const char *filename)\n {\n \tstruct stat st;\n \tFILE *f;\n+\tsize_t sz;\n \n \tif (stat(filename, &st))\n \t\treturn error(\"Could not stat %s\", filename);\n \tif ((f = fopen(filename, \"rb\")) == NULL)\n \t\treturn error(\"Could not open %s\", filename);\n-\tptr->ptr = xmalloc(st.st_size);\n-\tif (fread(ptr->ptr, st.st_size, 1, f) != 1)\n+\tsz = xsize_t(st.st_size);\n+\tptr->ptr = xmalloc(sz);\n+\tif (fread(ptr->ptr, sz, 1, f) != 1)\n \t\treturn error(\"Could not read %s\", filename);\n \tfclose(f);\n-\tptr->size = st.st_size;\n+\tptr->size = sz;\n \treturn 0;\n }\n \n-- \n1.5.0.3.863.gf0989\n"}]}