Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

segfault when xhr is reused without any headers #672

Closed
junsik opened this issue Dec 2, 2024 · 1 comment · Fixed by #673
Closed

segfault when xhr is reused without any headers #672

junsik opened this issue Dec 2, 2024 · 1 comment · Fixed by #673

Comments

@junsik
Copy link

junsik commented Dec 2, 2024

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 {
saghul added a commit that referenced this issue Dec 4, 2024
@saghul
Copy link
Owner

saghul commented Dec 4, 2024

Your fix is sound, I applied it in #673

saghul added a commit that referenced this issue Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants