diff --git a/ChangeLog b/ChangeLog index d1a85e84..14d24727 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2007-11-17 Enrico Tröger + + * src/templates.c, src/utils.c, src/utils.h: + Fix possible memory leak and add time_t argument in + utils_get_date_time() to pass a time pointer to use. + + 2007-11-14 Nick Treleaven * plugins/vcdiff.c, plugins/Makefile.am: diff --git a/src/templates.c b/src/templates.c index 6d1adaf9..d6d412b8 100644 --- a/src/templates.c +++ b/src/templates.c @@ -350,8 +350,8 @@ static void create_new_menu_items() void templates_init(void) { - gchar *year = utils_get_date_time("%Y"); - gchar *date = utils_get_date_time(TEMPLATE_DATE_FORMAT); + gchar *year = utils_get_date_time("%Y", NULL); + gchar *date = utils_get_date_time(TEMPLATE_DATE_FORMAT, NULL); init_general_templates(year, date); init_ft_templates(year, date); @@ -523,7 +523,7 @@ gchar *templates_get_template_fileheader(gint filetype_idx, const gchar *fname) gchar *template = g_strdup(templates[GEANY_TEMPLATE_FILEHEADER]); gchar *shortname; gchar *result; - gchar *date = utils_get_date_time(TEMPLATE_DATETIME_FORMAT); + gchar *date = utils_get_date_time(TEMPLATE_DATETIME_FORMAT, NULL); filetype_id ft_id = filetype_idx; filetype *ft = filetypes[ft_id]; @@ -586,8 +586,8 @@ gchar *templates_get_template_generic(gint template) gchar *templates_get_template_function(gint filetype_idx, const gchar *func_name) { gchar *template = g_strdup(templates[GEANY_TEMPLATE_FUNCTION]); - gchar *date = utils_get_date_time(TEMPLATE_DATE_FORMAT); - gchar *datetime = utils_get_date_time(TEMPLATE_DATETIME_FORMAT); + gchar *date = utils_get_date_time(TEMPLATE_DATE_FORMAT, NULL); + gchar *datetime = utils_get_date_time(TEMPLATE_DATETIME_FORMAT, NULL); gchar *result; template = utils_str_replace(template, "{date}", date); @@ -605,7 +605,7 @@ gchar *templates_get_template_function(gint filetype_idx, const gchar *func_name gchar *templates_get_template_changelog(void) { - gchar *date = utils_get_date_time(TEMPLATE_DATETIME_FORMAT); + gchar *date = utils_get_date_time(TEMPLATE_DATETIME_FORMAT, NULL); gchar *result = g_strdup(templates[GEANY_TEMPLATE_CHANGELOG]); result = utils_str_replace(result, "{date}", date); diff --git a/src/utils.c b/src/utils.c index 7937d11a..50b448e4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -881,15 +881,22 @@ gint utils_strpos(const gchar *haystack, const gchar *needle) } -gchar *utils_get_date_time(const gchar *format) +gchar *utils_get_date_time(const gchar *format, time_t *time_to_use) { - time_t tp = time(NULL); - const struct tm *tm = localtime(&tp); - gchar *date = g_malloc0(256); + time_t tp; + const struct tm *tm; + gchar *date; if (format == NULL) return NULL; + if (time_to_use != NULL) + tp = *time_to_use; + else + tp = time(NULL); + + tm = localtime(&tp); + date = g_malloc0(256); strftime(date, 256, format, tm); return date; } diff --git a/src/utils.h b/src/utils.h index 9d8768c4..1f44fb10 100644 --- a/src/utils.h +++ b/src/utils.h @@ -99,7 +99,7 @@ gchar *utils_str_replace(gchar *haystack, const gchar *needle, const gchar *repl gint utils_strpos(const gchar* haystack, const gchar * needle); -gchar *utils_get_date_time(const gchar *format); +gchar *utils_get_date_time(const gchar *format, time_t *time_to_use); gchar *utils_get_initials(gchar *name);