Skip to content

Commit fdf9462

Browse files
committed
lib/fetch: fix some error paths and use strlcpy to be safe
1 parent 277afba commit fdf9462

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

lib/fetch/fetch.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,25 @@ fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
238238
}
239239
u->netrcfd = -1;
240240

241-
if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
242-
fetch_syserr();
243-
free(u);
244-
return (NULL);
241+
#define seturl(x) \
242+
if (strlcpy(u->x, x, sizeof(u->x)) >= sizeof(u->x)) { \
243+
url_seterr(URL_MALFORMED); \
244+
free(u); \
245+
return (NULL); \
245246
}
246-
247-
#define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
248247
seturl(scheme);
249248
seturl(host);
250249
seturl(user);
251250
seturl(pwd);
252251
#undef seturl
253252
u->port = port;
254253

254+
if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
255+
fetch_syserr();
256+
free(u);
257+
return (NULL);
258+
}
259+
255260
return (u);
256261
}
257262

@@ -442,6 +447,7 @@ fetchParseURL(const char *URL)
442447
return (u);
443448

444449
ouch:
450+
url_seterr(URL_MALFORMED);
445451
free(u);
446452
return (NULL);
447453
}

lib/fetch/http.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ http_fillbuf(struct httpio *io, size_t len)
241241
if (io->chunksize == 0) {
242242
switch (http_new_chunk(io)) {
243243
case -1:
244+
http_seterr(HTTP_PROTOCOL_ERROR);;
244245
io->error = EPROTO;
245246
return (-1);
246247
case 0:
@@ -492,26 +493,25 @@ http_get_reply(conn_t *conn, int *keep_alive)
492493
p = conn->buf + 4;
493494
if (*p == '/') {
494495
if (p[1] != '1' || p[2] != '.')
495-
return (HTTP_PROTOCOL_ERROR);
496+
goto ouch;
496497
if (p[3] == '1') {
497498
if (keep_alive)
498499
*keep_alive = 1;
499500
} else if (p[3] != '0')
500-
return (HTTP_PROTOCOL_ERROR);
501-
/* HTTP/1.1 defaults to the use of "persistent connections" */
502-
if (keep_alive && p[3] == '1') {
503-
*keep_alive = 1;
504-
}
501+
goto ouch;
505502
p += 4;
506503
}
507504
if (*p != ' ' ||
508505
!isdigit((unsigned char)p[1]) ||
509506
!isdigit((unsigned char)p[2]) ||
510507
!isdigit((unsigned char)p[3]))
511-
return (HTTP_PROTOCOL_ERROR);
508+
goto ouch;
512509

513510
conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
514511
return (conn->err);
512+
ouch:
513+
conn->err = HTTP_PROTOCOL_ERROR;
514+
return (HTTP_PROTOCOL_ERROR);
515515
}
516516

517517
/*
@@ -1541,7 +1541,7 @@ http_get_proxy(struct url * url, const char *flags)
15411541
if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
15421542
*p && (purl = fetchParseURL(p))) {
15431543
if (!*purl->scheme)
1544-
strcpy(purl->scheme, SCHEME_HTTP);
1544+
strlcpy(purl->scheme, SCHEME_HTTP, sizeof(purl->scheme));
15451545
if (!purl->port)
15461546
purl->port = fetch_default_proxy_port(purl->scheme);
15471547
if (strcmp(purl->scheme, SCHEME_HTTP) == 0)
@@ -1855,7 +1855,8 @@ http_request_body(struct url *URL, const char *op, struct url_stat *us,
18551855
*/
18561856
break;
18571857
case HTTP_PROTOCOL_ERROR:
1858-
/* fall through */
1858+
http_seterr(conn->err);
1859+
goto ouch;
18591860
case -1:
18601861
fetch_syserr();
18611862
goto ouch;
@@ -1930,8 +1931,8 @@ http_request_body(struct url *URL, const char *op, struct url_stat *us,
19301931
/* Only copy credentials if the host matches */
19311932
if (strcmp(new->host, url->host) == 0 &&
19321933
!*new->user && !*new->pwd) {
1933-
strcpy(new->user, url->user);
1934-
strcpy(new->pwd, url->pwd);
1934+
strlcpy(new->user, url->user, sizeof(new->user));
1935+
strlcpy(new->pwd, url->pwd, sizeof(new->pwd));
19351936
}
19361937
new->offset = url->offset;
19371938
new->length = url->length;

0 commit comments

Comments
 (0)