Skip to content

Commit 108a34c

Browse files
authored
[+] Fix: Enhance request parsing to avoid hq parsing error (#520)
* [+] Fix: Enhance request parsing to avoid hq parsing error * [~] Chore: Simplify variable declarations in request parsing function
1 parent 79a8a2b commit 108a34c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

demo/xqc_hq_request.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,19 @@ ssize_t
231231
xqc_hq_parse_req(xqc_hq_request_t *hqr, char *res, size_t sz, uint8_t *fin)
232232
{
233233
char method[16] = {0};
234-
int ret = sscanf(hqr->req_recv_buf, "%s %s", method, res);
234+
char fmt[32] = {0};
235+
size_t method_cap = sizeof(method) - 1;
236+
237+
238+
if (sz <= 1) {
239+
PRINT_LOG("|invalid resource buffer size|sz:%zu|", sz);
240+
return -XQC_EPROTO;
241+
}
242+
243+
size_t res_cap = sz - 1;
244+
snprintf(fmt, sizeof(fmt), "%%%zus %%%zus", method_cap, res_cap);
245+
246+
int ret = sscanf((char *)hqr->req_recv_buf, fmt, method, res);
235247
if (ret <= 0) {
236248
PRINT_LOG("|parse hq request failed: %s", hqr->req_recv_buf);
237249
return -XQC_EPROTO;
@@ -284,6 +296,12 @@ xqc_hq_request_recv_req(xqc_hq_request_t *hqr, char *res_buf, size_t buf_sz, uin
284296
} while (read > 0 && !hqr->fin);
285297

286298

299+
if (hqr->recv_cnt >= hqr->recv_buf_len) {
300+
PRINT_LOG("|hq request too long|len:%zu|", hqr->recv_cnt);
301+
return -XQC_EPROTO;
302+
}
303+
hqr->req_recv_buf[hqr->recv_cnt] = '\0';
304+
287305
if (NULL == hqr->resource_buf) {
288306
hqr->resource_buf = xqc_malloc(XQC_HQ_REQUEST_RESOURCE_MAX_LEN);
289307
if (NULL == hqr->resource_buf) {

0 commit comments

Comments
 (0)