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 24, 2024
1 parent 6baa460 commit 2dddb73
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 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,13 +30,15 @@ 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)

Check failure on line 40 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / pedantic (fedora)

builtin/patch-id.c:40:21: implicit declaration of function 'strtol_i2'; did you mean 'strtol_i'? [-Werror=implicit-function-declaration]

Check failure on line 40 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / linux-musl (alpine)

builtin/patch-id.c:40:21: implicit declaration of function 'strtol_i2'; did you mean 'strtol_i'? [-Werror=implicit-function-declaration]

Check failure on line 40 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / linux32 (daald/ubuntu32:xenial)

builtin/patch-id.c:40:7: implicit declaration of function 'strtol_i2' [-Werror=implicit-function-declaration]

Check failure on line 40 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / win build

builtin/patch-id.c:40:21: implicit declaration of function 'strtol_i2'; did you mean 'strtol_i'? [-Werror=implicit-function-declaration]

Check failure on line 40 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / linux-asan-ubsan (ubuntu-latest)

builtin/patch-id.c:40:7: implicit declaration of function 'strtol_i2' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

Check failure on line 40 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / linux-gcc (ubuntu-20.04)

builtin/patch-id.c:40:7: implicit declaration of function ‘strtol_i2’; did you mean ‘strtol_i’? [-Werror=implicit-function-declaration]

Check failure on line 40 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / linux-gcc-default (ubuntu-latest)

builtin/patch-id.c:40:21: implicit declaration of function ‘strtol_i2’; did you mean ‘strtol_i’? [-Werror=implicit-function-declaration]

Check failure on line 40 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / linux-leaks (ubuntu-latest)

builtin/patch-id.c:40:21: implicit declaration of function ‘strtol_i2’; did you mean ‘strtol_i’? [-Werror=implicit-function-declaration]

Check failure on line 40 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / linux-sha256 (ubuntu-latest)

builtin/patch-id.c:40:7: implicit declaration of function 'strtol_i2' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

Check failure on line 40 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / linux-TEST-vars (ubuntu-20.04)

builtin/patch-id.c:40:7: implicit declaration of function ‘strtol_i2’; did you mean ‘strtol_i’? [-Werror=implicit-function-declaration]
return 0;
n = strspn(q, digits);
} else {
*p_before = 1;
Expand All @@ -48,7 +51,8 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
n = strspn(r, digits);
if (r[n] == ',') {
r += n + 1;
*p_after = atoi(r);
if (strtol_i2(r, 10, p_after, &endp) != 0)

Check failure on line 54 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / linux-asan-ubsan (ubuntu-latest)

builtin/patch-id.c:54:7: implicit declaration of function 'strtol_i2' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

Check failure on line 54 in builtin/patch-id.c

View workflow job for this annotation

GitHub Actions / linux-sha256 (ubuntu-latest)

builtin/patch-id.c:54:7: implicit declaration of function 'strtol_i2' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
return 0;
n = strspn(r, digits);
} else {
*p_after = 1;
Expand Down

0 comments on commit 2dddb73

Please sign in to comment.