Use new Memory API for all memory allocations.

This commit is contained in:
Jordan Bancino 2022-10-13 12:18:42 -04:00
parent 945acd1adf
commit 8cb86e8f67
14 changed files with 141 additions and 119 deletions

View file

@ -28,7 +28,7 @@
#endif #endif
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <Memory.h>
struct Array struct Array
{ {
@ -52,7 +52,7 @@ ArrayAdd(Array * array, void *value)
Array * Array *
ArrayCreate(void) ArrayCreate(void)
{ {
Array *array = malloc(sizeof(Array)); Array *array = Malloc(sizeof(Array));
if (!array) if (!array)
{ {
@ -61,11 +61,11 @@ ArrayCreate(void)
array->size = 0; array->size = 0;
array->allocated = ARRAY_BLOCK; array->allocated = ARRAY_BLOCK;
array->entries = malloc(sizeof(void *) * ARRAY_BLOCK); array->entries = Malloc(sizeof(void *) * ARRAY_BLOCK);
if (!array->entries) if (!array->entries)
{ {
free(array); Free(array);
return NULL; return NULL;
} }
@ -100,8 +100,8 @@ ArrayFree(Array * array)
{ {
if (array) if (array)
{ {
free(array->entries); Free(array->entries);
free(array); Free(array);
} }
} }
@ -139,7 +139,7 @@ ArrayInsert(Array * array, void *value, size_t index)
tmp = array->entries; tmp = array->entries;
array->entries = realloc(array->entries, array->entries = Realloc(array->entries,
sizeof(void *) * newSize); sizeof(void *) * newSize);
if (!array->entries) if (!array->entries)
@ -186,7 +186,7 @@ ArrayTrim(Array * array)
tmp = array->entries; tmp = array->entries;
array->entries = realloc(array->entries, array->entries = Realloc(array->entries,
sizeof(void *) * array->size); sizeof(void *) * array->size);
if (!array->entries) if (!array->entries)

View file

@ -23,7 +23,7 @@
*/ */
#include <Base64.h> #include <Base64.h>
#include <stdlib.h> #include <Memory.h>
static const char Base64EncodeMap[] = static const char Base64EncodeMap[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@ -94,7 +94,7 @@ Base64Encode(const char *input, size_t len)
} }
outLen = Base64EncodedSize(len); outLen = Base64EncodedSize(len);
out = malloc(outLen + 1); out = Malloc(outLen + 1);
if (!out) if (!out)
{ {
return NULL; return NULL;
@ -171,7 +171,7 @@ Base64Decode(const char *input, size_t len)
} }
} }
out = malloc(outLen + 1); out = Malloc(outLen + 1);
if (!out) if (!out)
{ {
return NULL; return NULL;
@ -226,7 +226,7 @@ Base64Pad(char **base64Ptr, size_t length)
newSize = length + (4 - (length % 4)); newSize = length + (4 - (length % 4));
tmp = realloc(*base64Ptr, newSize + 100);; tmp = Realloc(*base64Ptr, newSize + 100);;
if (!tmp) if (!tmp)
{ {
return 0; /* Memory error */ return 0; /* Memory error */

View file

@ -23,6 +23,8 @@
*/ */
#include <Config.h> #include <Config.h>
#include <Memory.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
@ -99,7 +101,7 @@ ConfigParseResultFree(ConfigParseResult * result)
* Note that if the parse was valid, the hash map * Note that if the parse was valid, the hash map
* needs to be freed separately. * needs to be freed separately.
*/ */
free(result); Free(result);
} }
Array * Array *
@ -126,14 +128,14 @@ ConfigDirectiveFree(ConfigDirective * directive)
for (i = 0; i < ArraySize(directive->values); i++) for (i = 0; i < ArraySize(directive->values); i++)
{ {
free(ArrayGet(directive->values, i)); Free(ArrayGet(directive->values, i));
} }
ArrayFree(directive->values); ArrayFree(directive->values);
ConfigFree(directive->children); ConfigFree(directive->children);
free(directive); Free(directive);
} }
void void
@ -145,7 +147,7 @@ ConfigFree(HashMap * conf)
while (HashMapIterate(conf, &key, &value)) while (HashMapIterate(conf, &key, &value))
{ {
ConfigDirectiveFree((ConfigDirective *) value); ConfigDirectiveFree((ConfigDirective *) value);
free(key); Free(key);
} }
HashMapFree(conf); HashMapFree(conf);
@ -154,7 +156,7 @@ ConfigFree(HashMap * conf)
static ConfigParserState * static ConfigParserState *
ConfigParserStateCreate(FILE * stream) ConfigParserStateCreate(FILE * stream)
{ {
ConfigParserState *state = malloc(sizeof(ConfigParserState)); ConfigParserState *state = Malloc(sizeof(ConfigParserState));
if (!state) if (!state)
{ {
@ -165,7 +167,7 @@ ConfigParserStateCreate(FILE * stream)
if (!state->macroMap) if (!state->macroMap)
{ {
free(state); Free(state);
return NULL; return NULL;
} }
@ -191,17 +193,17 @@ ConfigParserStateFree(ConfigParserState * state)
} }
free(state->token); Free(state->token);
while (HashMapIterate(state->macroMap, &key, &value)) while (HashMapIterate(state->macroMap, &key, &value))
{ {
free(key); Free(key);
free(value); Free(value);
} }
HashMapFree(state->macroMap); HashMapFree(state->macroMap);
free(state); Free(state);
} }
static int static int
@ -265,7 +267,7 @@ ConfigTokenSeek(ConfigParserState * state)
if (!state->token) if (!state->token)
{ {
state->tokenSize = CONFIG_BUFFER_BLOCK; state->tokenSize = CONFIG_BUFFER_BLOCK;
state->token = malloc(CONFIG_BUFFER_BLOCK); state->token = Malloc(CONFIG_BUFFER_BLOCK);
} }
state->token[state->tokenLen] = c; state->token[state->tokenLen] = c;
state->tokenLen++; state->tokenLen++;
@ -278,7 +280,7 @@ ConfigTokenSeek(ConfigParserState * state)
if (state->tokenLen >= state->tokenSize) if (state->tokenLen >= state->tokenSize)
{ {
state->tokenSize += CONFIG_BUFFER_BLOCK; state->tokenSize += CONFIG_BUFFER_BLOCK;
state->token = realloc(state->token, state->token = Realloc(state->token,
state->tokenSize); state->tokenSize);
} }
} }
@ -334,7 +336,7 @@ ConfigTokenSeek(ConfigParserState * state)
if (state->tokenLen >= state->tokenSize) if (state->tokenLen >= state->tokenSize)
{ {
state->tokenSize += CONFIG_BUFFER_BLOCK; state->tokenSize += CONFIG_BUFFER_BLOCK;
state->token = realloc(state->token, state->token = Realloc(state->token,
state->tokenSize); state->tokenSize);
} }
} }
@ -361,7 +363,7 @@ ConfigTokenSeek(ConfigParserState * state)
if (state->tokenLen >= state->tokenSize) if (state->tokenLen >= state->tokenSize)
{ {
state->tokenSize += CONFIG_BUFFER_BLOCK; state->tokenSize += CONFIG_BUFFER_BLOCK;
state->token = realloc(state->token, state->token = Realloc(state->token,
state->tokenSize); state->tokenSize);
} }
} }
@ -381,7 +383,7 @@ ConfigTokenSeek(ConfigParserState * state)
if (state->tokenLen) if (state->tokenLen)
{ {
state->tokenSize = state->tokenLen; state->tokenSize = state->tokenLen;
state->token = realloc(state->token, state->tokenSize); state->token = Realloc(state->token, state->tokenSize);
} }
} }
@ -401,7 +403,7 @@ ConfigParseBlock(ConfigParserState * state, int level)
while (ConfigExpect(state, TOKEN_NAME)) while (ConfigExpect(state, TOKEN_NAME))
{ {
char *name = malloc(state->tokenLen + 1); char *name = Malloc(state->tokenLen + 1);
strcpy(name, state->token); strcpy(name, state->token);
@ -410,7 +412,7 @@ ConfigParseBlock(ConfigParserState * state, int level)
{ {
ConfigDirective *directive; ConfigDirective *directive;
directive = malloc(sizeof(ConfigDirective)); directive = Malloc(sizeof(ConfigDirective));
directive->children = NULL; directive->children = NULL;
directive->values = ArrayCreate(); directive->values = ArrayCreate();
@ -440,7 +442,7 @@ ConfigParseBlock(ConfigParserState * state, int level)
/* dval is a pointer which is overwritten with the next /* dval is a pointer which is overwritten with the next
* token. */ * token. */
dvalCpy = malloc(strlen(dval) + 1); dvalCpy = Malloc(strlen(dval) + 1);
strcpy(dvalCpy, dval); strcpy(dvalCpy, dval);
ArrayAdd(directive->values, dvalCpy); ArrayAdd(directive->values, dvalCpy);
@ -473,10 +475,10 @@ ConfigParseBlock(ConfigParserState * state, int level)
ConfigTokenSeek(state); ConfigTokenSeek(state);
if (ConfigExpect(state, TOKEN_VALUE)) if (ConfigExpect(state, TOKEN_VALUE))
{ {
char *valueCopy = malloc(strlen(state->token) + 1); char *valueCopy = Malloc(strlen(state->token) + 1);
strcpy(valueCopy, state->token); strcpy(valueCopy, state->token);
free(HashMapSet(state->macroMap, name, valueCopy)); Free(HashMapSet(state->macroMap, name, valueCopy));
ConfigTokenSeek(state); ConfigTokenSeek(state);
} }
else else
@ -522,7 +524,7 @@ ConfigParse(FILE * stream)
HashMap *conf; HashMap *conf;
ConfigParserState *state; ConfigParserState *state;
result = malloc(sizeof(ConfigParseResult)); result = Malloc(sizeof(ConfigParseResult));
state = ConfigParserStateCreate(stream); state = ConfigParserStateCreate(stream);
conf = ConfigParseBlock(state, 0); conf = ConfigParseBlock(state, 0);

View file

@ -23,8 +23,10 @@
*/ */
#include <HashMap.h> #include <HashMap.h>
#include <Memory.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <string.h>
typedef struct HashMapBucket typedef struct HashMapBucket
{ {
@ -77,12 +79,14 @@ HashMapGrow(HashMap * map)
oldCapacity = map->capacity; oldCapacity = map->capacity;
map->capacity *= 2; map->capacity *= 2;
newEntries = calloc(map->capacity, sizeof(HashMapBucket *)); newEntries = Malloc(map->capacity * sizeof(HashMapBucket *));
if (!newEntries) if (!newEntries)
{ {
return 0; return 0;
} }
memset(&newEntries, 0, map->capacity * sizeof(HashMapBucket *));
for (i = 0; i < oldCapacity; i++) for (i = 0; i < oldCapacity; i++)
{ {
/* If there is a value here, and it isn't a tombstone */ /* If there is a value here, and it isn't a tombstone */
@ -97,7 +101,7 @@ HashMapGrow(HashMap * map)
{ {
if (!newEntries[index]->hash) if (!newEntries[index]->hash)
{ {
free(newEntries[index]); Free(newEntries[index]);
newEntries[index] = map->entries[i]; newEntries[index] = map->entries[i];
break; break;
} }
@ -114,11 +118,11 @@ HashMapGrow(HashMap * map)
else else
{ {
/* Either NULL or a tombstone */ /* Either NULL or a tombstone */
free(map->entries[i]); Free(map->entries[i]);
} }
} }
free(map->entries); Free(map->entries);
map->entries = newEntries; map->entries = newEntries;
return 1; return 1;
} }
@ -126,7 +130,7 @@ HashMapGrow(HashMap * map)
HashMap * HashMap *
HashMapCreate(void) HashMapCreate(void)
{ {
HashMap *map = malloc(sizeof(HashMap)); HashMap *map = Malloc(sizeof(HashMap));
if (!map) if (!map)
{ {
@ -139,13 +143,15 @@ HashMapCreate(void)
map->iterator = 0; map->iterator = 0;
map->hashFunc = HashMapHashKey; map->hashFunc = HashMapHashKey;
map->entries = calloc(map->capacity, sizeof(HashMapBucket *)); map->entries = Malloc(map->capacity * sizeof(HashMapBucket *));
if (!map->entries) if (!map->entries)
{ {
free(map); Free(map);
return NULL; return NULL;
} }
memset(map->entries, 0, map->capacity * sizeof(HashMapBucket *));
return map; return map;
} }
@ -195,11 +201,11 @@ HashMapFree(HashMap * map)
{ {
if (map->entries[i]) if (map->entries[i])
{ {
free(map->entries[i]); Free(map->entries[i]);
} }
} }
free(map->entries); Free(map->entries);
free(map); Free(map);
} }
} }
@ -318,7 +324,7 @@ HashMapSet(HashMap * map, char *key, void *value)
if (!bucket) if (!bucket)
{ {
bucket = malloc(sizeof(HashMapBucket)); bucket = Malloc(sizeof(HashMapBucket));
if (!bucket) if (!bucket)
{ {
break; break;
@ -359,11 +365,11 @@ HashMapIterateFree(char *key, void *value)
{ {
if (key) if (key)
{ {
free(key); Free(key);
} }
if (value) if (value)
{ {
free(value); Free(value);
} }
} }

View file

@ -27,6 +27,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <Memory.h>
#include <Constants.h> #include <Constants.h>
#include <HashMap.h> #include <HashMap.h>
@ -231,7 +232,7 @@ HttpUrlEncode(char *str)
size = TELODENDRIA_STRING_CHUNK; size = TELODENDRIA_STRING_CHUNK;
len = 0; len = 0;
encoded = malloc(size); encoded = Malloc(size);
if (!encoded) if (!encoded)
{ {
return NULL; return NULL;
@ -246,10 +247,10 @@ HttpUrlEncode(char *str)
char *tmp; char *tmp;
size += TELODENDRIA_STRING_CHUNK; size += TELODENDRIA_STRING_CHUNK;
tmp = realloc(encoded, size); tmp = Realloc(encoded, size);
if (!tmp) if (!tmp)
{ {
free(encoded); Free(encoded);
return NULL; return NULL;
} }
@ -326,7 +327,7 @@ HttpUrlDecode(char *str)
i = 0; i = 0;
inputLen = strlen(str); inputLen = strlen(str);
decoded = malloc(inputLen); decoded = Malloc(inputLen);
if (!decoded) if (!decoded)
{ {
@ -346,7 +347,7 @@ HttpUrlDecode(char *str)
if (sscanf(str, "%2X", &d) != 1) if (sscanf(str, "%2X", &d) != 1)
{ {
/* Decoding error */ /* Decoding error */
free(decoded); Free(decoded);
return NULL; return NULL;
} }
@ -407,7 +408,7 @@ HttpParamEncode(HashMap * params, FILE * out)
fprintf(out, "%s=%s&", encKey, encVal); fprintf(out, "%s=%s&", encKey, encVal);
free(encKey); Free(encKey);
free(encVal); Free(encVal);
} }
} }

View file

@ -24,6 +24,7 @@
#include <NonPosix.h> #include <NonPosix.h>
#include <HttpServer.h> #include <HttpServer.h>
#include <Memory.h>
#include <Queue.h> #include <Queue.h>
#include <Array.h> #include <Array.h>
#include <Util.h> #include <Util.h>
@ -78,7 +79,7 @@ HttpServerContextCreate(HttpRequestMethod requestMethod,
{ {
HttpServerContext *c; HttpServerContext *c;
c = malloc(sizeof(HttpServerContext)); c = Malloc(sizeof(HttpServerContext));
if (!c) if (!c)
{ {
return NULL; return NULL;
@ -87,15 +88,15 @@ HttpServerContextCreate(HttpRequestMethod requestMethod,
c->requestHeaders = HashMapCreate(); c->requestHeaders = HashMapCreate();
if (!c->requestHeaders) if (!c->requestHeaders)
{ {
free(c); Free(c);
return NULL; return NULL;
} }
c->responseHeaders = HashMapCreate(); c->responseHeaders = HashMapCreate();
if (!c->responseHeaders) if (!c->responseHeaders)
{ {
free(c->requestHeaders); Free(c->requestHeaders);
free(c); Free(c);
return NULL; return NULL;
} }
@ -120,15 +121,15 @@ HttpServerContextFree(HttpServerContext * c)
while (HashMapIterate(c->requestHeaders, &key, &val)) while (HashMapIterate(c->requestHeaders, &key, &val))
{ {
free(key); Free(key);
free(val); Free(val);
} }
HashMapFree(c->requestHeaders); HashMapFree(c->requestHeaders);
/* It is up to the handler to free its values */ /* It is up to the handler to free its values */
HashMapFree(c->responseHeaders); HashMapFree(c->responseHeaders);
free(c->requestPath); Free(c->requestPath);
fclose(c->stream); fclose(c->stream);
} }
@ -277,12 +278,14 @@ HttpServerCreate(unsigned short port, unsigned int nThreads, unsigned int maxCon
return NULL; return NULL;
} }
server = calloc(1, sizeof(HttpServer)); server = Malloc(sizeof(HttpServer));
if (!server) if (!server)
{ {
goto error; goto error;
} }
memset(server, 0, sizeof(HttpServer));
server->threadPool = ArrayCreate(); server->threadPool = ArrayCreate();
if (!server->threadPool) if (!server->threadPool)
{ {
@ -350,7 +353,7 @@ error:
close(server->sd); close(server->sd);
} }
free(server); Free(server);
} }
return NULL; return NULL;
} }
@ -367,7 +370,7 @@ HttpServerFree(HttpServer * server)
QueueFree(server->connQueue); QueueFree(server->connQueue);
pthread_mutex_destroy(&server->connQueueMutex); pthread_mutex_destroy(&server->connQueueMutex);
ArrayFree(server->threadPool); ArrayFree(server->threadPool);
free(server); Free(server);
} }
static void * static void *
@ -440,7 +443,7 @@ HttpServerWorkerThread(void *args)
} }
} }
requestPath = malloc((i * sizeof(char)) + 1); requestPath = Malloc((i * sizeof(char)) + 1);
strcpy(requestPath, pathPtr); strcpy(requestPath, pathPtr);
requestProtocol = &pathPtr[i + 1]; requestProtocol = &pathPtr[i + 1];
@ -481,7 +484,7 @@ HttpServerWorkerThread(void *args)
line[i] = tolower(line[i]); line[i] = tolower(line[i]);
} }
headerKey = malloc((i * sizeof(char)) + 1); headerKey = Malloc((i * sizeof(char)) + 1);
if (!headerKey) if (!headerKey)
{ {
goto internal_error; goto internal_error;
@ -505,7 +508,7 @@ HttpServerWorkerThread(void *args)
line[i] = '\0'; line[i] = '\0';
} }
headerValue = malloc(strlen(headerPtr) + 1); headerValue = Malloc(strlen(headerPtr) + 1);
if (!headerValue) if (!headerValue)
{ {
goto internal_error; goto internal_error;
@ -532,7 +535,7 @@ bad_request:
goto finish; goto finish;
finish: finish:
free(line); Free(line);
fclose(fp); fclose(fp);
} }
@ -555,7 +558,7 @@ HttpServerEventThread(void *args)
for (i = 0; i < server->nThreads; i++) for (i = 0; i < server->nThreads; i++)
{ {
pthread_t *workerThread = malloc(sizeof(pthread_t)); pthread_t *workerThread = Malloc(sizeof(pthread_t));
if (!workerThread) if (!workerThread)
{ {
@ -604,7 +607,7 @@ HttpServerEventThread(void *args)
pthread_t *workerThread = ArrayGet(server->threadPool, i); pthread_t *workerThread = ArrayGet(server->threadPool, i);
pthread_join(*workerThread, NULL); pthread_join(*workerThread, NULL);
free(workerThread); Free(workerThread);
} }
while ((fp = DequeueConnection(server))) while ((fp = DequeueConnection(server)))

View file

@ -23,6 +23,7 @@
*/ */
#include <Json.h> #include <Json.h>
#include <Memory.h>
#include <Util.h> #include <Util.h>
#include <stdio.h> #include <stdio.h>
@ -85,7 +86,7 @@ JsonValueType(JsonValue * value)
static JsonValue * static JsonValue *
JsonValueAllocate(void) JsonValueAllocate(void)
{ {
return malloc(sizeof(JsonValue)); return Malloc(sizeof(JsonValue));
} }
JsonValue * JsonValue *
@ -315,13 +316,13 @@ JsonValueFree(JsonValue * value)
ArrayFree(arr); ArrayFree(arr);
break; break;
case JSON_STRING: case JSON_STRING:
free(value->as.string); Free(value->as.string);
break; break;
default: default:
break; break;
} }
free(value); Free(value);
} }
void void
@ -405,7 +406,7 @@ JsonDecodeString(FILE * in)
len = 0; len = 0;
allocated = strBlockSize; allocated = strBlockSize;
str = malloc(allocated * sizeof(char)); str = Malloc(allocated * sizeof(char));
if (!str) if (!str)
{ {
return NULL; return NULL;
@ -416,7 +417,7 @@ JsonDecodeString(FILE * in)
if (c <= 0x001F) if (c <= 0x001F)
{ {
/* Bad byte; these must be escaped */ /* Bad byte; these must be escaped */
free(str); Free(str);
return NULL; return NULL;
} }
@ -461,7 +462,7 @@ JsonDecodeString(FILE * in)
if (fscanf(in, "%04lx", &utf8) != 1) if (fscanf(in, "%04lx", &utf8) != 1)
{ {
/* Bad hex value */ /* Bad hex value */
free(str); Free(str);
return NULL; return NULL;
} }
@ -489,18 +490,18 @@ JsonDecodeString(FILE * in)
if (!utf8Ptr) if (!utf8Ptr)
{ {
/* Mem error */ /* Mem error */
free(str); Free(str);
return NULL; return NULL;
} }
/* Move the output of UtilUtf8Encode() into our /* Move the output of UtilUtf8Encode() into our
* local buffer */ * local buffer */
strcpy(a, utf8Ptr); strcpy(a, utf8Ptr);
free(utf8Ptr); Free(utf8Ptr);
break; break;
default: default:
/* Bad escape value */ /* Bad escape value */
free(str); Free(str);
return NULL; return NULL;
} }
break; break;
@ -519,10 +520,10 @@ JsonDecodeString(FILE * in)
char *tmp; char *tmp;
allocated += strBlockSize; allocated += strBlockSize;
tmp = realloc(str, allocated * sizeof(char)); tmp = Realloc(str, allocated * sizeof(char));
if (!tmp) if (!tmp)
{ {
free(str); Free(str);
return NULL; return NULL;
} }
@ -535,7 +536,7 @@ JsonDecodeString(FILE * in)
} }
} }
free(str); Free(str);
return NULL; return NULL;
} }
@ -673,7 +674,7 @@ JsonTokenSeek(JsonParserState * state)
if (state->token) if (state->token)
{ {
free(state->token); Free(state->token);
state->token = NULL; state->token = NULL;
} }
@ -713,7 +714,7 @@ JsonTokenSeek(JsonParserState * state)
size_t allocated = 16; size_t allocated = 16;
state->tokenLen = 1; state->tokenLen = 1;
state->token = malloc(allocated); state->token = Malloc(allocated);
if (!state->token) if (!state->token)
{ {
state->tokenType = TOKEN_EOF; state->tokenType = TOKEN_EOF;
@ -748,7 +749,7 @@ JsonTokenSeek(JsonParserState * state)
allocated += 16; allocated += 16;
tmp = realloc(state->token, allocated); tmp = Realloc(state->token, allocated);
if (!tmp) if (!tmp)
{ {
state->tokenType = TOKEN_EOF; state->tokenType = TOKEN_EOF;
@ -781,7 +782,7 @@ JsonTokenSeek(JsonParserState * state)
else else
{ {
state->tokenLen = 8; state->tokenLen = 8;
state->token = malloc(state->tokenLen); state->token = Malloc(state->tokenLen);
if (!state->token) if (!state->token)
{ {
state->tokenType = TOKEN_EOF; state->tokenType = TOKEN_EOF;
@ -875,7 +876,7 @@ JsonDecodeValue(JsonParserState * state)
value = JsonValueArray(JsonDecodeArray(state)); value = JsonValueArray(JsonDecodeArray(state));
break; break;
case TOKEN_STRING: case TOKEN_STRING:
strValue = malloc(state->tokenLen); strValue = Malloc(state->tokenLen);
if (!strValue) if (!strValue)
{ {
return NULL; return NULL;
@ -919,7 +920,7 @@ JsonDecodeObject(JsonParserState * state)
JsonTokenSeek(state); JsonTokenSeek(state);
if (JsonExpect(state, TOKEN_STRING)) if (JsonExpect(state, TOKEN_STRING))
{ {
char *key = malloc(state->tokenLen); char *key = Malloc(state->tokenLen);
JsonValue *value; JsonValue *value;
if (!key) if (!key)

View file

@ -23,7 +23,9 @@
*/ */
#include <Log.h> #include <Log.h>
#include <stdlib.h> #include <Memory.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <ctype.h> #include <ctype.h>
@ -48,13 +50,15 @@ LogConfigCreate(void)
{ {
LogConfig *config; LogConfig *config;
config = calloc(1, sizeof(LogConfig)); config = Malloc(sizeof(LogConfig));
if (!config) if (!config)
{ {
return NULL; return NULL;
} }
memset(config, 0, sizeof(LogConfig));
LogConfigLevelSet(config, LOG_MESSAGE); LogConfigLevelSet(config, LOG_MESSAGE);
LogConfigIndentSet(config, 0); LogConfigIndentSet(config, 0);
LogConfigOutputSet(config, NULL); /* Will set to stdout */ LogConfigOutputSet(config, NULL); /* Will set to stdout */
@ -106,7 +110,7 @@ LogConfigFree(LogConfig * config)
} }
fclose(config->out); fclose(config->out);
free(config); Free(config);
} }
void void

View file

@ -23,7 +23,7 @@
*/ */
#include <Queue.h> #include <Queue.h>
#include <stdlib.h> #include <Memory.h>
struct Queue struct Queue
{ {
@ -44,16 +44,16 @@ QueueCreate(size_t size)
return NULL; return NULL;
} }
q = malloc(sizeof(Queue)); q = Malloc(sizeof(Queue));
if (!q) if (!q)
{ {
return NULL; return NULL;
} }
q->items = malloc(size * sizeof(void *)); q->items = Malloc(size * sizeof(void *));
if (!q->items) if (!q->items)
{ {
free(q); Free(q);
return NULL; return NULL;
} }
@ -69,10 +69,10 @@ QueueFree(Queue * q)
{ {
if (q) if (q)
{ {
free(q->items); Free(q->items);
} }
free(q); Free(q);
} }
int int

View file

@ -24,8 +24,8 @@
#include <Routes.h> #include <Routes.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <Memory.h>
#include <Json.h> #include <Json.h>
#include <HashMap.h> #include <HashMap.h>
@ -38,19 +38,19 @@ ROUTE(RouteMatrix)
if (!MATRIX_PATH_EQUALS(pathPart, "client") || MATRIX_PATH_PARTS(path) != 1) if (!MATRIX_PATH_EQUALS(pathPart, "client") || MATRIX_PATH_PARTS(path) != 1)
{ {
free(pathPart); Free(pathPart);
HttpResponseStatus(context, HTTP_NOT_FOUND); HttpResponseStatus(context, HTTP_NOT_FOUND);
return MatrixErrorCreate(M_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND);
} }
free(pathPart); Free(pathPart);
pathPart = MATRIX_PATH_POP(path); pathPart = MATRIX_PATH_POP(path);
if (MATRIX_PATH_EQUALS(pathPart, "versions")) if (MATRIX_PATH_EQUALS(pathPart, "versions"))
{ {
Array *versions = ArrayCreate(); Array *versions = ArrayCreate();
free(pathPart); Free(pathPart);
ArrayAdd(versions, JsonValueString(UtilStringDuplicate("v1.4"))); ArrayAdd(versions, JsonValueString(UtilStringDuplicate("v1.4")));
@ -61,7 +61,7 @@ ROUTE(RouteMatrix)
} }
else else
{ {
free(pathPart); Free(pathPart);
HttpResponseStatus(context, HTTP_NOT_FOUND); HttpResponseStatus(context, HTTP_NOT_FOUND);
return MatrixErrorCreate(M_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND);
} }

View file

@ -24,8 +24,8 @@
#include <Routes.h> #include <Routes.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <Memory.h>
#include <Json.h> #include <Json.h>
#include <HashMap.h> #include <HashMap.h>
@ -36,19 +36,19 @@ ROUTE(RouteWellKnown)
if (!MATRIX_PATH_EQUALS(pathPart, "matrix") || MATRIX_PATH_PARTS(path) != 1) if (!MATRIX_PATH_EQUALS(pathPart, "matrix") || MATRIX_PATH_PARTS(path) != 1)
{ {
free(pathPart); Free(pathPart);
HttpResponseStatus(context, HTTP_NOT_FOUND); HttpResponseStatus(context, HTTP_NOT_FOUND);
return MatrixErrorCreate(M_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND);
} }
free(pathPart); Free(pathPart);
pathPart = MATRIX_PATH_POP(path); pathPart = MATRIX_PATH_POP(path);
if (MATRIX_PATH_EQUALS(pathPart, "client")) if (MATRIX_PATH_EQUALS(pathPart, "client"))
{ {
HashMap *homeserver = HashMapCreate(); HashMap *homeserver = HashMapCreate();
free(pathPart); Free(pathPart);
response = HashMapCreate(); response = HashMapCreate();
@ -67,7 +67,7 @@ ROUTE(RouteWellKnown)
} }
else else
{ {
free(pathPart); Free(pathPart);
HttpResponseStatus(context, HTTP_NOT_FOUND); HttpResponseStatus(context, HTTP_NOT_FOUND);
return MatrixErrorCreate(M_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND);
} }

View file

@ -22,6 +22,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include <TelodendriaConfig.h> #include <TelodendriaConfig.h>
#include <Memory.h>
#include <Config.h> #include <Config.h>
#include <HashMap.h> #include <HashMap.h>
#include <Log.h> #include <Log.h>
@ -83,12 +84,14 @@ TelodendriaConfigParse(HashMap * config, LogConfig * lc)
return NULL; return NULL;
} }
tConfig = calloc(1, sizeof(TelodendriaConfig)); tConfig = Malloc(sizeof(TelodendriaConfig));
if (!tConfig) if (!tConfig)
{ {
return NULL; return NULL;
} }
memset(tConfig, 0, sizeof(TelodendriaConfig));
directive = (ConfigDirective *) HashMapGet(config, "listen"); directive = (ConfigDirective *) HashMapGet(config, "listen");
children = ConfigChildrenGet(directive); children = ConfigChildrenGet(directive);
value = ConfigValuesGet(directive); value = ConfigValuesGet(directive);
@ -129,7 +132,7 @@ TelodendriaConfigParse(HashMap * config, LogConfig * lc)
else else
{ {
Log(lc, LOG_WARNING, "Base URL not specified. Assuming it's 'https://%s'.", tConfig->serverName); Log(lc, LOG_WARNING, "Base URL not specified. Assuming it's 'https://%s'.", tConfig->serverName);
tConfig->baseUrl = malloc(strlen(tConfig->serverName) + 10); tConfig->baseUrl = Malloc(strlen(tConfig->serverName) + 10);
if (!tConfig->baseUrl) if (!tConfig->baseUrl)
{ {
Log(lc, LOG_ERROR, "Error allocating memory for default config value 'base-url'."); Log(lc, LOG_ERROR, "Error allocating memory for default config value 'base-url'.");
@ -391,13 +394,13 @@ TelodendriaConfigFree(TelodendriaConfig * tConfig)
return; return;
} }
free(tConfig->serverName); Free(tConfig->serverName);
free(tConfig->baseUrl); Free(tConfig->baseUrl);
free(tConfig->identityServer); Free(tConfig->identityServer);
free(tConfig->uid); Free(tConfig->uid);
free(tConfig->gid); Free(tConfig->gid);
free(tConfig->dataDir); Free(tConfig->dataDir);
free(tConfig); Free(tConfig);
} }

View file

@ -23,6 +23,8 @@
*/ */
#include <Util.h> #include <Util.h>
#include <Memory.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
@ -45,7 +47,7 @@ UtilUtf8Encode(unsigned long utf8)
{ {
char *str; char *str;
str = malloc(5 * sizeof(char)); str = Malloc(5 * sizeof(char));
if (!str) if (!str)
{ {
return NULL; return NULL;
@ -96,7 +98,7 @@ UtilStringDuplicate(char *inStr)
char *outStr; char *outStr;
len = strlen(inStr); len = strlen(inStr);
outStr = malloc(len + 1); /* For the null terminator */ outStr = Malloc(len + 1); /* For the null terminator */
if (!outStr) if (!outStr)
{ {
return NULL; return NULL;