forked from Telodendria/Telodendria
Fix leak in HttpClient.
This commit is contained in:
parent
4cc876eb10
commit
a4f369a0a9
4 changed files with 13 additions and 95 deletions
|
@ -186,7 +186,7 @@ HttpRequestSendHeaders(HttpClientContext * context)
|
|||
HttpStatus
|
||||
HttpRequestSend(HttpClientContext * context)
|
||||
{
|
||||
HttpStatus status;
|
||||
HttpStatus status = HTTP_STATUS_UNKNOWN;
|
||||
|
||||
char *line = NULL;
|
||||
ssize_t lineLen;
|
||||
|
@ -195,7 +195,7 @@ HttpRequestSend(HttpClientContext * context)
|
|||
|
||||
if (!context)
|
||||
{
|
||||
return 0;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
StreamFlush(context->stream);
|
||||
|
@ -210,19 +210,19 @@ HttpRequestSend(HttpClientContext * context)
|
|||
|
||||
if (lineLen == -1)
|
||||
{
|
||||
return 0;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
/* Line must contain at least "HTTP/x.x xxx" */
|
||||
if (lineLen < 12)
|
||||
{
|
||||
return 0;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
if (!(strncmp(line, "HTTP/1.0", 8) == 0 ||
|
||||
strncmp(line, "HTTP/1.1", 8) == 0))
|
||||
{
|
||||
return 0;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
tmp = line + 9;
|
||||
|
@ -234,22 +234,26 @@ HttpRequestSend(HttpClientContext * context)
|
|||
|
||||
if (!*tmp)
|
||||
{
|
||||
return 0;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
status = atoi(tmp);
|
||||
|
||||
if (!status)
|
||||
{
|
||||
return 0;
|
||||
status = HTTP_STATUS_UNKNOWN;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
context->responseHeaders = HttpParseHeaders(context->stream);
|
||||
if (!context->responseHeaders)
|
||||
{
|
||||
return 0;
|
||||
status = HTTP_STATUS_UNKNOWN;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
finish:
|
||||
Free(line);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
|
3
TODO.txt
3
TODO.txt
|
@ -20,7 +20,7 @@ Milestone: v0.3.0
|
|||
[ ] Directory
|
||||
|
||||
[ ] Fix leaks in http, json
|
||||
[ ] Remove TelodendriaGenerateMemReport()
|
||||
[x] Remove TelodendriaGenerateMemReport()
|
||||
|
||||
[ ] Cytoplasm
|
||||
[ ] Debug OpenSSL
|
||||
|
@ -28,7 +28,6 @@ Milestone: v0.3.0
|
|||
[x] hdoc(1) and hdoc(5)
|
||||
[x] Fix memory leaks in hdoc
|
||||
[x] Detect memory write out of bounds
|
||||
[ ] Memory check before reallocate, free
|
||||
[ ] Add a sane default memory hook
|
||||
|
||||
Milestone: v0.4.0
|
||||
|
|
|
@ -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(¤tTime);
|
||||
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
|
||||
TelodendriaPrintHeader(void)
|
||||
{
|
||||
|
|
|
@ -88,17 +88,6 @@ TelodendriaHeader[TELODENDRIA_HEADER_HEIGHT][TELODENDRIA_HEADER_WIDTH];
|
|||
*/
|
||||
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,
|
||||
* and the version number, out to the global log.
|
||||
|
|
Loading…
Reference in a new issue