Use Perl memory methods
FossilOrigin-Name: 1337944567f1c048fd4292a8acdab472328f5fb9559e9e1727ab53b86d895075
This commit is contained in:
parent
bf8785674f
commit
4ad40369e9
16 changed files with 70 additions and 55 deletions
|
@ -436,7 +436,7 @@ void content_account_blocked(PATH_ARGS)
|
|||
};
|
||||
|
||||
render_base_page(&b, req, ssn, api);
|
||||
free(result);
|
||||
Safefree(result);
|
||||
}
|
||||
|
||||
void content_account_muted(PATH_ARGS)
|
||||
|
@ -468,7 +468,7 @@ void content_account_muted(PATH_ARGS)
|
|||
};
|
||||
|
||||
render_base_page(&b, req, ssn, api);
|
||||
free(result);
|
||||
Safefree(result);
|
||||
}
|
||||
|
||||
void content_account_favourites(PATH_ARGS)
|
||||
|
|
|
@ -45,10 +45,10 @@ int try_upload_media(struct mstdnt_storage** storage,
|
|||
return 1;
|
||||
|
||||
if (media_ids)
|
||||
*media_ids = malloc(sizeof(char*) * size);
|
||||
*media_ids = safemalloc(sizeof(char*) * size);
|
||||
|
||||
*attachments = malloc(sizeof(struct mstdnt_attachment) * size);
|
||||
*storage = calloc(1, sizeof(struct mstdnt_storage) * size);
|
||||
*attachments = safemalloc(sizeof(struct mstdnt_attachment) * size);
|
||||
*storage = safecalloc(1, sizeof(struct mstdnt_storage) * size);
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
|
@ -72,26 +72,26 @@ int try_upload_media(struct mstdnt_storage** storage,
|
|||
{
|
||||
for (size_t j = 0; j < i; ++j)
|
||||
{
|
||||
if (media_ids) free((*media_ids)[j]);
|
||||
if (media_ids) Safefree((*media_ids)[j]);
|
||||
mstdnt_storage_cleanup(*storage + j);
|
||||
}
|
||||
|
||||
if (media_ids)
|
||||
{
|
||||
free(*media_ids);
|
||||
Safefree(*media_ids);
|
||||
*media_ids = NULL;
|
||||
}
|
||||
|
||||
free(*attachments);
|
||||
Safefree(*attachments);
|
||||
*attachments = NULL;
|
||||
free(*storage);
|
||||
Safefree(*storage);
|
||||
*storage = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (media_ids)
|
||||
{
|
||||
(*media_ids)[i] = malloc(strlen((*attachments)[i].id)+1);
|
||||
(*media_ids)[i] = safemalloc(strlen((*attachments)[i].id)+1);
|
||||
strcpy((*media_ids)[i], (*attachments)[i].id);
|
||||
}
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ void cleanup_media_storages(struct session* ssn, struct mstdnt_storage* storage)
|
|||
if (!FILES_READY(ssn)) return;
|
||||
for (size_t i = 0; i < keyfile(ssn->post.files).array_size; ++i)
|
||||
mstdnt_storage_cleanup(storage + i);
|
||||
free(storage);
|
||||
Safefree(storage);
|
||||
}
|
||||
|
||||
void cleanup_media_ids(struct session* ssn, char** media_ids)
|
||||
|
@ -112,8 +112,8 @@ void cleanup_media_ids(struct session* ssn, char** media_ids)
|
|||
if (!FILES_READY(ssn)) return;
|
||||
if (!media_ids) return;
|
||||
for (size_t i = 0; i < keyfile(ssn->post.files).array_size; ++i)
|
||||
free(media_ids[i]);
|
||||
free(media_ids);
|
||||
Safefree(media_ids[i]);
|
||||
Safefree(media_ids);
|
||||
}
|
||||
|
||||
HV* perlify_attachment(const struct mstdnt_attachment* const attachment)
|
||||
|
@ -153,7 +153,7 @@ void api_attachment_create(PATH_ARGS)
|
|||
{
|
||||
string = cJSON_Print(root);
|
||||
send_result(req, NULL, "application/json", string, 0);
|
||||
free(string);
|
||||
Safefree(string);
|
||||
}
|
||||
else
|
||||
send_result(req, NULL, "application/json", "{\"status\":\"Couldn't\"}", 0);
|
||||
|
@ -161,6 +161,6 @@ void api_attachment_create(PATH_ARGS)
|
|||
// Cleanup media stuff
|
||||
cleanup_media_storages(ssn, att_storage);
|
||||
cleanup_media_ids(ssn, media_ids);
|
||||
free(attachments);
|
||||
Safefree(attachments);
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ char* read_cookies_env(REQUEST_T req, struct cookie_values* cookies)
|
|||
if (!cookies_env)
|
||||
return NULL;
|
||||
|
||||
char* cookies_str = malloc(strlen(cookies_env) + 1);
|
||||
char* cookies_str = safemalloc(strlen(cookies_env) + 1);
|
||||
if (!cookies_str)
|
||||
{
|
||||
perror("malloc");
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include <fcgi_stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "global_perl.h"
|
||||
#include "easprintf.h"
|
||||
|
||||
int evasprintf(char** ret, const char* format, va_list ap)
|
||||
|
@ -28,7 +29,7 @@ int evasprintf(char** ret, const char* format, va_list ap)
|
|||
int sz = vsnprintf(NULL, 0, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
*ret = malloc(sz + 1);
|
||||
*ret = safemalloc(sz + 1);
|
||||
if(*ret == NULL)
|
||||
{
|
||||
perror("malloc");
|
||||
|
|
|
@ -72,7 +72,7 @@ void content_emoji_picker(PATH_ARGS)
|
|||
|
||||
send_result(req, NULL, NULL, picker, 0);
|
||||
|
||||
free(picker);
|
||||
Safefree(picker);
|
||||
}
|
||||
|
||||
char* construct_emoji_picker(char* status_id, size_t* size)
|
||||
|
@ -119,7 +119,7 @@ char* construct_emoji_picker(char* status_id, size_t* size)
|
|||
|
||||
// Cleanup
|
||||
for (size_t i = 0; i < EMO_CAT_LEN; ++i)
|
||||
free(emojis[i]);
|
||||
Safefree(emojis[i]);
|
||||
return dup;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "global_perl.h"
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include "key.h"
|
||||
|
@ -46,13 +47,13 @@ void key_files(char* val, struct file_content* form, struct key* arg)
|
|||
struct file_array* arr = &(arg->type.f);
|
||||
char* content_cpy;
|
||||
|
||||
arr->content = realloc(arr->content,
|
||||
arr->content = saferealloc(arr->content,
|
||||
sizeof(struct file_content) * ++(arr->array_size));
|
||||
if (!(arr->content))
|
||||
return;
|
||||
|
||||
// Make a copy so we can remember it later
|
||||
if (!(content_cpy = malloc(form->content_size+1)))
|
||||
if (!(content_cpy = safemalloc(form->content_size+1)))
|
||||
return;
|
||||
|
||||
memcpy(content_cpy, val, form->content_size+1);
|
||||
|
|
|
@ -105,7 +105,7 @@ void content_login_oauth(PATH_ARGS)
|
|||
PRINTF("Set-Cookie: client_secret=%s; Path=/; Max-Age=3153600\r\n", app.client_secret);
|
||||
|
||||
redirect(req, REDIRECT_303, url);
|
||||
free(url);
|
||||
Safefree(url);
|
||||
curl_free(encode_id);
|
||||
}
|
||||
}
|
||||
|
@ -117,7 +117,7 @@ void content_login_oauth(PATH_ARGS)
|
|||
|
||||
mstdnt_storage_cleanup(&storage);
|
||||
mstdnt_storage_cleanup(&oauth_storage);
|
||||
if (urlify_redirect_url) free(urlify_redirect_url);
|
||||
if (urlify_redirect_url) Safefree(urlify_redirect_url);
|
||||
if (decode_url) curl_free(decode_url);
|
||||
}
|
||||
|
||||
|
@ -192,7 +192,7 @@ void content_login(PATH_ARGS)
|
|||
PUT("Set-Cookie: instance_url=; Path=/; Max-Age=-1\r\n");
|
||||
|
||||
apply_access_token(req, token.access_token);
|
||||
free(url_link);
|
||||
Safefree(url_link);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -201,7 +201,7 @@ void content_login(PATH_ARGS)
|
|||
{
|
||||
// Restore and cleanup, an error occured
|
||||
m_args.url = orig_url;
|
||||
free(url_link);
|
||||
Safefree(url_link);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
16
src/main.c
16
src/main.c
|
@ -176,9 +176,9 @@ static void application(mstdnt_t* api, REQUEST_T req)
|
|||
handle_paths(req, &ssn, api, paths, sizeof(paths)/sizeof(paths[0]));
|
||||
|
||||
// Cleanup
|
||||
if (cookies_str) free(cookies_str);
|
||||
if (post_str) free(post_str);
|
||||
if (get_str) free(get_str);
|
||||
if (cookies_str) Safefree(cookies_str);
|
||||
if (post_str) Safefree(post_str);
|
||||
if (get_str) Safefree(get_str);
|
||||
free_files(&(keyfile(ssn.post.files)));
|
||||
if (ssn.logged_in) mstdnt_cleanup_account(&(ssn.acct));
|
||||
mstdnt_storage_cleanup(&(ssn.acct_storage));
|
||||
|
@ -251,6 +251,16 @@ int main(int argc, char **argv, char **env)
|
|||
|
||||
init_template_files(aTHX);
|
||||
|
||||
// Setup mstdnt hooks
|
||||
struct mstdnt_hooks hooks = {
|
||||
.malloc = safemalloc,
|
||||
// Not sure how this differs from Safefree?
|
||||
.free = safefree,
|
||||
.calloc = safecalloc,
|
||||
.realloc = saferealloc,
|
||||
};
|
||||
mstdnt_set_hooks(&hooks);
|
||||
|
||||
// Initiate mstdnt library
|
||||
mstdnt_t api;
|
||||
mstdnt_init(&api);
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include "global_perl.h"
|
||||
#include <fcgi_stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -33,7 +34,7 @@ char* get_mime_boundary(char* content_type_str, char** bound)
|
|||
char* content = content_type_str;
|
||||
|
||||
// Data gets changed in place
|
||||
char* content_type = malloc(strlen(content)+1);
|
||||
char* content_type = safemalloc(strlen(content)+1);
|
||||
if (!content_type)
|
||||
{
|
||||
perror("malloc");
|
||||
|
@ -66,7 +67,7 @@ char* get_mime_boundary(char* content_type_str, char** bound)
|
|||
|
||||
return content_type;
|
||||
error:
|
||||
free(content_type);
|
||||
Safefree(content_type);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
@ -63,11 +63,11 @@ int parse_path(REQUEST_T req,
|
|||
|
||||
read_len = (size_t)after_str - (size_t)(p2 + i);
|
||||
// Copy in new data from the string we just read
|
||||
tmp = malloc(read_len+1);
|
||||
tmp = safemalloc(read_len+1);
|
||||
strncpy(tmp, after_str - read_len, read_len);
|
||||
tmp[read_len] = '\0';
|
||||
// Add our new string
|
||||
data = realloc(data, ++size * sizeof(tmp));
|
||||
data = saferealloc(data, ++size * sizeof(tmp));
|
||||
data[size-1] = tmp;
|
||||
// Move ahead (-1 because we move again)
|
||||
i += read_len - 1;
|
||||
|
@ -95,9 +95,9 @@ breakpt:
|
|||
// Cleanup
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
{
|
||||
free(data[i]);
|
||||
Safefree(data[i]);
|
||||
}
|
||||
if (data) free(data);
|
||||
if (data) Safefree(data);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
12
src/query.c
12
src/query.c
|
@ -43,7 +43,7 @@ char* read_get_data(REQUEST_T req, struct get_values* query)
|
|||
|
||||
if (query_string)
|
||||
{
|
||||
get_query = malloc(strlen(query_string) + 1);
|
||||
get_query = safemalloc(strlen(query_string) + 1);
|
||||
if (!get_query)
|
||||
{
|
||||
perror("malloc");
|
||||
|
@ -135,7 +135,7 @@ char* read_post_data(REQUEST_T req, struct post_values* post)
|
|||
char* mime_boundary;
|
||||
char* mime_mem = get_mime_boundary(GET_ENV("CONTENT_TYPE", req), &mime_boundary);
|
||||
int content_length = atoi(GET_ENV("CONTENT_LENGTH", req));
|
||||
post_query = malloc(content_length + 1);
|
||||
post_query = safemalloc(content_length + 1);
|
||||
if (!post_query)
|
||||
{
|
||||
perror("malloc");
|
||||
|
@ -188,7 +188,7 @@ char* read_post_data(REQUEST_T req, struct post_values* post)
|
|||
}
|
||||
while (p_query_read);
|
||||
|
||||
if (mime_mem) free(mime_mem);
|
||||
if (mime_mem) Safefree(mime_mem);
|
||||
}
|
||||
|
||||
// Free me afterwards!
|
||||
|
@ -226,7 +226,7 @@ char* try_handle_post(REQUEST_T req, void (*call)(struct http_query_info*, void*
|
|||
if (request_method && (strcmp("POST", request_method) == 0))
|
||||
{
|
||||
int content_length = atoi(GET_ENV("CONTENT_LENGTH", req));
|
||||
post_query = malloc(content_length + 1);
|
||||
post_query = safemalloc(content_length + 1);
|
||||
if (!post_query)
|
||||
{
|
||||
puts("Malloc error!");
|
||||
|
@ -265,9 +265,9 @@ void free_files(struct file_array* files)
|
|||
struct file_content* content = files->content;
|
||||
for (size_t i = 0; i < files->array_size; ++i)
|
||||
{
|
||||
free(content[i].content);
|
||||
Safefree(content[i].content);
|
||||
}
|
||||
free(content);
|
||||
Safefree(content);
|
||||
}
|
||||
|
||||
// TODO use hvstores_XXX macros
|
||||
|
|
|
@ -70,7 +70,7 @@ void content_search_all(PATH_ARGS)
|
|||
redirect(req, REDIRECT_303, url);
|
||||
break;
|
||||
}
|
||||
free(url);
|
||||
Safefree(url);
|
||||
curl_free(query);
|
||||
return;
|
||||
}
|
||||
|
|
10
src/status.c
10
src/status.c
|
@ -70,7 +70,7 @@ int try_post_status(struct session* ssn, mstdnt_t* api)
|
|||
json_ids_len = cJSON_GetArraySize(json_ids);
|
||||
if (json_ids_len)
|
||||
{
|
||||
media_ids = malloc(json_ids_len * sizeof(char*));
|
||||
media_ids = safemalloc(json_ids_len * sizeof(char*));
|
||||
// TODO error
|
||||
cJSON* id;
|
||||
int i = 0;
|
||||
|
@ -110,11 +110,11 @@ int try_post_status(struct session* ssn, mstdnt_t* api)
|
|||
cleanup_media_storages(ssn, att_storage);
|
||||
|
||||
if (json_ids)
|
||||
free(media_ids);
|
||||
Safefree(media_ids);
|
||||
else
|
||||
cleanup_media_ids(ssn, media_ids);
|
||||
|
||||
free(attachments);
|
||||
Safefree(attachments);
|
||||
if (json_ids) cJSON_Delete(json_ids);
|
||||
return 0;
|
||||
}
|
||||
|
@ -403,7 +403,7 @@ void content_status(PATH_ARGS, uint8_t flags)
|
|||
mstdnt_storage_cleanup(&storage);
|
||||
mstdnt_storage_cleanup(&status_storage);
|
||||
Safefree(dup);
|
||||
free(picker);
|
||||
Safefree(picker);
|
||||
}
|
||||
|
||||
void notice_redirect(PATH_ARGS)
|
||||
|
@ -411,7 +411,7 @@ void notice_redirect(PATH_ARGS)
|
|||
char* url;
|
||||
easprintf(&url, "%s/status/%s", config_url_prefix, data[0]);
|
||||
redirect(req, REDIRECT_303, url);
|
||||
free(url);
|
||||
Safefree(url);
|
||||
}
|
||||
|
||||
HV* perlify_status_pleroma(const struct mstdnt_status_pleroma* pleroma)
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#define _DEFAULT_SOURCE
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include "global_perl.h"
|
||||
#include "easprintf.h"
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
@ -110,7 +111,7 @@ char* strrepl(char* source, char* find, char* repl, int flags)
|
|||
str_size += curr - last;
|
||||
|
||||
// Create and copy
|
||||
result = realloc(result, str_size + (!is_last ? repl_len : 0) + 1);
|
||||
result = saferealloc(result, str_size + (!is_last ? repl_len : 0) + 1);
|
||||
strncpy(result + last_str_size, last, curr - last);
|
||||
if (!is_last)
|
||||
{
|
||||
|
@ -134,9 +135,9 @@ char* sanitize_html(char* html)
|
|||
char* left = strrepl(amp, "<", "<", STRREPL_ALL);
|
||||
char* right = strrepl(left, ">", ">", STRREPL_ALL);
|
||||
char* quot = strrepl(right, "\"", """, STRREPL_ALL);
|
||||
if (quot != right && right != html && right != left) free(right);
|
||||
if (left != html && left != amp) free(left);
|
||||
if (amp != html) free(amp);
|
||||
if (quot != right && right != html && right != left) Safefree(right);
|
||||
if (left != html && left != amp) Safefree(left);
|
||||
if (amp != html) Safefree(amp);
|
||||
|
||||
return quot;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
*/
|
||||
|
||||
#include <fcgi_stdio.h>
|
||||
#include "global_perl.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "string_helpers.h"
|
||||
|
@ -42,24 +43,24 @@ char* construct_func_strings(char* (*func)(void*, size_t, size_t*),
|
|||
|
||||
if (parse_size == -1) /* Malloc error */
|
||||
{
|
||||
if (result) free(result);
|
||||
if (result) Safefree(result);
|
||||
return NULL;
|
||||
}
|
||||
last_parse_size = curr_parse_size;
|
||||
curr_parse_size += parse_size;
|
||||
|
||||
result = realloc(result, curr_parse_size + 1);
|
||||
result = saferealloc(result, curr_parse_size + 1);
|
||||
if (result == NULL)
|
||||
{
|
||||
perror("malloc");
|
||||
free(res_html);
|
||||
Safefree(res_html);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Copy res_html to result in correct position
|
||||
strncpy(result + last_parse_size, res_html, parse_size);
|
||||
// Cleanup
|
||||
free(res_html);
|
||||
Safefree(res_html);
|
||||
}
|
||||
|
||||
if (result)
|
||||
|
|
|
@ -221,7 +221,7 @@ void tl_tag(REQUEST_T req, struct session* ssn, mstdnt_t* api, char* tag_id)
|
|||
easprintf(&header, "Hashtag - #%s", tag_id);
|
||||
|
||||
content_timeline(req, ssn, api, &storage, statuses, statuses_len, BASE_CAT_NONE, header, 0, 0);
|
||||
free(header);
|
||||
Safefree(header);
|
||||
}
|
||||
|
||||
void content_tl_home(PATH_ARGS)
|
||||
|
|
Loading…
Reference in a new issue