git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Mohit Marathe via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Mohit Marathe <mohitmarathe@proton.me>,
	Mohit Marathe <mohitmarathe23@gmail.com>
Subject: [PATCH v6 2/2] patch-id: replace `atoi()` with `strtoi_with_tail`
Date: Sun, 04 Feb 2024 05:48:38 +0000	[thread overview]
Message-ID: <858d6f94e79ba388267cc24f43ebf68998cd56a8.1707025719.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1646.v6.git.1707025718.gitgitgadget@gmail.com>

From: Mohit Marathe <mohitmarathe23@gmail.com>

The change is made to improve the error-handling capabilities
during the conversion of string to integers. The
`strtoi_with_tail` function offers a more robust mechanism for
converting strings to integers by providing enhanced error
detection. Unlike `atoi`, `strtoi_with_tail` 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 <mohitmarathe@proton.me>
---
 builtin/patch-id.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/builtin/patch-id.c b/builtin/patch-id.c
index 3894d2b9706..4e9a301e9fb 100644
--- a/builtin/patch-id.c
+++ b/builtin/patch-id.c
@@ -1,3 +1,4 @@
+#include "git-compat-util.h"
 #include "builtin.h"
 #include "config.h"
 #include "diff.h"
@@ -29,14 +30,16 @@ 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);
-		n = strspn(q, digits);
+		if (strtoi_with_tail(q, 10, p_before, &endp) != 0)
+			return 0;
+		n = endp - q;
 	} else {
 		*p_before = 1;
 	}
@@ -48,8 +51,9 @@ 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);
-		n = strspn(r, digits);
+		if (strtoi_with_tail(r, 10, p_after, &endp) != 0)
+			return 0;
+		n = endp - r;
 	} else {
 		*p_after = 1;
 	}
-- 
gitgitgadget


  parent reply	other threads:[~2024-02-04  5:49 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-22  8:51 [PATCH 0/2] Replace atoi() with strtol_i2() Mohit Marathe via GitGitGadget
2024-01-22  8:51 ` [PATCH 1/2] git-compat-util: add strtol_i2 Mohit Marathe via GitGitGadget
2024-01-22  8:51 ` [PATCH 2/2] patch-id: replace `atoi()` with `strtol_i2()` Mohit Marathe via GitGitGadget
2024-01-22 19:32   ` Junio C Hamano
2024-01-24  4:22     ` Mohit Marathe
2024-01-24  6:32 ` [PATCH v2 0/2] Replace atoi() with strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24  6:32   ` [PATCH v2 1/2] git-compat-util: add strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24  6:32   ` [PATCH v2 2/2] patch-id: replace `atoi()` with `strtol_i_updated()` Mohit Marathe via GitGitGadget
2024-01-24  6:48   ` [PATCH v3 0/2] Replace atoi() with strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24  6:48     ` [PATCH v3 1/2] git-compat-util: add strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24  6:48     ` [PATCH v3 2/2] patch-id: replace `atoi()` with `strtol_i_updated()` Mohit Marathe via GitGitGadget
2024-01-24  6:55     ` [PATCH v4 0/2] Replace atoi() with strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24  6:55       ` [PATCH v4 1/2] git-compat-util: add strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24 20:20         ` Junio C Hamano
2024-01-24  6:55       ` [PATCH v4 2/2] patch-id: replace `atoi()` with `strtol_i_updated()` Mohit Marathe via GitGitGadget
2024-01-24 21:02         ` Junio C Hamano
2024-01-28  4:35           ` Mohit Marathe
2024-01-28  4:42       ` [PATCH v5 0/2] Replace atoi() with strtoi_with_tail() Mohit Marathe via GitGitGadget
2024-01-28  4:42         ` [PATCH v5 1/2] git-compat-util: add strtoi_with_tail() Mohit Marathe via GitGitGadget
2024-01-30  4:40           ` Junio C Hamano
2024-01-28  4:42         ` [PATCH v5 2/2] patch-id: replace `atoi()` with `strtoi_with_tail` Mohit Marathe via GitGitGadget
2024-02-04  5:48         ` [PATCH v6 0/2] Replace atoi() with strtoi_with_tail() Mohit Marathe via GitGitGadget
2024-02-04  5:48           ` [PATCH v6 1/2] git-compat-util: add strtoi_with_tail() Mohit Marathe via GitGitGadget
2024-02-04  5:48           ` Mohit Marathe via GitGitGadget [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-03-13 15:01 [PATCH v6 2/2] patch-id: replace `atoi()` with `strtoi_with_tail()` Mohit Marathe
2024-03-14  8:08 ` Junio C Hamano
2024-03-14 10:22   ` Mohit Marathe
2024-03-18 15:27     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=858d6f94e79ba388267cc24f43ebf68998cd56a8.1707025719.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=mohitmarathe23@gmail.com \
    --cc=mohitmarathe@proton.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).