Replace glib memory functions with try version

This commit is contained in:
Moritz Lipp 2014-01-19 16:47:08 +01:00
parent bafbedbcef
commit 0fc5b80121
16 changed files with 189 additions and 46 deletions

View file

@ -48,7 +48,10 @@ zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page)
return old; return old;
} }
zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t)); zathura_bookmark_t* bookmark = g_try_malloc0(sizeof(zathura_bookmark_t));
if (bookmark == NULL) {
return NULL;
}
bookmark->id = g_strdup(id); bookmark->id = g_strdup(id);
bookmark->page = page; bookmark->page = page;

View file

@ -400,7 +400,10 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu
} }
} }
girara_argument_t* arg = g_malloc0(sizeof(girara_argument_t)); girara_argument_t* arg = g_try_malloc0(sizeof(girara_argument_t));
if (arg == NULL) {
return false;
}
arg->n = FORWARD; arg->n = FORWARD;
sc_search(session, arg, NULL, 0); sc_search(session, arg, NULL, 0);

View file

@ -134,7 +134,11 @@ guess_type_glib(const char* path)
g_free((void*)content_type); g_free((void*)content_type);
content_type = NULL; content_type = NULL;
content = g_realloc(content, length + BUFSIZ); content = g_try_realloc(content, length + BUFSIZ);
if (content == NULL) {
continue;
}
bytes_read = read(fd, content + length, BUFSIZ); bytes_read = read(fd, content + length, BUFSIZ);
if (bytes_read == -1) { if (bytes_read == -1) {
break; break;

View file

@ -326,13 +326,27 @@ plain_add_bookmark(zathura_database_t* db, const char* file,
return false; return false;
} }
char* bmx = g_try_malloc(G_ASCII_DTOSTR_BUF_SIZE);
if (bmx == NULL) {
return false;
}
char* bmy = g_try_malloc(G_ASCII_DTOSTR_BUF_SIZE);
if (bmy == NULL) {
g_free(bmx);
return false;
}
char* name = prepare_filename(file); char* name = prepare_filename(file);
char* val_list[] = { char* val_list[] = {
g_strdup_printf("%d", bookmark->page), g_strdup_printf("%d", bookmark->page),
g_ascii_dtostr(g_malloc(G_ASCII_DTOSTR_BUF_SIZE), G_ASCII_DTOSTR_BUF_SIZE, bookmark->x), g_ascii_dtostr(bmx, G_ASCII_DTOSTR_BUF_SIZE, bookmark->x),
g_ascii_dtostr(g_malloc(G_ASCII_DTOSTR_BUF_SIZE), G_ASCII_DTOSTR_BUF_SIZE, bookmark->y) g_ascii_dtostr(bmy, G_ASCII_DTOSTR_BUF_SIZE, bookmark->y)
}; };
g_free(bmx);
g_free(bmy);
g_key_file_set_string_list(priv->bookmarks, name, bookmark->id, (const char**)val_list, LENGTH(val_list)); g_key_file_set_string_list(priv->bookmarks, name, bookmark->id, (const char**)val_list, LENGTH(val_list));
for (unsigned int i = 0; i < LENGTH(val_list); ++i) { for (unsigned int i = 0; i < LENGTH(val_list); ++i) {
@ -397,7 +411,10 @@ plain_load_bookmarks(zathura_database_t* db, const char* file)
gsize num_vals = 0; gsize num_vals = 0;
for (gsize i = 0; i < num_keys; i++) { for (gsize i = 0; i < num_keys; i++) {
zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t)); zathura_bookmark_t* bookmark = g_try_malloc0(sizeof(zathura_bookmark_t));
if (bookmark == NULL) {
continue;
}
bookmark->id = g_strdup(keys[i]); bookmark->id = g_strdup(keys[i]);
char **val_list = g_key_file_get_string_list(priv->bookmarks, name, keys[i], &num_vals, NULL); char **val_list = g_key_file_get_string_list(priv->bookmarks, name, keys[i], &num_vals, NULL);
@ -439,7 +456,10 @@ get_jumplist_from_str(const char* str)
char* token = strtok_r(copy, " ", &saveptr); char* token = strtok_r(copy, " ", &saveptr);
while (token != NULL) { while (token != NULL) {
zathura_jump_t* jump = g_malloc0(sizeof(zathura_jump_t)); zathura_jump_t* jump = g_try_malloc0(sizeof(zathura_jump_t));
if (jump == NULL) {
continue;
}
jump->page = strtoul(token, NULL, 0); jump->page = strtoul(token, NULL, 0);
token = strtok_r(NULL, " ", &saveptr); token = strtok_r(NULL, " ", &saveptr);

View file

@ -390,7 +390,10 @@ sqlite_load_bookmarks(zathura_database_t* db, const char* file)
(girara_free_function_t) zathura_bookmark_free); (girara_free_function_t) zathura_bookmark_free);
while (sqlite3_step(stmt) == SQLITE_ROW) { while (sqlite3_step(stmt) == SQLITE_ROW) {
zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t)); zathura_bookmark_t* bookmark = g_try_malloc0(sizeof(zathura_bookmark_t));
if (bookmark == NULL) {
continue;
}
bookmark->id = g_strdup((const char*) sqlite3_column_text(stmt, 0)); bookmark->id = g_strdup((const char*) sqlite3_column_text(stmt, 0));
bookmark->page = sqlite3_column_int(stmt, 1); bookmark->page = sqlite3_column_int(stmt, 1);
@ -524,7 +527,10 @@ sqlite_load_jumplist(zathura_database_t* db, const char* file)
int res = 0; int res = 0;
while ((res = sqlite3_step(stmt)) == SQLITE_ROW) { while ((res = sqlite3_step(stmt)) == SQLITE_ROW) {
zathura_jump_t* jump = g_malloc0(sizeof(zathura_jump_t)); zathura_jump_t* jump = g_try_malloc0(sizeof(zathura_jump_t));
if (jump == NULL) {
continue;
}
jump->page = sqlite3_column_int(stmt, 0); jump->page = sqlite3_column_int(stmt, 0);
jump->x = sqlite3_column_double(stmt, 1); jump->x = sqlite3_column_double(stmt, 1);

View file

@ -289,13 +289,20 @@ handle_method_call(GDBusConnection* UNUSED(connection),
} }
/* get rectangles */ /* get rectangles */
girara_list_t** rectangles = g_malloc0(number_of_pages * sizeof(girara_list_t*)); girara_list_t** rectangles = g_try_malloc0(number_of_pages * sizeof(girara_list_t*));
if (rectangles == NULL) {
return;
}
rectangles[page] = girara_list_new2(g_free); rectangles[page] = girara_list_new2(g_free);
zathura_rectangle_t temp_rect; zathura_rectangle_t temp_rect;
while (g_variant_iter_loop(iter, "(dddd)", &temp_rect.x1, &temp_rect.x2, while (g_variant_iter_loop(iter, "(dddd)", &temp_rect.x1, &temp_rect.x2,
&temp_rect.y1, &temp_rect.y2)) { &temp_rect.y1, &temp_rect.y2)) {
zathura_rectangle_t* rect = g_malloc0(sizeof(zathura_rectangle_t)); zathura_rectangle_t* rect = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rect == NULL) {
continue;
}
*rect = temp_rect; *rect = temp_rect;
girara_list_append(rectangles[page], rect); girara_list_append(rectangles[page], rect);
} }
@ -314,7 +321,10 @@ handle_method_call(GDBusConnection* UNUSED(connection),
rectangles[temp_page] = girara_list_new2(g_free); rectangles[temp_page] = girara_list_new2(g_free);
} }
zathura_rectangle_t* rect = g_malloc0(sizeof(zathura_rectangle_t)); zathura_rectangle_t* rect = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rect == NULL) {
continue;
}
*rect = temp_rect; *rect = temp_rect;
girara_list_append(rectangles[temp_page], rect); girara_list_append(rectangles[temp_page], rect);
} }

View file

@ -104,11 +104,19 @@ zathura_document_open(zathura_plugin_manager_t* plugin_manager, const char*
if (plugin == NULL) { if (plugin == NULL) {
girara_error("unknown file type\n"); girara_error("unknown file type\n");
if (error != NULL) {
*error = ZATHURA_ERROR_UNKNOWN; *error = ZATHURA_ERROR_UNKNOWN;
}
goto error_free; goto error_free;
} }
document = g_malloc0(sizeof(zathura_document_t)); document = g_try_malloc0(sizeof(zathura_document_t));
if (document == NULL) {
if (error != NULL) {
*error = ZATHURA_ERROR_OUT_OF_MEMORY;
}
goto error_free;
}
document->file_path = real_path; document->file_path = real_path;
document->basename = g_path_get_basename(real_path); document->basename = g_path_get_basename(real_path);

View file

@ -28,7 +28,10 @@ zathura_link_t*
zathura_link_new(zathura_link_type_t type, zathura_rectangle_t position, zathura_link_new(zathura_link_type_t type, zathura_rectangle_t position,
zathura_link_target_t target) zathura_link_target_t target)
{ {
zathura_link_t* link = g_malloc0(sizeof(zathura_link_t)); zathura_link_t* link = g_try_malloc0(sizeof(zathura_link_t));
if (link == NULL) {
return NULL;
}
link->type = type; link->type = type;
link->position = position; link->position = position;

View file

@ -215,7 +215,10 @@ mark_add(zathura_t* zathura, int key)
GIRARA_LIST_FOREACH_END(zathura->global.marks, zathura_mark_t*, iter, mark); GIRARA_LIST_FOREACH_END(zathura->global.marks, zathura_mark_t*, iter, mark);
/* add new mark */ /* add new mark */
zathura_mark_t* mark = g_malloc0(sizeof(zathura_mark_t)); zathura_mark_t* mark = g_try_malloc0(sizeof(zathura_mark_t));
if (mark == NULL) {
return NULL;
}
mark->key = key; mark->key = key;
mark->page = page_id; mark->page = page_id;

10
page.c
View file

@ -31,7 +31,13 @@ zathura_page_new(zathura_document_t* document, unsigned int index, zathura_error
} }
/* init page */ /* init page */
zathura_page_t* page = g_malloc0(sizeof(zathura_page_t)); zathura_page_t* page = g_try_malloc0(sizeof(zathura_page_t));
if (page == NULL) {
if (error != NULL) {
*error = ZATHURA_ERROR_OUT_OF_MEMORY;
}
goto error_ret;
}
page->index = index; page->index = index;
page->visible = false; page->visible = false;
@ -45,7 +51,7 @@ zathura_page_new(zathura_document_t* document, unsigned int index, zathura_error
if (error != NULL) { if (error != NULL) {
*error = ZATHURA_ERROR_NOT_IMPLEMENTED; *error = ZATHURA_ERROR_NOT_IMPLEMENTED;
} }
goto error_ret; goto error_free;
} }
zathura_error_t ret = functions->page_init(page); zathura_error_t ret = functions->page_init(page);

View file

@ -54,7 +54,10 @@ static void zathura_type_plugin_mapping_free(zathura_type_plugin_mapping_t* mapp
zathura_plugin_manager_t* zathura_plugin_manager_t*
zathura_plugin_manager_new() zathura_plugin_manager_new()
{ {
zathura_plugin_manager_t* plugin_manager = g_malloc0(sizeof(zathura_plugin_manager_t)); zathura_plugin_manager_t* plugin_manager = g_try_malloc0(sizeof(zathura_plugin_manager_t));
if (plugin_manager == NULL) {
return NULL;
}
plugin_manager->plugins = girara_list_new2((girara_free_function_t) zathura_plugin_free); plugin_manager->plugins = girara_list_new2((girara_free_function_t) zathura_plugin_free);
plugin_manager->path = girara_list_new2(g_free); plugin_manager->path = girara_list_new2(g_free);
@ -159,7 +162,11 @@ zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager)
continue; continue;
} }
plugin = g_malloc0(sizeof(zathura_plugin_t)); plugin = g_try_malloc0(sizeof(zathura_plugin_t));
if (plugin == NULL) {
continue;
}
plugin->content_types = girara_list_new2(g_free); plugin->content_types = girara_list_new2(g_free);
plugin->handle = handle; plugin->handle = handle;
@ -294,7 +301,11 @@ plugin_mapping_new(zathura_plugin_manager_t* plugin_manager, const gchar* type,
} }
GIRARA_LIST_FOREACH_END(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping); GIRARA_LIST_FOREACH_END(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);
zathura_type_plugin_mapping_t* mapping = g_malloc(sizeof(zathura_type_plugin_mapping_t)); zathura_type_plugin_mapping_t* mapping = g_try_malloc(sizeof(zathura_type_plugin_mapping_t));
if (mapping == NULL) {
return false;
}
mapping->type = g_strdup(type); mapping->type = g_strdup(type);
mapping->plugin = plugin; mapping->plugin = plugin;
girara_list_append(plugin_manager->type_plugin_mapping, mapping); girara_list_append(plugin_manager->type_plugin_mapping, mapping);

View file

@ -117,14 +117,20 @@ zathura_renderer_init(ZathuraRenderer* renderer)
priv->requests = girara_list_new(); priv->requests = girara_list_new();
} }
static void static bool
page_cache_init(ZathuraRenderer* renderer, size_t cache_size) page_cache_init(ZathuraRenderer* renderer, size_t cache_size)
{ {
private_t* priv = GET_PRIVATE(renderer); private_t* priv = GET_PRIVATE(renderer);
priv->page_cache.size = cache_size; priv->page_cache.size = cache_size;
priv->page_cache.cache = g_malloc(cache_size * sizeof(int)); priv->page_cache.cache = g_try_malloc(cache_size * sizeof(int));
if (priv->page_cache.cache == NULL) {
return false;
}
page_cache_invalidate_all(renderer); page_cache_invalidate_all(renderer);
return true;
} }
ZathuraRenderer* ZathuraRenderer*
@ -134,7 +140,10 @@ zathura_renderer_new(size_t cache_size)
GObject* obj = g_object_new(ZATHURA_TYPE_RENDERER, NULL); GObject* obj = g_object_new(ZATHURA_TYPE_RENDERER, NULL);
ZathuraRenderer* ret = ZATHURA_RENDERER(obj); ZathuraRenderer* ret = ZATHURA_RENDERER(obj);
page_cache_init(ret, cache_size);
if (page_cache_init(ret, cache_size) == false) {
return NULL;
}
return ret; return ret;
} }
@ -409,7 +418,11 @@ zathura_render_request(ZathuraRenderRequest* request, gint64 last_view_time)
if (unfinished_jobs == false) { if (unfinished_jobs == false) {
request_priv->last_view_time = last_view_time; request_priv->last_view_time = last_view_time;
render_job_t* job = g_malloc0(sizeof(render_job_t)); render_job_t* job = g_try_malloc0(sizeof(render_job_t));
if (job == NULL) {
return;
}
job->request = g_object_ref(request); job->request = g_object_ref(request);
job->aborted = false; job->aborted = false;
girara_list_append(request_priv->active_jobs, job); girara_list_append(request_priv->active_jobs, job);
@ -683,7 +696,11 @@ render(render_job_t* job, ZathuraRenderRequest* request, ZathuraRenderer* render
recolor(priv, page_width, page_height, surface); recolor(priv, page_width, page_height, surface);
} }
emit_completed_signal_t* ecs = g_malloc(sizeof(emit_completed_signal_t)); emit_completed_signal_t* ecs = g_try_malloc(sizeof(emit_completed_signal_t));
if (ecs == NULL) {
return false;
}
ecs->job = job; ecs->job = job;
ecs->surface = cairo_surface_reference(surface); ecs->surface = cairo_surface_reference(surface);

View file

@ -63,8 +63,12 @@ synctex_edit(zathura_t* zathura, zathura_page_t* page, int x, int y)
return; return;
} }
char** argv = g_malloc0(sizeof(char*) * (zathura->synctex.editor != NULL ? char** argv = g_try_malloc0(sizeof(char*) * (zathura->synctex.editor != NULL ?
6 : 4)); 6 : 4));
if (argv == NULL) {
return;
}
argv[0] = g_strdup("synctex"); argv[0] = g_strdup("synctex");
argv[1] = g_strdup("edit"); argv[1] = g_strdup("edit");
argv[2] = g_strdup("-o"); argv[2] = g_strdup("-o");
@ -101,7 +105,11 @@ synctex_rectangles_from_position(const char* filename, const char* position,
return NULL; return NULL;
} }
char** argv = g_malloc0(sizeof(char*) * 6); char** argv = g_try_malloc0(sizeof(char*) * 6);
if (argv == NULL) {
return NULL;
}
argv[0] = g_strdup("synctex"); argv[0] = g_strdup("synctex");
argv[1] = g_strdup("view"); argv[1] = g_strdup("view");
argv[2] = g_strdup("-i"); argv[2] = g_strdup("-i");
@ -180,31 +188,47 @@ synctex_rectangles_from_position(const char* filename, const char* position,
girara_list_append(hitlist, rectangle); girara_list_append(hitlist, rectangle);
rectangle = NULL; rectangle = NULL;
} else if (rectangle != NULL) { } else if (rectangle != NULL) {
synctex_page_rect_t* page_rect = g_malloc0(sizeof(synctex_page_rect_t)); synctex_page_rect_t* page_rect = g_try_malloc0(sizeof(synctex_page_rect_t));
if (page_rect == NULL) {
continue;
}
page_rect->page = current_page; page_rect->page = current_page;
page_rect->rect = *rectangle; page_rect->rect = *rectangle;
girara_list_append(other_rects, page_rect); girara_list_append(other_rects, page_rect);
} }
g_free(rectangle); g_free(rectangle);
rectangle = g_malloc0(sizeof(zathura_rectangle_t)); rectangle = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rectangle == NULL) {
continue;
}
} }
break; break;
case SYNCTEX_PROP_H: case SYNCTEX_PROP_H:
if (rectangle != NULL) {
rectangle->x1 = scan_float(scanner); rectangle->x1 = scan_float(scanner);
}
break; break;
case SYNCTEX_PROP_V: case SYNCTEX_PROP_V:
if (rectangle != NULL) {
rectangle->y2 = scan_float(scanner); rectangle->y2 = scan_float(scanner);
}
break; break;
case SYNCTEX_PROP_WIDTH: case SYNCTEX_PROP_WIDTH:
if (rectangle != NULL) {
rectangle->x2 = rectangle->x1 + scan_float(scanner); rectangle->x2 = rectangle->x1 + scan_float(scanner);
}
break; break;
case SYNCTEX_PROP_HEIGHT: case SYNCTEX_PROP_HEIGHT:
if (rectangle != NULL) {
rectangle->y1 = rectangle->y2 - scan_float(scanner); rectangle->y1 = rectangle->y2 - scan_float(scanner);
}
break; break;
} }
break; break;
@ -218,13 +242,15 @@ synctex_rectangles_from_position(const char* filename, const char* position,
if (current_page == rpage) { if (current_page == rpage) {
girara_list_append(hitlist, rectangle); girara_list_append(hitlist, rectangle);
} else { } else {
synctex_page_rect_t* page_rect = g_malloc0(sizeof(synctex_page_rect_t)); synctex_page_rect_t* page_rect = g_try_malloc0(sizeof(synctex_page_rect_t));
if (page_rect != NULL) {
page_rect->page = current_page; page_rect->page = current_page;
page_rect->rect = *rectangle; page_rect->rect = *rectangle;
girara_list_append(other_rects, page_rect); girara_list_append(other_rects, page_rect);
g_free(rectangle); g_free(rectangle);
} }
} }
}
g_scanner_destroy(scanner); g_scanner_destroy(scanner);
close(output); close(output);

10
types.c
View file

@ -15,7 +15,10 @@ zathura_index_element_new(const char* title)
return NULL; return NULL;
} }
zathura_index_element_t* res = g_malloc0(sizeof(zathura_index_element_t)); zathura_index_element_t* res = g_try_malloc0(sizeof(zathura_index_element_t));
if (res == NULL) {
return NULL;
}
res->title = g_strdup(title); res->title = g_strdup(title);
@ -86,7 +89,10 @@ zathura_document_information_entry_new(zathura_document_information_type_t type,
} }
zathura_document_information_entry_t* entry = zathura_document_information_entry_t* entry =
g_malloc0(sizeof(zathura_document_information_entry_t)); g_try_malloc0(sizeof(zathura_document_information_entry_t));
if (entry == NULL) {
return NULL;
}
entry->type = type; entry->type = type;
entry->value = g_strdup(value); entry->value = g_strdup(value);

11
utils.c
View file

@ -225,7 +225,11 @@ replace_substring(const char* string, const char* old, const char* new)
return NULL; return NULL;
} }
char* ret = g_malloc0(sizeof(char) * (i - count * old_len + count * new_len + 1)); char* ret = g_try_malloc0(sizeof(char) * (i - count * old_len + count * new_len + 1));
if (ret == NULL) {
return NULL;
}
i = 0; i = 0;
/* replace */ /* replace */
@ -249,7 +253,10 @@ GdkAtom* get_selection(zathura_t* zathura)
char* value; char* value;
girara_setting_get(zathura->ui.session, "selection-clipboard", &value); girara_setting_get(zathura->ui.session, "selection-clipboard", &value);
GdkAtom* selection = g_malloc(sizeof(GdkAtom)); GdkAtom* selection = g_try_malloc(sizeof(GdkAtom));
if (selection == NULL) {
return NULL;
}
if (strcmp(value, "primary") == 0) { if (strcmp(value, "primary") == 0) {
*selection = GDK_SELECTION_PRIMARY; *selection = GDK_SELECTION_PRIMARY;

View file

@ -53,7 +53,10 @@ static void zathura_jumplist_save(zathura_t* zathura);
zathura_t* zathura_t*
zathura_create(void) zathura_create(void)
{ {
zathura_t* zathura = g_malloc0(sizeof(zathura_t)); zathura_t* zathura = g_try_malloc0(sizeof(zathura_t));
if (zathura == NULL) {
return NULL;
}
/* global settings */ /* global settings */
zathura->global.search_direction = FORWARD; zathura->global.search_direction = FORWARD;
@ -821,7 +824,10 @@ document_open_idle(zathura_t* zathura, const char* path, const char* password,
return; return;
} }
zathura_document_info_t* document_info = g_malloc0(sizeof(zathura_document_info_t)); zathura_document_info_t* document_info = g_try_malloc0(sizeof(zathura_document_info_t));
if (document_info == NULL) {
return;
}
document_info->zathura = zathura; document_info->zathura = zathura;
document_info->path = path; document_info->path = path;
@ -1245,7 +1251,11 @@ zathura_jumplist_append_jump(zathura_t* zathura)
{ {
g_return_if_fail(zathura != NULL && zathura->jumplist.list != NULL); g_return_if_fail(zathura != NULL && zathura->jumplist.list != NULL);
zathura_jump_t *jump = g_malloc(sizeof(zathura_jump_t)); zathura_jump_t *jump = g_try_malloc(sizeof(zathura_jump_t));
if (jump == NULL) {
return;
}
jump->page = 0; jump->page = 0;
jump->x = 0.0; jump->x = 0.0;
jump->y = 0.0; jump->y = 0.0;