Add defensive checks for function receiving a GeanyDocument argument

Always check the passed-in GeanyDocument argument is a valid one, not
to possibly work on an invalid document.
This commit is contained in:
Colomban Wendling 2014-01-18 15:12:22 +01:00
parent c4549b1e73
commit 7473e4b1d9
13 changed files with 58 additions and 18 deletions

View File

@ -338,6 +338,8 @@ static GeanyBuildCommand *get_next_build_cmd(GeanyDocument *doc, guint cmdgrp, g
GeanyFiletype *ft = NULL;
guint sink, *fr = &sink;
g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
if (printbuildcmds)
printfcmds();
if (cmdgrp >= GEANY_GBG_COUNT)
@ -716,6 +718,8 @@ static gchar *build_replace_placeholder(const GeanyDocument *doc, const gchar *s
gchar *executable = NULL;
gchar *ret_str; /* to be freed when not in use anymore */
g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
stack = g_string_new(src);
if (doc != NULL && doc->file_name != NULL)
{
@ -780,6 +784,8 @@ static GPid build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *d
gint stderr_fd;
#endif
g_return_val_if_fail(doc == NULL || doc->is_valid, (GPid) -1);
if (!((doc != NULL && !EMPTY(doc->file_name)) || !EMPTY(dir)))
{
geany_debug("Failed to run command with no working directory");
@ -942,7 +948,7 @@ static GPid build_run_cmd(GeanyDocument *doc, guint cmdindex)
gchar *vte_cmd_nonscript = NULL;
GError *error = NULL;
if (doc == NULL || doc->file_name == NULL)
if (! DOC_VALID(doc) || doc->file_name == NULL)
return (GPid) 0;
working_dir = prepare_run_script(doc, &vte_cmd_nonscript, cmdindex);
@ -1583,6 +1589,8 @@ void build_menu_update(GeanyDocument *doc)
gboolean can_compile, can_build, can_make, run_sensitivity = FALSE, run_running = FALSE;
GeanyBuildCommand *bc;
g_return_if_fail(doc == NULL || doc->is_valid);
if (menu_items.menu == NULL)
create_build_menu(&menu_items);
if (doc == NULL)
@ -1977,6 +1985,8 @@ static RowWidgets *build_add_dialog_row(GeanyDocument *doc, GtkTable *table, gui
guint column = 0;
gchar *text;
g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
text = g_strdup_printf("%d.", cmd + 1);
label = gtk_label_new(text);
g_free(text);

View File

@ -591,7 +591,7 @@ static gboolean show_save_as_gtk(GeanyDocument *doc)
GtkWidget *dialog;
gint resp;
g_return_val_if_fail(doc != NULL, FALSE);
g_return_val_if_fail(DOC_VALID(doc), FALSE);
dialog = create_save_file_dialog(doc);
@ -1172,6 +1172,8 @@ void dialogs_show_file_properties(GeanyDocument *doc)
# define S_IXOTH 0
#endif
g_return_if_fail(doc == NULL || doc->is_valid);
if (doc == NULL || doc->file_name == NULL)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR,

View File

@ -1099,6 +1099,8 @@ GeanyDocument *document_open_file_full(GeanyDocument *doc, const gchar *filename
GeanyFiletype *use_ft;
FileData filedata;
g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
if (reload)
{
utf8_filename = g_strdup(doc->file_name);

View File

@ -1056,6 +1056,8 @@ GeanyFiletype *filetypes_detect_from_document(GeanyDocument *doc)
gchar *lines[GEANY_FILETYPE_SEARCH_LINES + 1];
gint i;
g_return_val_if_fail(doc == NULL || doc->is_valid, filetypes[GEANY_FILETYPES_NONE]);
if (doc == NULL)
return filetypes[GEANY_FILETYPES_NONE];

View File

@ -1041,6 +1041,8 @@ static void trigger_button_event(GtkWidget *widget, guint32 event_time)
* notebook tab list. */
static gboolean check_menu_key(GeanyDocument *doc, guint keyval, guint state, guint32 event_time)
{
g_return_val_if_fail(doc == NULL || doc->is_valid, FALSE);
if ((keyval == GDK_Menu && state == 0) || (keyval == GDK_F10 && state == GDK_SHIFT_MASK))
{
GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
@ -1585,8 +1587,7 @@ static gboolean cb_func_build_action(guint key_id)
static gboolean read_current_word(GeanyDocument *doc, gboolean sci_word)
{
if (doc == NULL)
return FALSE;
g_return_val_if_fail(DOC_VALID(doc), FALSE);
if (sci_word)
{
@ -1820,8 +1821,7 @@ static void goto_matching_brace(GeanyDocument *doc)
gint pos, new_pos;
gint after_brace;
if (doc == NULL)
return;
g_return_if_fail(DOC_VALID(doc));
pos = sci_get_current_position(doc->editor->sci);
after_brace = pos > 0 && utils_isbrace(sci_get_char_at(doc->editor->sci, pos - 1), TRUE);

View File

@ -150,7 +150,8 @@ gboolean navqueue_goto_line(GeanyDocument *old_doc, GeanyDocument *new_doc, gint
{
gint pos;
g_return_val_if_fail(new_doc != NULL, FALSE);
g_return_val_if_fail(old_doc == NULL || old_doc->is_valid, FALSE);
g_return_val_if_fail(DOC_VALID(new_doc), FALSE);
g_return_val_if_fail(line >= 1, FALSE);
pos = sci_get_position_from_line(new_doc->editor->sci, line - 1);

View File

@ -624,8 +624,7 @@ static void print_external(GeanyDocument *doc)
void printing_print_doc(GeanyDocument *doc)
{
if (doc == NULL)
return;
g_return_if_fail(DOC_VALID(doc));
if (printing_prefs.use_gtk_printing)
printing_print_gtk(doc);

View File

@ -394,7 +394,7 @@ void search_find_selection(GeanyDocument *doc, gboolean search_backwards)
{
gchar *s = NULL;
g_return_if_fail(doc != NULL);
g_return_if_fail(DOC_VALID(doc));
#ifdef G_OS_UNIX
if (search_prefs.find_selection_type == GEANY_FIND_SEL_X)
@ -1225,7 +1225,7 @@ gint search_mark_all(GeanyDocument *doc, const gchar *search_text, gint flags)
struct Sci_TextToFind ttf;
GSList *match, *matches;
g_return_val_if_fail(doc != NULL, 0);
g_return_val_if_fail(DOC_VALID(doc), 0);
/* clear previous search indicators */
editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
@ -2134,7 +2134,7 @@ static gint find_document_usage(GeanyDocument *doc, const gchar *search_text, gi
gint prev_line = -1;
GSList *match, *matches;
g_return_val_if_fail(doc != NULL, 0);
g_return_val_if_fail(DOC_VALID(doc), 0);
short_file_name = g_path_get_basename(DOC_FILENAME(doc));

View File

@ -187,6 +187,8 @@ void sidebar_update_tag_list(GeanyDocument *doc, gboolean update)
{
GtkWidget *child = gtk_bin_get_child(GTK_BIN(tag_window));
g_return_if_fail(doc == NULL || doc->is_valid);
/* changes the tree view to the given one, trying not to do useless changes */
#define CHANGE_TREE(new_child) \
G_STMT_START { \

View File

@ -1715,7 +1715,7 @@ gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
{
GList *tags;
g_return_val_if_fail(doc != NULL, FALSE);
g_return_val_if_fail(DOC_VALID(doc), FALSE);
tags = get_tag_list(doc, tm_tag_max_t);
if (tags == NULL)
@ -2226,6 +2226,8 @@ static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagnam
static gint tag_line = -1;
static gchar *cur_tag = NULL;
g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
if (doc == NULL) /* reset current function */
{
current_tag_changed(NULL, -1, -1, 0);

View File

@ -107,6 +107,8 @@ static void convert_eol_characters(GString *template, GeanyDocument *doc)
{
gint doc_eol_mode;
g_return_if_fail(doc == NULL || doc->is_valid);
if (doc == NULL)
doc = document_get_current();
@ -436,7 +438,7 @@ gchar *templates_get_template_licence(GeanyDocument *doc, gint licence_type)
{
GString *template;
g_return_val_if_fail(doc != NULL, NULL);
g_return_val_if_fail(DOC_VALID(doc), NULL);
g_return_val_if_fail(licence_type == GEANY_TEMPLATE_GPL || licence_type == GEANY_TEMPLATE_BSD, NULL);
template = g_string_new(templates[licence_type]);
@ -502,9 +504,13 @@ gchar *templates_get_template_function(GeanyDocument *doc, const gchar *func_nam
gchar *templates_get_template_changelog(GeanyDocument *doc)
{
GString *result = g_string_new(templates[GEANY_TEMPLATE_CHANGELOG]);
const gchar *file_type_name = (doc != NULL) ? doc->file_type->name : "";
GString *result;
const gchar *file_type_name;
g_return_val_if_fail(DOC_VALID(doc), NULL);
result = g_string_new(templates[GEANY_TEMPLATE_CHANGELOG]);
file_type_name = (doc->file_type != NULL) ? doc->file_type->name : "";
replace_static_values(result);
templates_replace_default_dates(result);
templates_replace_command(result, DOC_FILENAME(doc), file_type_name, NULL);

View File

@ -356,7 +356,7 @@ void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
gint stdout_fd;
gint stderr_fd;
g_return_if_fail(doc != NULL && command != NULL);
g_return_if_fail(DOC_VALID(doc) && command != NULL);
if (! sci_has_selection(doc->editor->sci))
editor_select_lines(doc->editor, FALSE);
@ -697,7 +697,7 @@ static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_d
GeanyDocument *doc = document_get_current();
gint command_idx;
g_return_if_fail(doc != NULL);
g_return_if_fail(DOC_VALID(doc));
command_idx = GPOINTER_TO_INT(user_data);

View File

@ -317,6 +317,8 @@ static gchar *create_statusbar_statistics(GeanyDocument *doc,
/* updates the status bar document statistics */
void ui_update_statusbar(GeanyDocument *doc, gint pos)
{
g_return_if_fail(doc == NULL || doc->is_valid);
if (! interface_prefs.statusbar_visible)
return; /* just do nothing if statusbar is not visible */
@ -359,6 +361,8 @@ void ui_set_window_title(GeanyDocument *doc)
GString *str;
GeanyProject *project = app->project;
g_return_if_fail(doc == NULL || doc->is_valid);
if (doc == NULL)
doc = document_get_current();
@ -445,6 +449,8 @@ void ui_update_popup_reundo_items(GeanyDocument *doc)
gboolean enable_redo;
guint i, len;
g_return_if_fail(doc == NULL || doc->is_valid);
if (doc == NULL)
{
enable_undo = FALSE;
@ -475,6 +481,8 @@ void ui_update_popup_copy_items(GeanyDocument *doc)
gboolean enable;
guint i, len;
g_return_if_fail(doc == NULL || doc->is_valid);
if (doc == NULL)
enable = FALSE;
else
@ -501,6 +509,8 @@ void ui_update_menu_copy_items(GeanyDocument *doc)
guint i, len;
GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
g_return_if_fail(doc == NULL || doc->is_valid);
if (IS_SCINTILLA(focusw))
enable = (doc == NULL) ? FALSE : sci_has_selection(doc->editor->sci);
else
@ -524,6 +534,8 @@ void ui_update_insert_include_item(GeanyDocument *doc, gint item)
{
gboolean enable = FALSE;
g_return_if_fail(doc == NULL || doc->is_valid);
if (doc == NULL || doc->file_type == NULL)
enable = FALSE;
else if (doc->file_type->id == GEANY_FILETYPES_C || doc->file_type->id == GEANY_FILETYPES_CPP)
@ -892,6 +904,8 @@ void ui_document_show_hide(GeanyDocument *doc)
GtkWidget *item;
const GeanyIndentPrefs *iprefs;
g_return_if_fail(doc == NULL || doc->is_valid);
if (doc == NULL)
doc = document_get_current();