Skip to content

Commit a31cfe3

Browse files
peffgitster
authored andcommitted
server_supports_v2(): use a separate function for die_on_error
The server_supports_v2() helper lets a caller find out if the server supports a feature, and will optionally die if it's not supported. This makes the return value confusing, as it's only meaningful when the function is not asked to die. Coverity flagged a new call like: /* check that we support "foo" */ server_supports_v2("foo", 1); complaining that we usually checked the return value, but this time we didn't. But this call is correct, and other ones that did: if (server_supports_v2("foo", 1)) do_something_with_foo(); are "wrong", in the sense that we know the conditional will always be true (but there's no bug; the code is simply misleading). Let's split the "die" behavior into its own function which returns void, and modify each caller to use the correct one. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8706a59 commit a31cfe3

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

connect.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static NORETURN void die_initial_contact(int unexpected)
6666
}
6767

6868
/* Checks if the server supports the capability 'c' */
69-
int server_supports_v2(const char *c, int die_on_error)
69+
int server_supports_v2(const char *c)
7070
{
7171
int i;
7272

@@ -76,11 +76,13 @@ int server_supports_v2(const char *c, int die_on_error)
7676
(!*out || *out == '='))
7777
return 1;
7878
}
79+
return 0;
80+
}
7981

80-
if (die_on_error)
82+
void ensure_server_supports_v2(const char *c)
83+
{
84+
if (!server_supports_v2(c))
8185
die(_("server doesn't support '%s'"), c);
82-
83-
return 0;
8486
}
8587

8688
int server_feature_v2(const char *c, const char **v)
@@ -477,7 +479,7 @@ static void send_capabilities(int fd_out, struct packet_reader *reader)
477479
{
478480
const char *hash_name;
479481

480-
if (server_supports_v2("agent", 0))
482+
if (server_supports_v2("agent"))
481483
packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
482484

483485
if (server_feature_v2("object-format", &hash_name)) {
@@ -504,17 +506,18 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
504506
&transport_options->unborn_head_target : NULL;
505507
*list = NULL;
506508

507-
if (server_supports_v2("ls-refs", 1))
508-
packet_write_fmt(fd_out, "command=ls-refs\n");
509+
ensure_server_supports_v2("ls-refs");
510+
packet_write_fmt(fd_out, "command=ls-refs\n");
509511

510512
/* Send capabilities */
511513
send_capabilities(fd_out, reader);
512514

513-
if (server_options && server_options->nr &&
514-
server_supports_v2("server-option", 1))
515+
if (server_options && server_options->nr) {
516+
ensure_server_supports_v2("server-option");
515517
for (i = 0; i < server_options->nr; i++)
516518
packet_write_fmt(fd_out, "server-option=%s",
517519
server_options->items[i].string);
520+
}
518521

519522
packet_delim(fd_out);
520523
/* When pushing we don't want to request the peeled tags */

connect.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ enum protocol_version discover_version(struct packet_reader *reader);
2020

2121
int server_supports_hash(const char *desired, int *feature_supported);
2222
const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp, int *offset);
23-
int server_supports_v2(const char *c, int die_on_error);
23+
int server_supports_v2(const char *c);
24+
void ensure_server_supports_v2(const char *c);
2425
int server_feature_v2(const char *c, const char **v);
2526
int server_supports_feature(const char *c, const char *feature,
2627
int die_on_error);

fetch-pack.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,15 +1317,15 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
13171317
{
13181318
const char *hash_name;
13191319

1320-
if (server_supports_v2("fetch", 1))
1321-
packet_buf_write(req_buf, "command=fetch");
1322-
if (server_supports_v2("agent", 0))
1320+
ensure_server_supports_v2("fetch");
1321+
packet_buf_write(req_buf, "command=fetch");
1322+
if (server_supports_v2("agent"))
13231323
packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1324-
if (advertise_sid && server_supports_v2("session-id", 0))
1324+
if (advertise_sid && server_supports_v2("session-id"))
13251325
packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1326-
if (server_options && server_options->nr &&
1327-
server_supports_v2("server-option", 1)) {
1326+
if (server_options && server_options->nr) {
13281327
int i;
1328+
ensure_server_supports_v2("server-option");
13291329
for (i = 0; i < server_options->nr; i++)
13301330
packet_buf_write(req_buf, "server-option=%s",
13311331
server_options->items[i].string);

0 commit comments

Comments
 (0)