@matheusbsbs / matheus-bagliano / commits / dc49cd769b
Fork 0

Cast 64 bit off_t to 32 bit size_t

Some systems have sizeof(off_t) == 8 while sizeof(size_t) == 4. This implies that we are able to access and work on files whose maximum length is around 2^63-1 bytes, but we can only malloc or mmap somewhat less than 2^32-1 bytes of memory. On such a system an implicit conversion of off_t to size_t can cause the size_t to wrap, resulting in unexpected and exciting behavior. Right now we are working around all gcc warnings generated by the -Wshorten-64-to-32 option by passing the off_t through xsize_t(). In the future we should make xsize_t on such problematic platforms detect the wrapping and die if such a file is accessed. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Mar 6, 2007 at 20:44 UTC dc49cd769b5fa6b7e0114b051c34a849828a7603
16 files changed +83 -59
builtin-apply.c
+1 -1
@@ -1981,7 +1981,7 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
1981 }
1982 }
1983 else if (patch->old_name) {
1984 - size = st->st_size;
1984 + size = xsize_t(st->st_size);
1985 alloc = size + 8192;
1986 buf = xmalloc(alloc);
1987 if (read_old_data(st, patch->old_name, &buf, &alloc, &size))
builtin-blame.c
+1 -1
@@ -1963,7 +1963,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
1963 die("Cannot lstat %s", path);
1964 read_from = path;
1965 }
1966 - fin_size = st.st_size;
1966 + fin_size = xsize_t(st.st_size);
1967 buf = xmalloc(fin_size+1);
1968 mode = canon_mode(st.st_mode);
1969 switch (st.st_mode & S_IFMT) {
builtin-count-objects.c
+1 -1
@@ -44,7 +44,7 @@ static void count_objects(DIR *d, char *path, int len, int verbose,
44 if (lstat(path, &st) || !S_ISREG(st.st_mode))
45 bad = 1;
46 else
47 - (*loose_size) += st.st_blocks;
47 + (*loose_size) += xsize_t(st.st_blocks);
48 }
49 if (bad) {
50 if (verbose) {
builtin-grep.c
+6 -3
@@ -122,6 +122,8 @@ static int grep_file(struct grep_opt *opt, const char *filename)
122 struct stat st;
123 int i;
124 char *data;
125 + size_t sz;
126 +
127 if (lstat(filename, &st) < 0) {
128 err_ret:
129 if (errno != ENOENT)
@@ -132,11 +134,12 @@ static int grep_file(struct grep_opt *opt, const char *filename)
134 return 0; /* empty file -- no grep hit */
135 if (!S_ISREG(st.st_mode))
136 return 0;
137 + sz = xsize_t(st.st_size);
138 i = open(filename, O_RDONLY);
139 if (i < 0)
140 goto err_ret;
138 - data = xmalloc(st.st_size + 1);
139 - if (st.st_size != read_in_full(i, data, st.st_size)) {
141 + data = xmalloc(sz + 1);
142 + if (st.st_size != read_in_full(i, data, sz)) {
143 error("'%s': short read %s", filename, strerror(errno));
144 close(i);
145 free(data);
@@ -145,7 +148,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
148 close(i);
149 if (opt->relative && opt->prefix_length)
150 filename += opt->prefix_length;
148 - i = grep_buffer(opt, filename, data, st.st_size);
151 + i = grep_buffer(opt, filename, data, sz);
152 free(data);
153 return i;
154 }
combine-diff.c
+2 -2
@@ -684,7 +684,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
684 goto deleted_file;
685
686 if (S_ISLNK(st.st_mode)) {
687 - size_t len = st.st_size;
687 + size_t len = xsize_t(st.st_size);
688 result_size = len;
689 result = xmalloc(len + 1);
690 if (result_size != readlink(elem->path, result, len)) {
@@ -697,7 +697,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
697 }
698 else if (0 <= (fd = open(elem->path, O_RDONLY)) &&
699 !fstat(fd, &st)) {
700 - size_t len = st.st_size;
700 + size_t len = xsize_t(st.st_size);
701 size_t sz = 0;
702 int is_file, i;
703
config.c
+15 -13
@@ -431,7 +431,7 @@ static struct {
431 int do_not_match;
432 regex_t* value_regex;
433 int multi_replace;
434 - off_t offset[MAX_MATCHES];
434 + size_t offset[MAX_MATCHES];
435 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
436 int seen;
437 } store;
@@ -579,11 +579,11 @@ static int store_write_pair(int fd, const char* key, const char* value)
579 return 1;
580 }
581
582 -static int find_beginning_of_line(const char* contents, int size,
583 - int offset_, int* found_bracket)
582 +static ssize_t find_beginning_of_line(const char* contents, size_t size,
583 + size_t offset_, int* found_bracket)
584 {
585 - int equal_offset = size, bracket_offset = size;
586 - int offset;
585 + size_t equal_offset = size, bracket_offset = size;
586 + ssize_t offset;
587
588 for (offset = offset_-2; offset > 0
589 && contents[offset] != '\n'; offset--)
@@ -727,7 +727,8 @@ int git_config_set_multivar(const char* key, const char* value,
727 } else {
728 struct stat st;
729 char* contents;
730 - int i, copy_begin, copy_end, new_line = 0;
730 + size_t contents_sz, copy_begin, copy_end;
731 + int i, new_line = 0;
732
733 if (value_regex == NULL)
734 store.value_regex = NULL;
@@ -784,7 +785,8 @@ int git_config_set_multivar(const char* key, const char* value,
785 }
786
787 fstat(in_fd, &st);
787 - contents = xmmap(NULL, st.st_size, PROT_READ,
788 + contents_sz = xsize_t(st.st_size);
789 + contents = xmmap(NULL, contents_sz, PROT_READ,
790 MAP_PRIVATE, in_fd, 0);
791 close(in_fd);
792
@@ -793,12 +795,12 @@ int git_config_set_multivar(const char* key, const char* value,
795
796 for (i = 0, copy_begin = 0; i < store.seen; i++) {
797 if (store.offset[i] == 0) {
796 - store.offset[i] = copy_end = st.st_size;
798 + store.offset[i] = copy_end = contents_sz;
799 } else if (store.state != KEY_SEEN) {
800 copy_end = store.offset[i];
801 } else
802 copy_end = find_beginning_of_line(
801 - contents, st.st_size,
803 + contents, contents_sz,
804 store.offset[i]-2, &new_line);
805
806 /* write the first part of the config */
@@ -825,13 +827,13 @@ int git_config_set_multivar(const char* key, const char* value,
827 }
828
829 /* write the rest of the config */
828 - if (copy_begin < st.st_size)
830 + if (copy_begin < contents_sz)
831 if (write_in_full(fd, contents + copy_begin,
830 - st.st_size - copy_begin) <
831 - st.st_size - copy_begin)
832 + contents_sz - copy_begin) <
833 + contents_sz - copy_begin)
834 goto write_err_out;
835
834 - munmap(contents, st.st_size);
836 + munmap(contents, contents_sz);
837 unlink(config_filename);
838 }
839
diff.c
+5 -4
@@ -1399,7 +1399,7 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1399 return err;
1400 }
1401 }
1402 - s->size = st.st_size;
1402 + s->size = xsize_t(st.st_size);
1403 if (!s->size)
1404 goto empty;
1405 if (size_only)
@@ -1515,12 +1515,13 @@ static void prepare_temp_file(const char *name,
1515 if (S_ISLNK(st.st_mode)) {
1516 int ret;
1517 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1518 + size_t sz = xsize_t(st.st_size);
1519 if (sizeof(buf) <= st.st_size)
1520 die("symlink too long: %s", name);
1520 - ret = readlink(name, buf, st.st_size);
1521 + ret = readlink(name, buf, sz);
1522 if (ret < 0)
1523 die("readlink(%s)", name);
1523 - prep_temp_blob(temp, buf, st.st_size,
1524 + prep_temp_blob(temp, buf, sz,
1525 (one->sha1_valid ?
1526 one->sha1 : null_sha1),
1527 (one->sha1_valid ?
@@ -2138,7 +2139,7 @@ static int parse_num(const char **cp_p)
2139 /* user says num divided by scale and we say internally that
2140 * is MAX_SCORE * num / scale.
2141 */
2141 - return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
2142 + return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
2143 }
2144
2145 int diff_scoreopt_parse(const char *opt)
diffcore-break.c
+1 -1
@@ -89,7 +89,7 @@ static int should_break(struct diff_filespec *src,
89 * merge the surviving pair together if the score is
90 * less than the minimum, after rename/copy runs.
91 */
92 - *merge_score_p = src_removed * MAX_SCORE / src->size;
92 + *merge_score_p = (int)(src_removed * MAX_SCORE / src->size);
93
94 /* Extent of damage, which counts both inserts and
95 * deletes.
diffcore-order.c
+4 -2
@@ -14,6 +14,7 @@ static void prepare_order(const char *orderfile)
14 void *map;
15 char *cp, *endp;
16 struct stat st;
17 + size_t sz;
18
19 if (order)
20 return;
@@ -25,11 +26,12 @@ static void prepare_order(const char *orderfile)
26 close(fd);
27 return;
28 }
28 - map = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
29 + sz = xsize_t(st.st_size);
30 + map = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
31 close(fd);
32 if (map == MAP_FAILED)
33 return;
32 - endp = (char *) map + st.st_size;
34 + endp = (char *) map + sz;
35 for (pass = 0; pass < 2; pass++) {
36 cnt = 0;
37 cp = map;
diffcore-rename.c
+4 -3
@@ -172,7 +172,8 @@ static int estimate_similarity(struct diff_filespec *src,
172 return 0; /* error but caught downstream */
173
174
175 - delta_limit = base_size * (MAX_SCORE-minimum_score) / MAX_SCORE;
175 + delta_limit = (unsigned long)
176 + (base_size * (MAX_SCORE-minimum_score) / MAX_SCORE);
177 if (diffcore_count_changes(src->data, src->size,
178 dst->data, dst->size,
179 &src->cnt_data, &dst->cnt_data,
@@ -186,7 +187,7 @@ static int estimate_similarity(struct diff_filespec *src,
187 if (!dst->size)
188 score = 0; /* should not happen */
189 else
189 - score = src_copied * MAX_SCORE / max_size;
190 + score = (int)(src_copied * MAX_SCORE / max_size);
191 return score;
192 }
193
@@ -297,7 +298,7 @@ void diffcore_rename(struct diff_options *options)
298 struct diff_filespec *one = rename_src[j].one;
299 if (!is_exact_match(one, two, contents_too))
300 continue;
300 - record_rename_pair(i, j, MAX_SCORE);
301 + record_rename_pair(i, j, (int)MAX_SCORE);
302 rename_count++;
303 break; /* we are done with this entry */
304 }
dir.c
+2 -2
@@ -130,13 +130,13 @@ static int add_excludes_from_file_1(const char *fname,
130 {
131 struct stat st;
132 int fd, i;
133 - long size;
133 + size_t size;
134 char *buf, *entry;
135
136 fd = open(fname, O_RDONLY);
137 if (fd < 0 || fstat(fd, &st) < 0)
138 goto err;
139 - size = st.st_size;
139 + size = xsize_t(st.st_size);
140 if (size == 0) {
141 close(fd);
142 return 0;
git-compat-util.h
+5
@@ -258,6 +258,11 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)
258 }
259 }
260
261 +static inline size_t xsize_t(off_t len)
262 +{
263 + return (size_t)len;
264 +}
265 +
266 static inline int has_extension(const char *filename, const char *ext)
267 {
268 size_t len = strlen(filename);
read-cache.c
+3 -3
@@ -66,7 +66,7 @@ static int ce_compare_data(struct cache_entry *ce, struct stat *st)
66 return match;
67 }
68
69 -static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
69 +static int ce_compare_link(struct cache_entry *ce, size_t expected_size)
70 {
71 int match = -1;
72 char *target;
@@ -101,7 +101,7 @@ static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
101 return DATA_CHANGED;
102 break;
103 case S_IFLNK:
104 - if (ce_compare_link(ce, st->st_size))
104 + if (ce_compare_link(ce, xsize_t(st->st_size)))
105 return DATA_CHANGED;
106 break;
107 default:
@@ -797,7 +797,7 @@ int read_cache_from(const char *path)
797 }
798
799 if (!fstat(fd, &st)) {
800 - cache_mmap_size = st.st_size;
800 + cache_mmap_size = xsize_t(st.st_size);
801 errno = EINVAL;
802 if (cache_mmap_size >= sizeof(struct cache_header) + 20)
803 cache_mmap = xmmap(NULL, cache_mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
refs.c
+5 -3
@@ -1075,6 +1075,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1075 unsigned long date;
1076 unsigned char logged_sha1[20];
1077 void *log_mapped;
1078 + size_t mapsz;
1079
1080 logfile = git_path("logs/%s", ref);
1081 logfd = open(logfile, O_RDONLY, 0);
@@ -1083,7 +1084,8 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1084 fstat(logfd, &st);
1085 if (!st.st_size)
1086 die("Log %s is empty.", logfile);
1086 - log_mapped = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
1087 + mapsz = xsize_t(st.st_size);
1088 + log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0);
1089 logdata = log_mapped;
1090 close(logfd);
1091
@@ -1136,7 +1138,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1138 logfile, show_rfc2822_date(date, tz));
1139 }
1140 }
1139 - munmap(log_mapped, st.st_size);
1141 + munmap(log_mapped, mapsz);
1142 return 0;
1143 }
1144 lastrec = rec;
@@ -1155,7 +1157,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1157 die("Log %s is corrupt.", logfile);
1158 if (msg)
1159 *msg = ref_msg(logdata, logend);
1158 - munmap(log_mapped, st.st_size);
1160 + munmap(log_mapped, mapsz);
1161
1162 if (cutoff_time)
1163 *cutoff_time = date;
sha1_file.c
+23 -17
@@ -349,6 +349,7 @@ static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
349 static void read_info_alternates(const char * relative_base, int depth)
350 {
351 char *map;
352 + size_t mapsz;
353 struct stat st;
354 char path[PATH_MAX];
355 int fd;
@@ -361,12 +362,13 @@ static void read_info_alternates(const char * relative_base, int depth)
362 close(fd);
363 return;
364 }
364 - map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
365 + mapsz = xsize_t(st.st_size);
366 + map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0);
367 close(fd);
368
367 - link_alt_odb_entries(map, map + st.st_size, '\n', relative_base, depth);
369 + link_alt_odb_entries(map, map + mapsz, '\n', relative_base, depth);
370
369 - munmap(map, st.st_size);
371 + munmap(map, mapsz);
372 }
373
374 void prepare_alt_odb(void)
@@ -436,7 +438,7 @@ static int check_packed_git_idx(const char *path,
438 {
439 void *idx_map;
440 uint32_t *index;
439 - unsigned long idx_size;
441 + size_t idx_size;
442 uint32_t nr, i;
443 int fd = open(path, O_RDONLY);
444 struct stat st;
@@ -446,7 +448,7 @@ static int check_packed_git_idx(const char *path,
448 close(fd);
449 return -1;
450 }
449 - idx_size = st.st_size;
451 + idx_size = xsize_t(st.st_size);
452 if (idx_size < 4 * 256 + 20 + 20) {
453 close(fd);
454 return error("index file %s is too small", path);
@@ -669,11 +671,13 @@ unsigned char* use_pack(struct packed_git *p,
671 }
672 if (!win) {
673 size_t window_align = packed_git_window_size / 2;
674 + off_t len;
675 win = xcalloc(1, sizeof(*win));
676 win->offset = (offset / window_align) * window_align;
674 - win->len = p->pack_size - win->offset;
675 - if (win->len > packed_git_window_size)
676 - win->len = packed_git_window_size;
677 + len = p->pack_size - win->offset;
678 + if (len > packed_git_window_size)
679 + len = packed_git_window_size;
680 + win->len = (size_t)len;
681 pack_mapped += win->len;
682 while (packed_git_limit < pack_mapped
683 && unuse_one_window(p))
@@ -702,7 +706,7 @@ unsigned char* use_pack(struct packed_git *p,
706 }
707 offset -= win->offset;
708 if (left)
705 - *left = win->len - offset;
709 + *left = win->len - xsize_t(offset);
710 return win->base + offset;
711 }
712
@@ -878,9 +882,9 @@ void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
882 */
883 sha1_file_open_flag = 0;
884 }
881 - map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
885 + *size = xsize_t(st.st_size);
886 + map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
887 close(fd);
883 - *size = st.st_size;
888 return map;
889 }
890
@@ -1346,7 +1350,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
1350 uint32_t num_packed_objects(const struct packed_git *p)
1351 {
1352 /* See check_packed_git_idx() */
1349 - return (p->index_size - 20 - 20 - 4*256) / 24;
1353 + return (uint32_t)((p->index_size - 20 - 20 - 4*256) / 24);
1354 }
1355
1356 int nth_packed_object_sha1(const struct packed_git *p, uint32_t n,
@@ -2068,7 +2072,7 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
2072 int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2073 enum object_type type, const char *path)
2074 {
2071 - unsigned long size = st->st_size;
2075 + size_t size = xsize_t(st->st_size);
2076 void *buf = NULL;
2077 int ret, re_allocated = 0;
2078
@@ -2111,6 +2115,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
2115 {
2116 int fd;
2117 char *target;
2118 + size_t len;
2119
2120 switch (st->st_mode & S_IFMT) {
2121 case S_IFREG:
@@ -2123,16 +2128,17 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
2128 path);
2129 break;
2130 case S_IFLNK:
2126 - target = xmalloc(st->st_size+1);
2127 - if (readlink(path, target, st->st_size+1) != st->st_size) {
2131 + len = xsize_t(st->st_size);
2132 + target = xmalloc(len + 1);
2133 + if (readlink(path, target, len + 1) != st->st_size) {
2134 char *errstr = strerror(errno);
2135 free(target);
2136 return error("readlink(\"%s\"): %s", path,
2137 errstr);
2138 }
2139 if (!write_object)
2134 - hash_sha1_file(target, st->st_size, blob_type, sha1);
2135 - else if (write_sha1_file(target, st->st_size, blob_type, sha1))
2140 + hash_sha1_file(target, len, blob_type, sha1);
2141 + else if (write_sha1_file(target, len, blob_type, sha1))
2142 return error("%s: failed to insert into database",
2143 path);
2144 free(target);
xdiff-interface.c
+5 -3
@@ -107,16 +107,18 @@ int read_mmfile(mmfile_t *ptr, const char *filename)
107 {
108 struct stat st;
109 FILE *f;
110 + size_t sz;
111
112 if (stat(filename, &st))
113 return error("Could not stat %s", filename);
114 if ((f = fopen(filename, "rb")) == NULL)
115 return error("Could not open %s", filename);
115 - ptr->ptr = xmalloc(st.st_size);
116 - if (fread(ptr->ptr, st.st_size, 1, f) != 1)
116 + sz = xsize_t(st.st_size);
117 + ptr->ptr = xmalloc(sz);
118 + if (fread(ptr->ptr, sz, 1, f) != 1)
119 return error("Could not read %s", filename);
120 fclose(f);
119 - ptr->size = st.st_size;
121 + ptr->size = sz;
122 return 0;
123 }
124