remote-curl: avoid assigning string constant to non-const variable

When processing remote options, we split the option line into two by
searching for a space. If there is one, we replace the space with '\0',
otherwise we implicitly assume that the value is "true" and thus assign
a string constant.

As the return value of strchr(3P) weirdly enough is a `char *` even
though it gets a `const char *` as input, the assigned-to variable also
is a non-constant. This is fine though because the argument is in fact
an allocated string, and thus we are allowed to modify it. But this will
break once we enable `-Wwrite-strings`.

Refactor the code stop splitting the fields with '\0' altogether.
Instead, we can pass the length of the option name to `set_option()` and
then use strncmp(3P) instead of strcmp(3P).

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt 2024-06-07 08:39:03 +02:00 committed by Junio C Hamano
parent 5bd0851d97
commit a3da6948c3

View File

@ -58,9 +58,9 @@ struct options {
static struct options options; static struct options options;
static struct string_list cas_options = STRING_LIST_INIT_DUP; static struct string_list cas_options = STRING_LIST_INIT_DUP;
static int set_option(const char *name, const char *value) static int set_option(const char *name, size_t namelen, const char *value)
{ {
if (!strcmp(name, "verbosity")) { if (!strncmp(name, "verbosity", namelen)) {
char *end; char *end;
int v = strtol(value, &end, 10); int v = strtol(value, &end, 10);
if (value == end || *end) if (value == end || *end)
@ -68,7 +68,7 @@ static int set_option(const char *name, const char *value)
options.verbosity = v; options.verbosity = v;
return 0; return 0;
} }
else if (!strcmp(name, "progress")) { else if (!strncmp(name, "progress", namelen)) {
if (!strcmp(value, "true")) if (!strcmp(value, "true"))
options.progress = 1; options.progress = 1;
else if (!strcmp(value, "false")) else if (!strcmp(value, "false"))
@ -77,7 +77,7 @@ static int set_option(const char *name, const char *value)
return -1; return -1;
return 0; return 0;
} }
else if (!strcmp(name, "depth")) { else if (!strncmp(name, "depth", namelen)) {
char *end; char *end;
unsigned long v = strtoul(value, &end, 10); unsigned long v = strtoul(value, &end, 10);
if (value == end || *end) if (value == end || *end)
@ -85,15 +85,15 @@ static int set_option(const char *name, const char *value)
options.depth = v; options.depth = v;
return 0; return 0;
} }
else if (!strcmp(name, "deepen-since")) { else if (!strncmp(name, "deepen-since", namelen)) {
options.deepen_since = xstrdup(value); options.deepen_since = xstrdup(value);
return 0; return 0;
} }
else if (!strcmp(name, "deepen-not")) { else if (!strncmp(name, "deepen-not", namelen)) {
string_list_append(&options.deepen_not, value); string_list_append(&options.deepen_not, value);
return 0; return 0;
} }
else if (!strcmp(name, "deepen-relative")) { else if (!strncmp(name, "deepen-relative", namelen)) {
if (!strcmp(value, "true")) if (!strcmp(value, "true"))
options.deepen_relative = 1; options.deepen_relative = 1;
else if (!strcmp(value, "false")) else if (!strcmp(value, "false"))
@ -102,7 +102,7 @@ static int set_option(const char *name, const char *value)
return -1; return -1;
return 0; return 0;
} }
else if (!strcmp(name, "followtags")) { else if (!strncmp(name, "followtags", namelen)) {
if (!strcmp(value, "true")) if (!strcmp(value, "true"))
options.followtags = 1; options.followtags = 1;
else if (!strcmp(value, "false")) else if (!strcmp(value, "false"))
@ -111,7 +111,7 @@ static int set_option(const char *name, const char *value)
return -1; return -1;
return 0; return 0;
} }
else if (!strcmp(name, "dry-run")) { else if (!strncmp(name, "dry-run", namelen)) {
if (!strcmp(value, "true")) if (!strcmp(value, "true"))
options.dry_run = 1; options.dry_run = 1;
else if (!strcmp(value, "false")) else if (!strcmp(value, "false"))
@ -120,7 +120,7 @@ static int set_option(const char *name, const char *value)
return -1; return -1;
return 0; return 0;
} }
else if (!strcmp(name, "check-connectivity")) { else if (!strncmp(name, "check-connectivity", namelen)) {
if (!strcmp(value, "true")) if (!strcmp(value, "true"))
options.check_self_contained_and_connected = 1; options.check_self_contained_and_connected = 1;
else if (!strcmp(value, "false")) else if (!strcmp(value, "false"))
@ -129,7 +129,7 @@ static int set_option(const char *name, const char *value)
return -1; return -1;
return 0; return 0;
} }
else if (!strcmp(name, "cas")) { else if (!strncmp(name, "cas", namelen)) {
struct strbuf val = STRBUF_INIT; struct strbuf val = STRBUF_INIT;
strbuf_addstr(&val, "--force-with-lease="); strbuf_addstr(&val, "--force-with-lease=");
if (*value != '"') if (*value != '"')
@ -139,7 +139,7 @@ static int set_option(const char *name, const char *value)
string_list_append(&cas_options, val.buf); string_list_append(&cas_options, val.buf);
strbuf_release(&val); strbuf_release(&val);
return 0; return 0;
} else if (!strcmp(name, TRANS_OPT_FORCE_IF_INCLUDES)) { } else if (!strncmp(name, TRANS_OPT_FORCE_IF_INCLUDES, namelen)) {
if (!strcmp(value, "true")) if (!strcmp(value, "true"))
options.force_if_includes = 1; options.force_if_includes = 1;
else if (!strcmp(value, "false")) else if (!strcmp(value, "false"))
@ -147,7 +147,7 @@ static int set_option(const char *name, const char *value)
else else
return -1; return -1;
return 0; return 0;
} else if (!strcmp(name, "cloning")) { } else if (!strncmp(name, "cloning", namelen)) {
if (!strcmp(value, "true")) if (!strcmp(value, "true"))
options.cloning = 1; options.cloning = 1;
else if (!strcmp(value, "false")) else if (!strcmp(value, "false"))
@ -155,7 +155,7 @@ static int set_option(const char *name, const char *value)
else else
return -1; return -1;
return 0; return 0;
} else if (!strcmp(name, "update-shallow")) { } else if (!strncmp(name, "update-shallow", namelen)) {
if (!strcmp(value, "true")) if (!strcmp(value, "true"))
options.update_shallow = 1; options.update_shallow = 1;
else if (!strcmp(value, "false")) else if (!strcmp(value, "false"))
@ -163,7 +163,7 @@ static int set_option(const char *name, const char *value)
else else
return -1; return -1;
return 0; return 0;
} else if (!strcmp(name, "pushcert")) { } else if (!strncmp(name, "pushcert", namelen)) {
if (!strcmp(value, "true")) if (!strcmp(value, "true"))
options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS; options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
else if (!strcmp(value, "false")) else if (!strcmp(value, "false"))
@ -173,7 +173,7 @@ static int set_option(const char *name, const char *value)
else else
return -1; return -1;
return 0; return 0;
} else if (!strcmp(name, "atomic")) { } else if (!strncmp(name, "atomic", namelen)) {
if (!strcmp(value, "true")) if (!strcmp(value, "true"))
options.atomic = 1; options.atomic = 1;
else if (!strcmp(value, "false")) else if (!strcmp(value, "false"))
@ -181,7 +181,7 @@ static int set_option(const char *name, const char *value)
else else
return -1; return -1;
return 0; return 0;
} else if (!strcmp(name, "push-option")) { } else if (!strncmp(name, "push-option", namelen)) {
if (*value != '"') if (*value != '"')
string_list_append(&options.push_options, value); string_list_append(&options.push_options, value);
else { else {
@ -192,7 +192,7 @@ static int set_option(const char *name, const char *value)
strbuf_detach(&unquoted, NULL)); strbuf_detach(&unquoted, NULL));
} }
return 0; return 0;
} else if (!strcmp(name, "family")) { } else if (!strncmp(name, "family", namelen)) {
if (!strcmp(value, "ipv4")) if (!strcmp(value, "ipv4"))
git_curl_ipresolve = CURL_IPRESOLVE_V4; git_curl_ipresolve = CURL_IPRESOLVE_V4;
else if (!strcmp(value, "ipv6")) else if (!strcmp(value, "ipv6"))
@ -202,16 +202,16 @@ static int set_option(const char *name, const char *value)
else else
return -1; return -1;
return 0; return 0;
} else if (!strcmp(name, "from-promisor")) { } else if (!strncmp(name, "from-promisor", namelen)) {
options.from_promisor = 1; options.from_promisor = 1;
return 0; return 0;
} else if (!strcmp(name, "refetch")) { } else if (!strncmp(name, "refetch", namelen)) {
options.refetch = 1; options.refetch = 1;
return 0; return 0;
} else if (!strcmp(name, "filter")) { } else if (!strncmp(name, "filter", namelen)) {
options.filter = xstrdup(value); options.filter = xstrdup(value);
return 0; return 0;
} else if (!strcmp(name, "object-format")) { } else if (!strncmp(name, "object-format", namelen)) {
options.object_format = 1; options.object_format = 1;
if (strcmp(value, "true")) if (strcmp(value, "true"))
die(_("unknown value for object-format: %s"), value); die(_("unknown value for object-format: %s"), value);
@ -1588,15 +1588,16 @@ int cmd_main(int argc, const char **argv)
parse_push(&buf); parse_push(&buf);
} else if (skip_prefix(buf.buf, "option ", &arg)) { } else if (skip_prefix(buf.buf, "option ", &arg)) {
char *value = strchr(arg, ' '); const char *value = strchrnul(arg, ' ');
size_t arglen = value - arg;
int result; int result;
if (value) if (*value)
*value++ = '\0'; value++; /* skip over SP */
else else
value = "true"; value = "true";
result = set_option(arg, value); result = set_option(arg, arglen, value);
if (!result) if (!result)
printf("ok\n"); printf("ok\n");
else if (result < 0) else if (result < 0)