@matheusbsbs / matheus-bagliano / commits / 145136a95a
Fork 0

C: use skip_prefix() to avoid hardcoded string length

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); Note that this changes a few error messages from "git reflog expire --expire=nonsense.timestamp", which used to complain by saying '--expire=nonsense.timestamp' is not a valid timestamp but with this change, we say 'nonsense.timestamp' is not a valid timestamp which is more technically correct (the string with --expire= as a prefix obviously cannot be a valid timestamp, but the error is about the part of the input without that prefix). Helped-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 30, 2020 at 11:35 UTC 145136a95a8755528aa012a4ce0ed50d1ec39e24
8 files changed +18 -25
builtin/fast-export.c
+1 -2
@@ -870,8 +870,7 @@ static void handle_tag(const char *name, struct tag *tag)
870 printf("reset %s\nfrom %s\n\n",
871 name, oid_to_hex(&null_oid));
872 }
873 - if (starts_with(name, "refs/tags/"))
874 - name += 10;
873 + skip_prefix(name, "refs/tags/", &name);
874 printf("tag %s\n", name);
875 if (mark_tags) {
876 mark_next_object(&tag->object);
builtin/reflog.c
+5 -4
@@ -560,15 +560,16 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
560
561 for (i = 1; i < argc; i++) {
562 const char *arg = argv[i];
563 +
564 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
565 flags |= EXPIRE_REFLOGS_DRY_RUN;
565 - else if (starts_with(arg, "--expire=")) {
566 - if (parse_expiry_date(arg + 9, &cb.cmd.expire_total))
566 + else if (skip_prefix(arg, "--expire=", &arg)) {
567 + if (parse_expiry_date(arg, &cb.cmd.expire_total))
568 die(_("'%s' is not a valid timestamp"), arg);
569 explicit_expiry |= EXPIRE_TOTAL;
570 }
570 - else if (starts_with(arg, "--expire-unreachable=")) {
571 - if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable))
571 + else if (skip_prefix(arg, "--expire-unreachable=", &arg)) {
572 + if (parse_expiry_date(arg, &cb.cmd.expire_unreachable))
573 die(_("'%s' is not a valid timestamp"), arg);
574 explicit_expiry |= EXPIRE_UNREACH;
575 }
notes.c
+2 -4
@@ -1279,10 +1279,8 @@ static void format_note(struct notes_tree *t, const struct object_id *object_oid
1279 if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
1280 strbuf_addstr(sb, "\nNotes:\n");
1281 } else {
1282 - if (starts_with(ref, "refs/"))
1283 - ref += 5;
1284 - if (starts_with(ref, "notes/"))
1285 - ref += 6;
1282 + skip_prefix(ref, "refs/", &ref);
1283 + skip_prefix(ref, "notes/", &ref);
1284 strbuf_addf(sb, "\nNotes (%s):\n", ref);
1285 }
1286 }
parse-options.c
+1 -2
@@ -357,8 +357,7 @@ is_abbreviated:
357 }
358 /* negated? */
359 if (!starts_with(arg, "no-")) {
360 - if (starts_with(long_name, "no-")) {
361 - long_name += 3;
360 + if (skip_prefix(long_name, "no-", &long_name)) {
361 opt_flags |= OPT_UNSET;
362 goto again;
363 }
refs/files-backend.c
+1 -2
@@ -465,8 +465,7 @@ stat_ref:
465 close(fd);
466 strbuf_rtrim(&sb_contents);
467 buf = sb_contents.buf;
468 - if (starts_with(buf, "ref:")) {
469 - buf += 4;
468 + if (skip_prefix(buf, "ref:", &buf)) {
469 while (isspace(*buf))
470 buf++;
471
remote-curl.c
+3 -2
@@ -1255,8 +1255,9 @@ static void parse_push(struct strbuf *buf)
1255 int ret;
1256
1257 do {
1258 - if (starts_with(buf->buf, "push "))
1259 - argv_array_push(&specs, buf->buf + 5);
1258 + const char *arg;
1259 + if (skip_prefix(buf->buf, "push ", &arg))
1260 + argv_array_push(&specs, arg);
1261 else
1262 die(_("http transport does not support %s"), buf->buf);
1263
sha1-name.c
+2 -7
@@ -908,14 +908,9 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
908 real_ref, flags, at_time, nth, oid, NULL,
909 &co_time, &co_tz, &co_cnt)) {
910 if (!len) {
911 - if (starts_with(real_ref, "refs/heads/")) {
912 - str = real_ref + 11;
913 - len = strlen(real_ref + 11);
914 - } else {
915 - /* detached HEAD */
911 + if (!skip_prefix(real_ref, "refs/heads/", &str))
912 str = "HEAD";
917 - len = 4;
918 - }
913 + len = strlen(str);
914 }
915 if (at_time) {
916 if (!(flags & GET_OID_QUIETLY)) {
transport-helper.c
+3 -2
@@ -404,11 +404,12 @@ static int fetch_with_fetch(struct transport *transport,
404 sendline(data, &buf);
405
406 while (1) {
407 + const char *name;
408 +
409 if (recvline(data, &buf))
410 exit(128);
411
410 - if (starts_with(buf.buf, "lock ")) {
411 - const char *name = buf.buf + 5;
412 + if (skip_prefix(buf.buf, "lock ", &name)) {
413 if (transport->pack_lockfile)
414 warning(_("%s also locked %s"), data->name, name);
415 else