Skip to content

Commit

Permalink
patch-id: replace atoi() with strtol_i2()
Browse files Browse the repository at this point in the history
The change is made to improve the error-handling capabilities
during the conversion of string representations to integers.
The `strtol_i2(` function offers a more robust mechanism for
converting strings to integers by providing enhanced error
detection. Unlike `atoi(`, `strtol_i2(` allows the code to
differentiate between a valid conversion and an invalid one,
offering better resilience against potential issues such as
reading hunk header of a corrupted patch.

Signed-off-by: Mohit Marathe <[email protected]>
  • Loading branch information
mohit-marathe committed Jan 22, 2024
1 parent 4e2b03c commit 1ece724
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions builtin/patch-id.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "git-compat-util.h"
#include "builtin.h"
#include "config.h"
#include "diff.h"
Expand Down Expand Up @@ -29,33 +30,32 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
{
static const char digits[] = "0123456789";
const char *q, *r;
char *endp;
int n;

q = p + 4;
n = strspn(q, digits);
if (q[n] == ',') {
q += n + 1;
*p_before = atoi(q);
if (strtol_i2(q, 10, p_before, &endp) != 0)
return 0;
n = strspn(q, digits);
} else {
*p_before = 1;
}

if (n == 0 || q[n] != ' ' || q[n+1] != '+')
if (q[n] != ' ' || q[n+1] != '+')
return 0;

r = q + n + 2;
n = strspn(r, digits);
if (r[n] == ',') {
r += n + 1;
*p_after = atoi(r);
n = strspn(r, digits);
if (strtol_i2(r, 10, p_after, &endp) != 0)
return 0;
} else {
*p_after = 1;
}
if (n == 0)
return 0;

return 1;
}

Expand Down

0 comments on commit 1ece724

Please sign in to comment.