Skip to content

Error in successCallBack when a header is passed in to LLHttpRequest #7

@abdiel-mireles

Description

@abdiel-mireles

Hi,

We noticed an issue when getting an API response back. The response could be truncated in certain cases and nowhere near our buffer limit. When we investigated we found that the length of the response copied happened to always match the length of the header. Upon closer inspection it seems like the incorrect value is being used as the length for the memcpy from the raw response to the user specified location (pResponse) in the code below.

I think what was meant to happen is the copy length would be the min between the length of the content and the length of the user specified buffer. However, what is actually happening is that the length of the header gets used and most of the response is lost.

This snippet of code is from https://github.com/loupeteam/LLHttp/blob/main/src/Ar/LLHttp/HttpRequest.c line 25.

void successCallback(LLHttpRequest_typ* t, LLHttpServiceLink_typ* api, LLHttpHeader_typ* header, unsigned long content) {
	if(t) {
		t->internal.error = 0;
		t->internal.done  = 1;
		t->internal.busy = 0;
		
		if(header) {
			memcpy(&t->header, header, sizeof(t->header));
			t->contentLength = t->header.contentLength;
			if(content) {
				unsigned long length = MIN(t->header.contentLength, t->responseSize);
				if(t->pResponse) memcpy((void*)t->pResponse, (void*)content, length);
			}
		}
	}
}

If this turns out to be a real issue, then a more sophisticated fix will be necessary. For now, we simply replaced length on the last line of code in this snippet with t->responseSize (which is the user specified length of response buffer). Slightly wasteful, but it works.

void successCallback(LLHttpRequest_typ* t, LLHttpServiceLink_typ* api, LLHttpHeader_typ* header, unsigned long content) {
	if(t) {
		t->internal.error = 0;
		t->internal.done  = 1;
		t->internal.busy = 0;
		
		if(header) {
			memcpy(&t->header, header, sizeof(t->header));
			//t->contentLength = t->header.contentLength;
			if(content) {
				//unsigned long length = MIN(t->header.contentLength, t->responseSize);
				if(t->pResponse) memcpy((void*)t->pResponse, (void*)content, t->responseSize);
			}
		}
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions