这是indexloc提供的服务,不要输入任何密码
Skip to content

segfault when xhr is reused without any headers #672

@junsik

Description

@junsik

When xhr is reused without any headers, CURLOPT_HTTPHEADER needs to points to NULL.
Othrewise, tjs will be segfault.

This is test case:

// tests/test-xhr-headers.js
import assert from 'tjs:assert';

const url = 'https://postman-echo.com/get';
const xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send();

xhr.open('POST', url, false);
xhr.send();  // <== segfault

The following is explicitly set to NULL when there is no header.
I am not sure this is curl bug or not.

diff --git a/src/xhr.c b/src/xhr.c
index 8171c90..291b604 100644
--- a/src/xhr.c
+++ b/src/xhr.c
@@ -744,9 +744,7 @@ static JSValue tjs_xhr_send(JSContext *ctx, JSValue this_val, int argc, JSValue
             curl_easy_setopt(x->curl_h, CURLOPT_POSTFIELDSIZE_LARGE, size);
             curl_easy_setopt(x->curl_h, CURLOPT_COPYPOSTFIELDS, buf);
         }
-        if (x->slist) {
-            curl_easy_setopt(x->curl_h, CURLOPT_HTTPHEADER, x->slist);
-        }
+        curl_easy_setopt(x->curl_h, CURLOPT_HTTPHEADER, x->slist);
         if (x->async) {
             curl_multi_add_handle(x->curlm_h, x->curl_h);
         } else {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions