Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e227b64

Browse files
committed
smart: don't access a NULL pkt pointer in strange cases
By clarifying what detect_caps returns on empty/missing packet, we can be sure there are actually refs to process. The old code would blindly dereference first, which might not have been sent at all. Reported by Coverity, CID 1393614
1 parent 2b5b3eb commit e227b64

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/transports/smart.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,20 +274,25 @@ static int git_smart__connect(
274274
first = (git_pkt_ref *)pkt;
275275

276276
/* Detect capabilities */
277-
if (git_smart__detect_caps(&symrefs, first, &t->caps) < 0)
278-
return -1;
279-
280-
/* If the only ref in the list is capabilities^{} with OID_ZERO, remove it */
281-
if (1 == t->refs.length && !strcmp(first->head.name, "capabilities^{}") &&
282-
git_oid_iszero(&first->head.oid)) {
283-
git_vector_clear(&t->refs);
284-
git_pkt_free((git_pkt *)first);
285-
}
277+
if (git_smart__detect_caps(&symrefs, first, &t->caps) == 0) {
278+
/* If the only ref in the list is capabilities^{} with OID_ZERO, remove it */
279+
if (1 == t->refs.length && !strcmp(first->head.name, "capabilities^{}") &&
280+
git_oid_iszero(&first->head.oid)) {
281+
git_vector_clear(&t->refs);
282+
git_pkt_free((git_pkt *)first);
283+
}
286284

287-
/* Keep a list of heads for _ls */
288-
git_smart__update_heads(t, &symrefs);
285+
/* Keep a list of heads for _ls */
286+
git_smart__update_heads(t, &symrefs);
289287

290-
free_symrefs(&symrefs);
288+
free_symrefs(&symrefs);
289+
} else if (error == GIT_ENOTFOUND) {
290+
/* There was no ref packet received, or the cap list was empty */
291+
error = 0;
292+
} else {
293+
giterr_set(GITERR_NET, "invalid response");
294+
return -1;
295+
}
291296

292297
if (t->rpc && git_smart__reset_stream(t, false) < 0)
293298
return -1;

src/transports/smart_protocol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ int git_smart__detect_caps(git_vector *out_symrefs, git_pkt_ref *pkt, transport_
137137

138138
/* No refs or capabilites, odd but not a problem */
139139
if (pkt == NULL || pkt->capabilities == NULL)
140-
return 0;
140+
return GIT_ENOTFOUND;
141141

142142
if (git_vector_init(&symrefs, 1, NULL) < 0)
143143
return -1;

0 commit comments

Comments
 (0)