Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 944dbd1

Browse files
committed
blame: use size_t for line counts in git_blame__entry
The `git_blame__entry` struct keeps track of line counts with `int` fields. Since `int` is only guaranteed to be at least 16 bits we may overflow on certain platforms when line counts exceed 2^15. Fix this by instead storing line counts in `size_t`.
1 parent cb1cb24 commit 944dbd1

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/blame.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ typedef struct git_blame__entry {
3131
/* the first line of this group in the final image;
3232
* internally all line numbers are 0 based.
3333
*/
34-
int lno;
34+
size_t lno;
3535

3636
/* how many lines this group has */
37-
int num_lines;
37+
size_t num_lines;
3838

3939
/* the commit that introduced this group into the final image */
4040
git_blame__origin *suspect;
@@ -51,7 +51,7 @@ typedef struct git_blame__entry {
5151
/* the line number of the first line of this group in the
5252
* suspect's file; internally all line numbers are 0 based.
5353
*/
54-
int s_lno;
54+
size_t s_lno;
5555

5656
/* how significant this entry is -- cached to avoid
5757
* scanning the lines over and over.

src/blame_git.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,25 @@ static bool same_suspect(git_blame__origin *a, git_blame__origin *b)
9393
}
9494

9595
/* find the line number of the last line the target is suspected for */
96-
static int find_last_in_target(git_blame *blame, git_blame__origin *target)
96+
static bool find_last_in_target(size_t *out, git_blame *blame, git_blame__origin *target)
9797
{
9898
git_blame__entry *e;
99-
int last_in_target = -1;
99+
size_t last_in_target = 0;
100+
bool found = false;
101+
102+
*out = 0;
100103

101104
for (e=blame->ent; e; e=e->next) {
102105
if (e->guilty || !same_suspect(e->suspect, target))
103106
continue;
104-
if (last_in_target < e->s_lno + e->num_lines)
107+
if (last_in_target < e->s_lno + e->num_lines) {
108+
found = true;
105109
last_in_target = e->s_lno + e->num_lines;
110+
}
106111
}
107-
return last_in_target;
112+
113+
*out = last_in_target;
114+
return found;
108115
}
109116

110117
/*
@@ -122,9 +129,9 @@ static int find_last_in_target(git_blame *blame, git_blame__origin *target)
122129
* to be blamed for the parent, and after that portion.
123130
*/
124131
static void split_overlap(git_blame__entry *split, git_blame__entry *e,
125-
int tlno, int plno, int same, git_blame__origin *parent)
132+
size_t tlno, size_t plno, size_t same, git_blame__origin *parent)
126133
{
127-
int chunk_end_lno;
134+
size_t chunk_end_lno;
128135

129136
if (e->s_lno < tlno) {
130137
/* there is a pre-chunk part not blamed on the parent */
@@ -265,9 +272,9 @@ static void decref_split(git_blame__entry *split)
265272
static void blame_overlap(
266273
git_blame *blame,
267274
git_blame__entry *e,
268-
int tlno,
269-
int plno,
270-
int same,
275+
size_t tlno,
276+
size_t plno,
277+
size_t same,
271278
git_blame__origin *parent)
272279
{
273280
git_blame__entry split[3] = {{0}};
@@ -285,9 +292,9 @@ static void blame_overlap(
285292
*/
286293
static void blame_chunk(
287294
git_blame *blame,
288-
int tlno,
289-
int plno,
290-
int same,
295+
size_t tlno,
296+
size_t plno,
297+
size_t same,
291298
git_blame__origin *target,
292299
git_blame__origin *parent)
293300
{
@@ -314,7 +321,7 @@ static int my_emit(
314321
blame_chunk(d->blame, d->tlno, d->plno, start_b, d->target, d->parent);
315322
d->plno = start_a + count_a;
316323
d->tlno = start_b + count_b;
317-
324+
318325
return 0;
319326
}
320327

@@ -376,12 +383,11 @@ static int pass_blame_to_parent(
376383
git_blame__origin *target,
377384
git_blame__origin *parent)
378385
{
379-
int last_in_target;
386+
size_t last_in_target;
380387
mmfile_t file_p, file_o;
381388
blame_chunk_cb_data d = { blame, target, parent, 0, 0 };
382389

383-
last_in_target = find_last_in_target(blame, target);
384-
if (last_in_target < 0)
390+
if (!find_last_in_target(&last_in_target, blame, target))
385391
return 1; /* nothing remains for this target */
386392

387393
fill_origin_blob(parent, &file_p);

0 commit comments

Comments
 (0)