Fix leak in HttpClient.

This commit is contained in:
Jordan Bancino 2023-05-21 13:55:49 +00:00
parent 4cc876eb10
commit a4f369a0a9
4 changed files with 13 additions and 95 deletions

View file

@ -186,7 +186,7 @@ HttpRequestSendHeaders(HttpClientContext * context)
HttpStatus HttpStatus
HttpRequestSend(HttpClientContext * context) HttpRequestSend(HttpClientContext * context)
{ {
HttpStatus status; HttpStatus status = HTTP_STATUS_UNKNOWN;
char *line = NULL; char *line = NULL;
ssize_t lineLen; ssize_t lineLen;
@ -195,7 +195,7 @@ HttpRequestSend(HttpClientContext * context)
if (!context) if (!context)
{ {
return 0; goto finish;
} }
StreamFlush(context->stream); StreamFlush(context->stream);
@ -210,19 +210,19 @@ HttpRequestSend(HttpClientContext * context)
if (lineLen == -1) if (lineLen == -1)
{ {
return 0; goto finish;
} }
/* Line must contain at least "HTTP/x.x xxx" */ /* Line must contain at least "HTTP/x.x xxx" */
if (lineLen < 12) if (lineLen < 12)
{ {
return 0; goto finish;
} }
if (!(strncmp(line, "HTTP/1.0", 8) == 0 || if (!(strncmp(line, "HTTP/1.0", 8) == 0 ||
strncmp(line, "HTTP/1.1", 8) == 0)) strncmp(line, "HTTP/1.1", 8) == 0))
{ {
return 0; goto finish;
} }
tmp = line + 9; tmp = line + 9;
@ -234,22 +234,26 @@ HttpRequestSend(HttpClientContext * context)
if (!*tmp) if (!*tmp)
{ {
return 0; goto finish;
} }
status = atoi(tmp); status = atoi(tmp);
if (!status) if (!status)
{ {
return 0; status = HTTP_STATUS_UNKNOWN;
goto finish;
} }
context->responseHeaders = HttpParseHeaders(context->stream); context->responseHeaders = HttpParseHeaders(context->stream);
if (!context->responseHeaders) if (!context->responseHeaders)
{ {
return 0; status = HTTP_STATUS_UNKNOWN;
goto finish;
} }
finish:
Free(line);
return status; return status;
} }

View file

@ -20,7 +20,7 @@ Milestone: v0.3.0
[ ] Directory [ ] Directory
[ ] Fix leaks in http, json [ ] Fix leaks in http, json
[ ] Remove TelodendriaGenerateMemReport() [x] Remove TelodendriaGenerateMemReport()
[ ] Cytoplasm [ ] Cytoplasm
[ ] Debug OpenSSL [ ] Debug OpenSSL
@ -28,7 +28,6 @@ Milestone: v0.3.0
[x] hdoc(1) and hdoc(5) [x] hdoc(1) and hdoc(5)
[x] Fix memory leaks in hdoc [x] Fix memory leaks in hdoc
[x] Detect memory write out of bounds [x] Detect memory write out of bounds
[ ] Memory check before reallocate, free
[ ] Add a sane default memory hook [ ] Add a sane default memory hook
Milestone: v0.4.0 Milestone: v0.4.0

View file

@ -115,80 +115,6 @@ TelodendriaMemoryHook(MemoryAction a, MemoryInfo * i, void *args)
} }
} }
static void
HexDump(size_t off, char *hexBuf, char *asciiBuf, void *args)
{
FILE *report = args;
if (hexBuf && asciiBuf)
{
fprintf(report, "%04lx: %s | %s |\n", off, hexBuf, asciiBuf);
}
else
{
fprintf(report, "%04lx\n", off);
}
}
static void
MemoryIterator(MemoryInfo * i, void *args)
{
FILE *report = args;
fprintf(report, "%s:%d: %lu bytes at %p\n",
MemoryInfoGetFile(i), MemoryInfoGetLine(i),
MemoryInfoGetSize(i), MemoryInfoGetPointer(i));
MemoryHexDump(i, HexDump, report);
fprintf(report, "\n");
}
void
TelodendriaGenerateMemReport(void)
{
static const char *reportName = "Memory.txt";
/*
* Use C standard IO instead of the Stream or Io APIs, because
* those use the Memory API, and that's exactly what we're trying
* to assess, so using it would generate false positives. None of
* this code should leak memory.
*/
FILE *report;
time_t currentTime;
struct tm *timeInfo;
char tsBuffer[1024];
if (!MemoryAllocated())
{
/* No memory leaked, no need to write the report. This is the
* ideal situation; we only want the report to show up if leaks
* occurred. */
return;
}
report = fopen(reportName, "a");
if (!report)
{
return;
}
currentTime = time(NULL);
timeInfo = localtime(&currentTime);
strftime(tsBuffer, sizeof(tsBuffer), "%c", timeInfo);
fprintf(report, "---------- Telodendria Memory Report ----------\n");
fprintf(report, "Date: %s\n", tsBuffer);
fprintf(report, "Total Bytes: %lu\n", MemoryAllocated());
fprintf(report, "\n");
MemoryIterate(MemoryIterator, report);
fclose(report);
}
void void
TelodendriaPrintHeader(void) TelodendriaPrintHeader(void)
{ {

View file

@ -88,17 +88,6 @@ TelodendriaHeader[TELODENDRIA_HEADER_HEIGHT][TELODENDRIA_HEADER_WIDTH];
*/ */
extern void TelodendriaMemoryHook(MemoryAction, MemoryInfo *, void *); extern void TelodendriaMemoryHook(MemoryAction, MemoryInfo *, void *);
/**
* Generate a memory report in the current working directory. This
* function is intended to be called after all memory has supposedly
* been freed. It allocates no new memory of its own (except what is
* required by FILE pointers) and simply dumps all of the allocated
* memory out to a file if there is any memory allocated.
* .Pp
* This function is used to detect and fix memory leaks.
*/
extern void TelodendriaGenerateMemReport(void);
/** /**
* Print the logo and header, along with the copyright year and holder, * Print the logo and header, along with the copyright year and holder,
* and the version number, out to the global log. * and the version number, out to the global log.