@matheusbsbs / matheus-bagliano / commits / 0974c117ff
Fork 0

diff: use large integers for diffstat calculations

The diffstat "added" and "changed" fields generally store line counts; however, for binary files, they store file sizes. Since we store and print these values as ints, a diffstat on a file larger than 2G can show a negative size. Instead, let's use uintmax_t, which should be at least 64 bits on modern platforms. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 17, 2010 at 13:41 UTC 0974c117ff4e17e8b6300519cae0fbc67d34adaa
1 file changed +12 -9
diff.c
+12 -9
@@ -942,7 +942,7 @@ struct diffstat_t {
942 unsigned is_unmerged:1;
943 unsigned is_binary:1;
944 unsigned is_renamed:1;
945 - unsigned int added, deleted;
945 + uintmax_t added, deleted;
946 } **files;
947 };
948
@@ -1034,7 +1034,7 @@ static void fill_print_name(struct diffstat_file *file)
1034 static void show_stats(struct diffstat_t *data, struct diff_options *options)
1035 {
1036 int i, len, add, del, adds = 0, dels = 0;
1037 - int max_change = 0, max_len = 0;
1037 + uintmax_t max_change = 0, max_len = 0;
1038 int total_files = data->nr;
1039 int width, name_width;
1040 const char *reset, *set, *add_c, *del_c;
@@ -1063,7 +1063,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1063
1064 for (i = 0; i < data->nr; i++) {
1065 struct diffstat_file *file = data->files[i];
1066 - int change = file->added + file->deleted;
1066 + uintmax_t change = file->added + file->deleted;
1067 fill_print_name(file);
1068 len = strlen(file->print_name);
1069 if (max_len < len)
@@ -1091,8 +1091,8 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1091 for (i = 0; i < data->nr; i++) {
1092 const char *prefix = "";
1093 char *name = data->files[i]->print_name;
1094 - int added = data->files[i]->added;
1095 - int deleted = data->files[i]->deleted;
1094 + uintmax_t added = data->files[i]->added;
1095 + uintmax_t deleted = data->files[i]->deleted;
1096 int name_len;
1097
1098 /*
@@ -1113,9 +1113,11 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1113 if (data->files[i]->is_binary) {
1114 show_name(options->file, prefix, name, len);
1115 fprintf(options->file, " Bin ");
1116 - fprintf(options->file, "%s%d%s", del_c, deleted, reset);
1116 + fprintf(options->file, "%s%"PRIuMAX"%s",
1117 + del_c, deleted, reset);
1118 fprintf(options->file, " -> ");
1118 - fprintf(options->file, "%s%d%s", add_c, added, reset);
1119 + fprintf(options->file, "%s%"PRIuMAX"%s",
1120 + add_c, added, reset);
1121 fprintf(options->file, " bytes");
1122 fprintf(options->file, "\n");
1123 continue;
@@ -1144,7 +1146,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1146 del = scale_linear(del, width, max_change);
1147 }
1148 show_name(options->file, prefix, name, len);
1147 - fprintf(options->file, "%5d%s", added + deleted,
1149 + fprintf(options->file, "%5"PRIuMAX"%s", added + deleted,
1150 added + deleted ? " " : "");
1151 show_graph(options->file, '+', add, add_c, reset);
1152 show_graph(options->file, '-', del, del_c, reset);
@@ -1194,7 +1196,8 @@ static void show_numstat(struct diffstat_t *data, struct diff_options *options)
1196 fprintf(options->file, "-\t-\t");
1197 else
1198 fprintf(options->file,
1197 - "%d\t%d\t", file->added, file->deleted);
1199 + "%"PRIuMAX"\t%"PRIuMAX"\t",
1200 + file->added, file->deleted);
1201 if (options->line_termination) {
1202 fill_print_name(file);
1203 if (!file->is_renamed)