Skip to content

Commit f4b7d50

Browse files
committed
BUG/MINOR: fixed saving of HTTP headers data in struct mirror
Sometimes, the header list was not initialized, so when the memory was freed in the mir_ptr_free() function, the program crashed because the list pointer had NULL content. This should probably solve the GitHub issue #12. Version of the program changed to v1.2.13.
1 parent 777aa34 commit f4b7d50

File tree

5 files changed

+30
-31
lines changed

5 files changed

+30
-31
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.12
1+
1.2.13

include/types/spoa-message.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct mirror {
4141
char *method; /* */
4242
int request_method; /* */
4343
char *version; /* */
44-
struct list *hdrs; /* */
44+
struct list hdrs; /* */
4545
char *body; /* */
4646
size_t body_head; /* */
4747
size_t body_size; /* */

src/.build-counter

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2415
1+
2432

src/curl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ static CURLcode mir_curl_set_headers(struct curl_con *con, const struct mirror *
475475
if (_NULL(con) || _NULL(mir))
476476
return retval;
477477

478-
list_for_each_entry_safe(hdr, hdr_back, mir->hdrs, list) {
478+
list_for_each_entry_safe(hdr, hdr_back, &(mir->hdrs), list) {
479479
slist = curl_slist_append(con->hdrs, (const char *)hdr->ptr);
480480
if (_NULL(slist)) {
481481
retval = CURLE_OUT_OF_MEMORY;
@@ -930,7 +930,7 @@ int mir_curl_add(struct curl_data *curl, struct mirror *mir)
930930
if (_NULL(curl) || _NULL(mir))
931931
return retval;
932932

933-
CURL_DBG("Adding mirror { \"%s\" \"%s\" \"%s\" %d \"%s\" %p %p %zu/%zu }", mir->url, mir->path, mir->method, mir->request_method, mir->version, mir->hdrs, mir->body, mir->body_head, mir->body_size);
933+
CURL_DBG("Adding mirror { \"%s\" \"%s\" \"%s\" %d \"%s\" { %p %p } %p %zu/%zu }", mir->url, mir->path, mir->method, mir->request_method, mir->version, mir->hdrs.p, mir->hdrs.n, mir->body, mir->body_head, mir->body_size);
934934

935935
con_timeout_ms = CLAMP_VALUE(con_timeout_ms, CURL_CON_TMOUT_MIN, CURL_CON_TMOUT_MAX);
936936
timeout_ms = CLAMP_VALUE(timeout_ms, CURL_TMOUT_MIN, CURL_TMOUT_MAX);

src/spoa-message.c

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -245,55 +245,50 @@ int spoa_msg_test(struct spoe_frame *frame, const char **buf, const char *end)
245245
* frame -
246246
* buf -
247247
* end -
248+
* hdrs -
248249
*
249250
* DESCRIPTION
250251
* -
251252
*
252253
* RETURN VALUE
253254
* -
254255
*/
255-
static struct list *spoa_msg_arg_hdrs(struct spoe_frame *frame, const char *buf, const char *end)
256+
static int spoa_msg_arg_hdrs(struct spoe_frame *frame, const char *buf, const char *end, struct list *hdrs)
256257
{
257258
struct buffer *hdr = NULL, *hdr_back;
258-
struct list *retptr = NULL;
259259
const char *str;
260260
uint64_t len;
261-
int i, rc;
261+
int i, retval = FUNC_RET_ERROR;
262262

263-
DBG_FUNC(FW_PTR, "%p, %p, %p", frame, buf, end);
264-
265-
if (_NULL(retptr = calloc(1, sizeof(*retptr))))
266-
return retptr;
267-
268-
LIST_INIT(retptr);
263+
DBG_FUNC(FW_PTR, "%p, %p, %p, %p", frame, buf, end, hdrs);
269264

270265
/* Build the HTTP headers. */
271266
for (i = 0; buf < end; i++) {
272-
rc = spoe_decode(frame, &buf, end, SPOE_DEC_STR0, &str, &len, SPOE_DEC_END);
273-
if (_ERROR(rc))
267+
retval = spoe_decode(frame, &buf, end, SPOE_DEC_STR0, &str, &len, SPOE_DEC_END);
268+
if (_ERROR(retval))
274269
break;
275270

276271
F_DBG(SPOA, frame, "str[%d]: <%.*s>", i, (int)len, str);
277272

278273
if (i & 1) {
279274
if (_NULL(str)) {
280275
/* HTTP header has no value. */
281-
if (_ERROR(rc = buffer_grow(hdr, ";\0", 2)))
276+
if (_ERROR(retval = buffer_grow(hdr, ";\0", 2)))
282277
break;
283278
}
284-
else if (_ERROR(rc = buffer_grow_va(hdr, ": ", 2, str, len, "\0", 1, NULL))) {
279+
else if (_ERROR(retval = buffer_grow_va(hdr, ": ", 2, str, len, "\0", 1, NULL))) {
285280
break;
286281
}
287282

288283
F_DBG(SPOA, frame, "header[%d]: <%.*s>", i / 2, (int)hdr->len, hdr->ptr);
289-
LIST_ADDQ(retptr, &(hdr->list));
284+
LIST_ADDQ(hdrs, &(hdr->list));
290285
}
291286
else if (_NULL(str)) {
292287
if (buf != end) {
293288
/* HTTP header has no name. */
294289
f_log(frame, _E("HTTP header defined without a name"));
295290

296-
rc = FUNC_RET_ERROR;
291+
retval = FUNC_RET_ERROR;
297292
}
298293

299294
break;
@@ -304,16 +299,18 @@ static struct list *spoa_msg_arg_hdrs(struct spoe_frame *frame, const char *buf,
304299
}
305300

306301
/* In the case of a fault, the allocated memory is released. */
307-
if (_ERROR(rc) || _NULL(hdr)) {
302+
if (_ERROR(retval) || _NULL(hdr)) {
308303
buffer_ptr_free(&hdr);
309304

310-
list_for_each_entry_safe(hdr, hdr_back, retptr, list)
305+
list_for_each_entry_safe(hdr, hdr_back, hdrs, list) {
306+
LIST_DEL(&(hdr->list));
311307
buffer_ptr_free(&hdr);
308+
}
312309

313-
PTR_FREE(retptr);
310+
retval = FUNC_RET_ERROR;
314311
}
315312

316-
return retptr;
313+
return retval;
317314
}
318315

319316

@@ -349,6 +346,7 @@ int spoa_msg_mirror(struct spoe_frame *frame, const char **buf, const char *end)
349346

350347
return retval;
351348
}
349+
LIST_INIT(&(mir->hdrs));
352350

353351
retval = spoe_decode(frame, &ptr, end, SPOE_DEC_UINT8, &nbargs, SPOE_DEC_END);
354352
if (_nERROR(retval))
@@ -386,13 +384,13 @@ int spoa_msg_mirror(struct spoe_frame *frame, const char **buf, const char *end)
386384
F_DBG(SPOA, frame, "mirror[%d] name='%.*s' type=%hhu: <%s> <%s>", i, (int)len, str, type, str_hex(data.chk.ptr, data.chk.len), str_ctrl(data.chk.ptr, data.chk.len));
387385

388386
if ((len == STR_SIZE(SPOE_MSG_ARG_HDRS)) && (memcmp(str, STR_ADDRSIZE(SPOE_MSG_ARG_HDRS)) == 0)) {
389-
if (_nNULL(mir->hdrs)) {
387+
if (!LIST_ISEMPTY(&(mir->hdrs))) {
390388
f_log(frame, _E("arg[%d] '%.*s': Duplicated argument"), i, (int)len, str);
391389

392390
retval = FUNC_RET_ERROR;
393391
}
394-
else if (_NULL(mir->hdrs = spoa_msg_arg_hdrs(frame, data.chk.ptr, data.chk.ptr + data.chk.len - 1)))
395-
retval = FUNC_RET_ERROR;
392+
else
393+
retval = spoa_msg_arg_hdrs(frame, data.chk.ptr, data.chk.ptr + data.chk.len - 1, &(mir->hdrs));
396394
}
397395
else if ((len == STR_SIZE(SPOE_MSG_ARG_BODY)) && (memcmp(str, STR_ADDRSIZE(SPOE_MSG_ARG_BODY)) == 0)) {
398396
if (_NULL(spoa_msg_arg_dup(frame, i, str, len, &data, &(mir->body), &(mir->body_size), "allocate memory for body")))
@@ -424,7 +422,7 @@ int spoa_msg_mirror(struct spoe_frame *frame, const char **buf, const char *end)
424422
f_log(frame, _E("HTTP request method not set"));
425423
else if (_NULL(mir->version))
426424
f_log(frame, _E("HTTP version not set"));
427-
else if (_NULL(mir->hdrs))
425+
else if (LIST_ISEMPTY(&(mir->hdrs)))
428426
f_log(frame, _E("HTTP headers not set"));
429427
else if (_NULL(mir->url = malloc(url_len)))
430428
f_log(frame, _E("Failed to allocate memory"));
@@ -488,18 +486,19 @@ void mir_ptr_free(struct mirror **data)
488486
if (_NULL(data) || _NULL(*data))
489487
return;
490488

491-
W_DBG(NOTICE, NULL, " freeing mirror { \"%s\" \"%s\" \"%s\" %d \"%s\" %p %p %zu/%zu }", (*data)->url, (*data)->path, (*data)->method, (*data)->request_method, (*data)->version, (*data)->hdrs, (*data)->body, (*data)->body_head, (*data)->body_size);
489+
W_DBG(NOTICE, NULL, " freeing mirror { \"%s\" \"%s\" \"%s\" %d \"%s\" { %p %p } %p %zu/%zu }", (*data)->url, (*data)->path, (*data)->method, (*data)->request_method, (*data)->version, (*data)->hdrs.p, (*data)->hdrs.n, (*data)->body, (*data)->body_head, (*data)->body_size);
492490

493491
PTR_FREE((*data)->out_address);
494492
PTR_FREE((*data)->url);
495493
PTR_FREE((*data)->path);
496494
PTR_FREE((*data)->method);
497495
PTR_FREE((*data)->version);
498496

499-
list_for_each_entry_safe(hdr, hdr_back, (*data)->hdrs, list)
497+
list_for_each_entry_safe(hdr, hdr_back, &((*data)->hdrs), list) {
498+
LIST_DEL(&(hdr->list));
500499
buffer_ptr_free(&hdr);
500+
}
501501

502-
PTR_FREE((*data)->hdrs);
503502
PTR_FREE((*data)->body);
504503
PTR_FREE(*data);
505504
}

0 commit comments

Comments
 (0)