From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] C: use skip-prefix to avoid hardcoded string length
Date: Mon, 27 Jan 2020 18:20:04 -0500 [thread overview]
Message-ID: <20200127232004.GE19360@coredump.intra.peff.net> (raw)
In-Reply-To: <xmqqv9owa6cw.fsf@gitster-ct.c.googlers.com>
On Mon, Jan 27, 2020 at 02:21:03PM -0800, Junio C Hamano wrote:
> We often skip an optional prefix in a string with a hardcoded
> constant, e.g.
>
> if (starts_with(string, "prefix"))
> string += 6;
>
> which is less error prone when written
>
> skip_prefix(string, "prefix", &string);
All of these look obviously correct, because you've introduced new
temporary variables to replace the computed values. But...
> diff --git a/builtin/reflog.c b/builtin/reflog.c
> index 4d3430900d..51ffd74945 100644
> --- a/builtin/reflog.c
> +++ b/builtin/reflog.c
> @@ -560,15 +560,17 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
>
> for (i = 1; i < argc; i++) {
> const char *arg = argv[i];
> + const char *valptr;
> +
> if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
> flags |= EXPIRE_REFLOGS_DRY_RUN;
> - else if (starts_with(arg, "--expire=")) {
> - if (parse_expiry_date(arg + 9, &cb.cmd.expire_total))
> + else if (skip_prefix(arg, "--expire=", &valptr)) {
> + if (parse_expiry_date(valptr, &cb.cmd.expire_total))
> die(_("'%s' is not a valid timestamp"), arg);
> explicit_expiry |= EXPIRE_TOTAL;
> }
In this case, I think the die message in the context could be improved
to show "valptr". At which point you might as well do:
else if (skip_prefix(arg, "--expire=", &arg)) {
> - else if (starts_with(arg, "--expire-unreachable=")) {
> - if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable))
> + else if (skip_prefix(arg, "--expire-unreachable=", &valptr)) {
> + if (parse_expiry_date(valptr, &cb.cmd.expire_unreachable))
> die(_("'%s' is not a valid timestamp"), arg);
> explicit_expiry |= EXPIRE_UNREACH;
> }
Ditto here.
-Peff
next prev parent reply other threads:[~2020-01-27 23:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-27 22:21 [PATCH] C: use skip-prefix to avoid hardcoded string length Junio C Hamano
2020-01-27 23:20 ` Jeff King [this message]
2020-01-28 0:45 ` Junio C Hamano
2020-01-30 19:35 ` [PATCH v2] " Junio C Hamano
2020-01-31 0:19 ` Jeff King
2020-01-31 20:15 ` Andrei Rybak
2020-01-31 21:03 ` 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=20200127232004.GE19360@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/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).