Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix freeing in a few places #2058

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
// caused by insufficient memory or by code bugs (not properly dealing
// with NULL pointers) much easier.
#undef strdup // strdup() is a macro in itself, it needs special handling
#define free(ptr) FTLfree((void**)&ptr, __FILE__, __FUNCTION__, __LINE__)
#define free(ptr) { FTLfree(ptr, __FILE__, __FUNCTION__, __LINE__); ptr = NULL; }
#define strdup(str_in) FTLstrdup(str_in, __FILE__, __FUNCTION__, __LINE__)
#define calloc(numer_of_elements, element_size) FTLcalloc(numer_of_elements, element_size, __FILE__, __FUNCTION__, __LINE__)
#define realloc(ptr, new_size) FTLrealloc(ptr, new_size, __FILE__, __FUNCTION__, __LINE__)
Expand Down
1 change: 0 additions & 1 deletion src/config/password.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ enum password_result verify_password(const char *password, const char *pwhash, c
config.webserver.api.pwhash.v.s = new_hash;
config.webserver.api.pwhash.t = CONF_STRING_ALLOCATED;
writeFTLtoml(true);
free(new_hash);
}

// Successful logins do not count against rate-limiting
Expand Down
20 changes: 10 additions & 10 deletions src/config/toml_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
}
case CONF_ENUM_PTR_TYPE:
{
const toml_datum_t val = toml_string_in(toml, key);
toml_datum_t val = toml_string_in(toml, key);
if(val.ok)
{
const int ptr_type = get_ptr_type_val(val.u.s);
Expand All @@ -559,7 +559,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
}
case CONF_ENUM_BUSY_TYPE:
{
const toml_datum_t val = toml_string_in(toml, key);
toml_datum_t val = toml_string_in(toml, key);
if(val.ok)
{
const int busy_reply = get_busy_reply_val(val.u.s);
Expand All @@ -575,7 +575,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
}
case CONF_ENUM_BLOCKING_MODE:
{
const toml_datum_t val = toml_string_in(toml, key);
toml_datum_t val = toml_string_in(toml, key);
if(val.ok)
{
const int blocking_mode = get_blocking_mode_val(val.u.s);
Expand All @@ -591,7 +591,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
}
case CONF_ENUM_REFRESH_HOSTNAMES:
{
const toml_datum_t val = toml_string_in(toml, key);
toml_datum_t val = toml_string_in(toml, key);
if(val.ok)
{
const int refresh_hostnames = get_refresh_hostnames_val(val.u.s);
Expand All @@ -607,7 +607,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
}
case CONF_ENUM_LISTENING_MODE:
{
const toml_datum_t val = toml_string_in(toml, key);
toml_datum_t val = toml_string_in(toml, key);
if(val.ok)
{
const int listeningMode = get_listeningMode_val(val.u.s);
Expand All @@ -623,7 +623,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
}
case CONF_ENUM_WEB_THEME:
{
const toml_datum_t val = toml_string_in(toml, key);
toml_datum_t val = toml_string_in(toml, key);
if(val.ok)
{
const int web_theme = get_web_theme_val(val.u.s);
Expand All @@ -639,7 +639,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
}
case CONF_ENUM_TEMP_UNIT:
{
const toml_datum_t val = toml_string_in(toml, key);
toml_datum_t val = toml_string_in(toml, key);
if(val.ok)
{
const int temp_unit = get_temp_unit_val(val.u.s);
Expand All @@ -665,7 +665,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
case CONF_STRUCT_IN_ADDR:
{
struct in_addr addr4 = { 0 };
const toml_datum_t val = toml_string_in(toml, key);
toml_datum_t val = toml_string_in(toml, key);
if(val.ok)
{
if(strlen(val.u.s) == 0)
Expand All @@ -686,7 +686,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
case CONF_STRUCT_IN6_ADDR:
{
struct in6_addr addr6 = { 0 };
const toml_datum_t val = toml_string_in(toml, key);
toml_datum_t val = toml_string_in(toml, key);
if(val.ok)
{
if(strlen(val.u.s) == 0)
Expand Down Expand Up @@ -717,7 +717,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
for(unsigned int i = 0; i < nelem; i++)
{
// Get string from TOML
const toml_datum_t d = toml_string_at(array, i);
toml_datum_t d = toml_string_at(array, i);
if(!d.ok)
{
log_warn("Config %s is an invalid array (found at index %u)", conf_item->k, i);
Expand Down
14 changes: 4 additions & 10 deletions src/syscalls/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,20 @@
#include "log.h"

#undef free
void FTLfree(void **ptr, const char *file, const char *func, const int line)
bool FTLfree(void *ptr, const char *file, const char *func, const int line)
{
// The free() function frees the memory space pointed to by ptr, which
// must have been returned by a previous call to malloc(), calloc(), or
// realloc(). Otherwise, or if free(ptr) has already been called before,
// undefined behavior occurs. If ptr is NULL, no operation is performed.
if(ptr == NULL)
{
log_warn("Trying to free NULL memory location in %s() (%s:%i)", func, file, line);
return;
}
if(*ptr == NULL)
{
log_warn("Trying to free NULL pointer in %s() (%s:%i)", func, file, line);
return;
return false;
}

// Actually free the memory
free(*ptr);
free(ptr);

// Set the pointer to NULL
*ptr = NULL;
return true;
}
2 changes: 1 addition & 1 deletion src/syscalls/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
char *FTLstrdup(const char *src, const char *file, const char *func, const int line) __attribute__((malloc));
void *FTLcalloc(size_t n, size_t size, const char *file, const char *func, const int line) __attribute__((malloc)) __attribute__((alloc_size(1,2)));
void *FTLrealloc(void *ptr_in, size_t size, const char *file, const char *func, const int line) __attribute__((alloc_size(2)));
void FTLfree(void **ptr, const char*file, const char *func, const int line);
bool FTLfree(void *ptr, const char*file, const char *func, const int line);
int FTLfallocate(const int fd, const off_t offset, const off_t len, const char *file, const char *func, const int line);


Expand Down
4 changes: 3 additions & 1 deletion src/webserver/webserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ unsigned short get_api_string(char **buf, const bool domain)
if(this_len < 0)
{
log_err("Failed to append API URL to buffer: %s", strerror(errno));
free(api_str);
return 0;
}

Expand All @@ -311,15 +312,16 @@ unsigned short get_api_string(char **buf, const bool domain)
if((size_t)this_len >= bufsz - len - 1)
{
log_err("API URL buffer too small!");
free(api_str);
return 0;
}

// Check if this string is already present in the buffer
if(memmem(*buf, len, api_str, this_len) != NULL)
{
// This string is already present, so skip it
free(api_str);
log_debug(DEBUG_API, "Skipping duplicate API URL: %s", api_str);
free(api_str);
continue;
}

Expand Down
Loading