1
0
Fork 0

[ecmascript] Avoid strlen

This commit is contained in:
Witold Filipczyk 2023-01-30 15:59:43 +01:00
parent be40c1bb87
commit c8aba2ef8c
3 changed files with 12 additions and 17 deletions

View File

@ -145,6 +145,7 @@ struct mjs_xhr {
int method;
int status;
int timeout;
size_t responseLength;
unsigned short readyState;
};
@ -516,6 +517,7 @@ mjs_xhr_open(js_State *J)
xhr->responseHeaders.clear();
mem_free_set(&xhr->response, NULL);
mem_free_set(&xhr->responseText, NULL);
xhr->responseLength = 0;
if (xhr->readyState != OPENED) {
xhr->readyState = OPENED;
@ -782,11 +784,7 @@ write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
struct mjs_xhr *xhr = (mjs_xhr *)stream;
size_t length = 0;
if (xhr->responseText) {
length = strlen(xhr->responseText);
}
size_t length = xhr->responseLength;
char *n = (char *)mem_realloc(xhr->responseText, length + size * nmemb + 1);
@ -797,6 +795,7 @@ write_data(void *ptr, size_t size, size_t nmemb, void *stream)
}
memcpy(xhr->responseText + length, ptr, (size * nmemb));
xhr->responseText[length + size * nmemb] = '\0';
xhr->responseLength += size * nmemb;
return nmemb;
}

View File

@ -230,6 +230,7 @@ typedef struct {
int status;
char *status_text;
char *response_text;
size_t response_length;
struct {
JSValue url;
@ -1192,6 +1193,7 @@ xhr_open(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
x->events[i] = JS_UNDEFINED;
}
mem_free_set(&x->response_text, NULL);
x->response_length = 0;
}
if (x->ready_state < XHR_RSTATE_OPENED) {
@ -1317,12 +1319,7 @@ static size_t
write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
Xhr *x = (Xhr *)stream;
size_t length = 0;
if (x->response_text) {
length = strlen(x->response_text);
}
size_t length = x->response_length;
char *n = (char *)mem_realloc(x->response_text, length + size * nmemb + 1);
@ -1333,6 +1330,7 @@ write_data(void *ptr, size_t size, size_t nmemb, void *stream)
}
memcpy(x->response_text + length, ptr, (size * nmemb));
x->response_text[length + size * nmemb] = '\0';
x->response_length += size * nmemb;
return nmemb;
}

View File

@ -153,6 +153,7 @@ struct xhr {
int status;
int timeout;
unsigned short readyState;
size_t responseLength;
};
static void onload_run(void *data);
@ -678,6 +679,7 @@ xhr_open(JSContext *ctx, unsigned int argc, JS::Value *rval)
xhr->responseHeaders.clear();
mem_free_set(&xhr->response, NULL);
mem_free_set(&xhr->responseText, NULL);
xhr->responseLength = 0;
if (xhr->readyState != OPENED) {
xhr->readyState = OPENED;
@ -934,12 +936,7 @@ static size_t
write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
struct xhr *xhr = (struct xhr *)stream;
size_t length = 0;
if (xhr->responseText) {
length = strlen(xhr->responseText);
}
size_t length = xhr->responseLength;
char *n = (char *)mem_realloc(xhr->responseText, length + size * nmemb + 1);
if (n) {
@ -949,6 +946,7 @@ write_data(void *ptr, size_t size, size_t nmemb, void *stream)
}
memcpy(xhr->responseText + length, ptr, (size * nmemb));
xhr->responseText[length + size * nmemb] = '\0';
xhr->responseLength += size * nmemb;
return nmemb;
}