Make utils_build_path() return a copy for safety.

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@5721 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
Nick Treleaven 2011-04-13 12:59:20 +00:00
parent 0fc97a1177
commit 8a817e694b
9 changed files with 65 additions and 36 deletions

View File

@ -5,6 +5,9 @@
about parameter addresses.
* src/document.h:
Use brackets for DOC_FILENAME() macro 'doc' argument.
* src/templates.c, src/utils.c, src/toolbar.c, src/utils.h,
src/keyfile.c, src/filetypes.c, src/editor.c, src/symbols.c:
Make utils_build_path() return a copy for safety.
2011-04-12 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>

View File

@ -4814,15 +4814,17 @@ void editor_destroy(GeanyEditor *editor)
static void on_document_save(GObject *obj, GeanyDocument *doc)
{
gchar *f = utils_build_path(app->configdir, "snippets.conf", NULL);
g_return_if_fail(NZV(doc->real_path));
if (utils_str_equal(doc->real_path,
utils_build_path(app->configdir, "snippets.conf", NULL)))
if (utils_str_equal(doc->real_path, f))
{
/* reload snippets */
editor_snippets_free();
editor_snippets_init();
}
g_free(f);
}
@ -4849,6 +4851,7 @@ gboolean editor_complete_word_part(GeanyEditor *editor)
void editor_init(void)
{
static GeanyIndentPrefs indent_prefs;
gchar *f;
memset(&editor_prefs, 0, sizeof(GeanyEditorPrefs));
memset(&indent_prefs, 0, sizeof(GeanyIndentPrefs));
@ -4858,8 +4861,9 @@ void editor_init(void)
* handler (on_editor_notify) is called */
g_signal_connect_after(geany_object, "editor-notify", G_CALLBACK(on_editor_notify), NULL);
ui_add_config_file_menu_item(utils_build_path(app->configdir, "snippets.conf", NULL),
NULL, NULL);
f = utils_build_path(app->configdir, "snippets.conf", NULL);
ui_add_config_file_menu_item(f, NULL, NULL);
g_free(f);
g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
}

View File

@ -609,6 +609,7 @@ static void init_custom_filetypes(const gchar *path)
void filetypes_init_types()
{
filetype_id ft_id;
gchar *f;
g_return_if_fail(filetypes_array == NULL);
g_return_if_fail(filetypes_hash == NULL);
@ -629,7 +630,9 @@ void filetypes_init_types()
filetype_add(filetypes[ft_id]);
}
init_custom_filetypes(app->datadir);
init_custom_filetypes(utils_build_path(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL));
f = utils_build_path(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL);
init_custom_filetypes(f);
g_free(f);
/* sort last instead of on insertion to prevent exponential time */
filetypes_by_title = g_slist_sort_with_data(filetypes_by_title,
@ -639,13 +642,17 @@ void filetypes_init_types()
static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
{
gchar *f;
g_return_if_fail(NZV(doc->real_path));
if (utils_str_equal(doc->real_path,
utils_build_path(app->configdir, "filetype_extensions.conf", NULL)))
f = utils_build_path(app->configdir, "filetype_extensions.conf", NULL);
if (utils_str_equal(doc->real_path, f))
filetypes_read_extensions();
else if (utils_str_equal(doc->real_path,
utils_build_path(app->configdir, GEANY_FILEDEFS_SUBDIR, "filetypes.common", NULL)))
g_free(f);
f = utils_build_path(app->configdir, GEANY_FILEDEFS_SUBDIR, "filetypes.common", NULL);
if (utils_str_equal(doc->real_path, f))
{
guint i;
@ -656,15 +663,19 @@ static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
foreach_document(i)
document_reload_config(documents[i]);
}
g_free(f);
}
static void setup_config_file_menus(void)
{
ui_add_config_file_menu_item(
utils_build_path(app->configdir, "filetype_extensions.conf", NULL), NULL, NULL);
ui_add_config_file_menu_item(
utils_build_path(app->configdir, GEANY_FILEDEFS_SUBDIR, "filetypes.common", NULL), NULL, NULL);
gchar *f;
f = utils_build_path(app->configdir, "filetype_extensions.conf", NULL);
ui_add_config_file_menu_item(f, NULL, NULL);
setptr(f, utils_build_path(app->configdir, GEANY_FILEDEFS_SUBDIR, "filetypes.common", NULL));
ui_add_config_file_menu_item(f, NULL, NULL);
g_free(f);
g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
}
@ -770,7 +781,7 @@ static gboolean match_basename(gconstpointer pft, gconstpointer user_data)
static GeanyFiletype *check_builtin_filenames(const gchar *utf8_filename)
{
gchar *lfn = NULL;
const gchar *path;
gchar *path;
gboolean found = FALSE;
#ifdef G_OS_WIN32
@ -785,10 +796,11 @@ static GeanyFiletype *check_builtin_filenames(const gchar *utf8_filename)
if (g_str_has_prefix(lfn, path))
found = TRUE;
path = utils_build_path(app->datadir, "filetypes.", NULL);
setptr(path, utils_build_path(app->datadir, "filetypes.", NULL));
if (g_str_has_prefix(lfn, path))
found = TRUE;
g_free(path);
g_free(lfn);
return found ? filetypes[GEANY_FILETYPES_CONF] : NULL;
}

View File

@ -962,16 +962,17 @@ void configuration_reload_default_session(void)
gboolean configuration_load(void)
{
const gchar *configfile = utils_build_path(app->configdir, "geany.conf", NULL);
gchar *configfile = utils_build_path(app->configdir, "geany.conf", NULL);
GKeyFile *config = g_key_file_new();
if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
{ /* config file does not (yet) exist, so try to load a global config file which may be */
/* created by distributors */
geany_debug("No user config file found, trying to use global configuration.");
configfile = utils_build_path(app->datadir, "geany.conf", NULL);
setptr(configfile, utils_build_path(app->datadir, "geany.conf", NULL));
}
g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
g_free(configfile);
load_dialog_prefs(config);
load_ui_prefs(config);

View File

@ -1729,7 +1729,7 @@ static GHashTable *init_user_tags(void)
{
GSList *file_list = NULL, *list = NULL;
GHashTable *lang_hash;
const gchar *dir;
gchar *dir;
dir = utils_build_path(app->configdir, "tags", NULL);
/* create the user tags dir for next time if it doesn't exist */
@ -1739,8 +1739,10 @@ static GHashTable *init_user_tags(void)
}
file_list = utils_get_file_list_full(dir, TRUE, TRUE, NULL);
dir = utils_build_path(app->datadir, "tags", NULL);
list = utils_get_file_list_full(dir, TRUE, TRUE, NULL);
setptr(dir, utils_build_path(app->datadir, "tags", NULL));
list = utils_get_file_list_full(dir, TRUE, TRUE, NULL);
g_free(dir);
file_list = g_slist_concat(file_list, list);
lang_hash = get_tagfile_hash(file_list);
@ -2168,20 +2170,27 @@ static void create_taglist_popup_menu(void)
static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
{
gchar *f = utils_build_path(app->configdir, "ignore.tags", NULL);
g_return_if_fail(NZV(doc->real_path));
if (utils_str_equal(doc->real_path,
utils_build_path(app->configdir, "ignore.tags", NULL)))
if (utils_str_equal(doc->real_path, f))
load_c_ignore_tags();
g_free(f);
}
void symbols_init(void)
{
gchar *f;
create_taglist_popup_menu();
ui_add_config_file_menu_item(utils_build_path(app->configdir, "ignore.tags", NULL),
NULL, NULL);
f = utils_build_path(app->configdir, "ignore.tags", NULL);
ui_add_config_file_menu_item(f, NULL, NULL);
g_free(f);
g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
}

View File

@ -327,7 +327,7 @@ static void on_file_menu_hide(GtkWidget *item)
/* reload templates if any file in the templates path is saved */
static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
{
const gchar *path = utils_build_path(app->configdir, GEANY_TEMPLATES_SUBDIR, NULL);
gchar *path = utils_build_path(app->configdir, GEANY_TEMPLATES_SUBDIR, NULL);
g_return_if_fail(NZV(doc->real_path));
@ -337,6 +337,7 @@ static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
templates_free_templates();
templates_init();
}
g_free(path);
}

View File

@ -177,7 +177,7 @@ static GtkWidget *toolbar_reload(const gchar *markup)
GSList *l;
GtkWidget *entry;
GError *error = NULL;
const gchar *filename;
gchar *filename;
static guint merge_id = 0;
GtkWidget *toolbar_new_file_menu = NULL;
GtkWidget *toolbar_recent_files_menu = NULL;
@ -226,9 +226,10 @@ static GtkWidget *toolbar_reload(const gchar *markup)
g_error_free(error);
error = NULL;
filename = utils_build_path(app->datadir, "ui_toolbar.xml", NULL);
setptr(filename, utils_build_path(app->datadir, "ui_toolbar.xml", NULL));
merge_id = gtk_ui_manager_add_ui_from_file(uim, filename, &error);
}
g_free(filename);
}
if (error != NULL)
{
@ -890,7 +891,7 @@ editor in Geany.\n\n\
A list of available actions can be found in the documentation included with Geany or\n\
at http://www.geany.org/manual/current/index.html#customizing-the-toolbar.\n-->\n\
\t<toolbar name='GeanyToolbar'>\n";
const gchar *filename = utils_build_path(app->configdir, "ui_toolbar.xml", NULL);
gchar *filename;
GString *str = g_string_new(template);
gtk_tree_model_foreach(GTK_TREE_MODEL(tbw->store_used), tb_editor_foreach_used, str);
@ -899,7 +900,9 @@ at http://www.geany.org/manual/current/index.html#customizing-the-toolbar.\n-->\
toolbar_reload(str->str);
filename = utils_build_path(app->configdir, "ui_toolbar.xml", NULL);
utils_write_file(filename, str->str);
g_free(filename);
g_string_free(str, TRUE);
}

View File

@ -1741,12 +1741,8 @@ static gboolean utils_string_vappend(GString *buffer, const gchar *sep, va_list
}
/* Similar to g_build_path() but (re)using a fixed buffer, so never free it.
* This assumes a small enough resulting string length to be kept without freeing,
* but this should be the case for filenames.
* @warning As the buffer is reused, you can't call this recursively, e.g. for a
* function argument and within the function called. */
const gchar *utils_build_path(const gchar *first, ...)
/* Like g_build_path() but without first argument. */
gchar *utils_build_path(const gchar *first, ...)
{
static GString *buffer = NULL;
va_list args;
@ -1761,7 +1757,7 @@ const gchar *utils_build_path(const gchar *first, ...)
va_start(args, first);
utils_string_vappend(buffer, G_DIR_SEPARATOR_S, args);
va_end(args);
return buffer->str;
return g_strdup(buffer->str);
}

View File

@ -247,7 +247,7 @@ gboolean utils_spawn_async(const gchar *dir, gchar **argv, gchar **env, GSpawnFl
gint utils_str_casecmp(const gchar *s1, const gchar *s2);
const gchar *utils_build_path(const gchar *first, ...) G_GNUC_NULL_TERMINATED;
gchar *utils_build_path(const gchar *first, ...) G_GNUC_NULL_TERMINATED;
gchar *utils_make_filename(const gchar *path, ...) G_GNUC_NULL_TERMINATED;