From: Junio C Hamano <gitster@pobox.com>
To: "Mohit Marathe via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, Mohit Marathe <mohitmarathe@proton.me>,
Mohit Marathe <mohitmarathe23@gmail.com>
Subject: Re: [PATCH 2/2] patch-id: replace `atoi()` with `strtol_i2()`
Date: Mon, 22 Jan 2024 11:32:36 -0800 [thread overview]
Message-ID: <xmqq8r4htb8r.fsf@gitster.g> (raw)
In-Reply-To: <1ece724b1ca7f71542bfef42795fce798563ecde.1705913519.git.gitgitgadget@gmail.com> (Mohit Marathe via GitGitGadget's message of "Mon, 22 Jan 2024 08:51:59 +0000")
"Mohit Marathe via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 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;
> }
Looking at this code again, because we upfront run strspn() to make
sure q[] begins with a run of digits *and* followed by a comma
(which is not a digit), I think it is safe to use atoi() and assume
it would slurp all the digits. So the lack of another check the use
of new helper allows us to do, namely
if (endp != q + n)
return 0;
is probably OK, but that is one of the two reasons why you would
favor the use of new helper over atoi(), so the upside of this
change is not all that great as I originally hoped for X-<.
Not your fault, of course. We would still catch when the digit
string that starts q[] is too large to fit in an int, which is an
upside.
> - if (n == 0 || q[n] != ' ' || q[n+1] != '+')
> + if (q[n] != ' ' || q[n+1] != '+')
> return 0;
When we saw q[] that begins with ',' upon entry to this function, we
used to say *p_before = 1 and then saw n==0 and realized it is not a
good input and returned 0 from the function.
Now we instead peek q[0] and the check says q[0] is not SP so we
will return 0 the same way so there is no behaviour change from the
upper hunk? The conversion may be correct, but it wasn't explained
in the proposed commit log message.
How are the change to stop caring about n==0 here ...
> 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;
... and this change here, linked to the switch from atoi() to
strtul_i2()[*]?
It looks like an unrelated behaviour change that is left
unexplained.
> return 1;
> }
Thanks for working on this one.
[Footnote]
* by the way, what a horrible name for a public function. Yuck.
next prev parent reply other threads:[~2024-01-22 19:32 UTC|newest]
Thread overview: 24+ 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 [this message]
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 ` [PATCH v6 2/2] patch-id: replace `atoi()` with `strtoi_with_tail` Mohit Marathe via GitGitGadget
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqq8r4htb8r.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--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.
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 NNTP newsgroup(s).