Skip to content

Commit 0974c11

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 902f235 commit 0974c11

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

diff.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ struct diffstat_t {
942942
unsigned is_unmerged:1;
943943
unsigned is_binary:1;
944944
unsigned is_renamed:1;
945-
unsigned int added, deleted;
945+
uintmax_t added, deleted;
946946
} **files;
947947
};
948948

@@ -1034,7 +1034,7 @@ static void fill_print_name(struct diffstat_file *file)
10341034
static void show_stats(struct diffstat_t *data, struct diff_options *options)
10351035
{
10361036
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;
10381038
int total_files = data->nr;
10391039
int width, name_width;
10401040
const char *reset, *set, *add_c, *del_c;
@@ -1063,7 +1063,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
10631063

10641064
for (i = 0; i < data->nr; i++) {
10651065
struct diffstat_file *file = data->files[i];
1066-
int change = file->added + file->deleted;
1066+
uintmax_t change = file->added + file->deleted;
10671067
fill_print_name(file);
10681068
len = strlen(file->print_name);
10691069
if (max_len < len)
@@ -1091,8 +1091,8 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
10911091
for (i = 0; i < data->nr; i++) {
10921092
const char *prefix = "";
10931093
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;
10961096
int name_len;
10971097

10981098
/*
@@ -1113,9 +1113,11 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
11131113
if (data->files[i]->is_binary) {
11141114
show_name(options->file, prefix, name, len);
11151115
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);
11171118
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);
11191121
fprintf(options->file, " bytes");
11201122
fprintf(options->file, "\n");
11211123
continue;
@@ -1144,7 +1146,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
11441146
del = scale_linear(del, width, max_change);
11451147
}
11461148
show_name(options->file, prefix, name, len);
1147-
fprintf(options->file, "%5d%s", added + deleted,
1149+
fprintf(options->file, "%5"PRIuMAX"%s", added + deleted,
11481150
added + deleted ? " " : "");
11491151
show_graph(options->file, '+', add, add_c, reset);
11501152
show_graph(options->file, '-', del, del_c, reset);
@@ -1194,7 +1196,8 @@ static void show_numstat(struct diffstat_t *data, struct diff_options *options)
11941196
fprintf(options->file, "-\t-\t");
11951197
else
11961198
fprintf(options->file,
1197-
"%d\t%d\t", file->added, file->deleted);
1199+
"%"PRIuMAX"\t%"PRIuMAX"\t",
1200+
file->added, file->deleted);
11981201
if (options->line_termination) {
11991202
fill_print_name(file);
12001203
if (!file->is_renamed)

0 commit comments

Comments
 (0)