Fix a bunch of memory allocation errors found by clang static analysis.

This commit is contained in:
John Ralls
2018-11-28 14:48:42 +09:00
parent de6c173ef4
commit faba7975ac
20 changed files with 95 additions and 85 deletions

View File

@@ -366,8 +366,6 @@ gnc_reset_warnings_dialog (GtkWindow *parent)
GtkWidget *dialog;
GtkBuilder *builder;
rw_dialog = g_new0 (RWDialog, 1);
ENTER("");
if (gnc_forall_gui_components(DIALOG_RESET_WARNINGS_CM_CLASS,
show_handler, NULL))
@@ -386,6 +384,7 @@ gnc_reset_warnings_dialog (GtkWindow *parent)
gtk_window_set_transient_for(GTK_WINDOW (dialog), parent);
rw_dialog = g_new0 (RWDialog, 1);
rw_dialog->dialog = dialog;
PINFO("rw_dialog %p, dialog %p", rw_dialog, dialog);

View File

@@ -315,8 +315,6 @@ gnc_totd_dialog (GtkWindow *parent, gboolean startup)
GtkTextView *textview;
gboolean show_tips;
totd_dialog = g_new0 (TotdDialog, 1);
show_tips = gnc_prefs_get_bool(GNC_PREFS_GROUP, GNC_PREF_SHOW_TIPS);
if (startup && !show_tips)
return;
@@ -350,6 +348,7 @@ gnc_totd_dialog (GtkWindow *parent, gboolean startup)
// Set the style context for this dialog so it can be easily manipulated with css
gnc_widget_set_style_context (GTK_WIDGET(dialog), "GncTotdDialog");
totd_dialog = g_new0 (TotdDialog, 1);
totd_dialog->dialog = dialog;
ENTER("totd_dialog %p, dialog %p", totd_dialog, dialog);

View File

@@ -4996,7 +4996,7 @@ gtv_sr_num_cb (GtkEntry *entry,
snprintf (buff, sizeof(buff), "%ld", number);
if (g_strcmp0 (buff, "") == 0)
leave_string = "";
leave_string = g_strdup ("");
else
leave_string = g_strdup (buff);
}

View File

@@ -2017,7 +2017,7 @@ void
loan_rev_hash_to_list( gpointer key, gpointer val, gpointer user_data )
{
GList **l = (GList**)user_data;
RevRepaymentRow *rrr = g_new0( RevRepaymentRow, 1 );
RevRepaymentRow *rrr;
if ( !key || !val )
{
DEBUG( "%.8x, %.8x",
@@ -2025,6 +2025,7 @@ loan_rev_hash_to_list( gpointer key, gpointer val, gpointer user_data )
GPOINTER_TO_UINT(val));
return;
}
rrr = g_new0( RevRepaymentRow, 1 );
rrr->date = *(GDate*)key;
rrr->numCells = (gnc_numeric*)val;
*l = g_list_append( *l, (gpointer)rrr );

View File

@@ -2187,6 +2187,7 @@ gnc_invoice_recreate_page (GncMainWindow *window,
goto give_up;
}
g_free(tmp_string);
tmp_string = NULL;
/* Get Owner Type */
owner_type = g_key_file_get_string(key_file, group_name,

View File

@@ -1529,6 +1529,7 @@ read_one_check_directory(PrintCheckDialog *pcd, GtkListStore *store,
found = TRUE;
}
}
free_check_format (format);
g_dir_close(dir);
/* If any files were added to the list, add a separator between

View File

@@ -789,7 +789,7 @@ gnc_sx_create_from_trans( GtkWindow *parent, Transaction *trans )
"from a Transaction currently "
"being edited. Please Enter the "
"Transaction before Scheduling." ) );
sxftd_close( sxfti, TRUE );
sxftd_destroy (NULL, sxfti);
return;
}
else

View File

@@ -2285,7 +2285,6 @@ static
void
gnc_split_reg_determine_read_only( GNCSplitReg *gsr )
{
dialog_args *args = g_malloc(sizeof(dialog_args));
SplitRegister *reg;
if (qof_book_is_readonly(gnc_get_current_book()))
@@ -2297,7 +2296,8 @@ gnc_split_reg_determine_read_only( GNCSplitReg *gsr )
if ( !gsr->read_only )
{
dialog_args *args;
char *string = NULL;
switch (gnc_split_reg_get_placeholder(gsr))
{
case PLACEHOLDER_NONE:
@@ -2305,14 +2305,14 @@ gnc_split_reg_determine_read_only( GNCSplitReg *gsr )
return;
case PLACEHOLDER_THIS:
args->string = _("This account may not be edited. If you want "
string = _("This account may not be edited. If you want "
"to edit transactions in this register, please "
"open the account options and turn off the "
"placeholder checkbox.");
break;
default:
args->string = _("One of the sub-accounts selected may not be "
string = _("One of the sub-accounts selected may not be "
"edited. If you want to edit transactions in "
"this register, please open the sub-account "
"options and turn off the placeholder checkbox. "
@@ -2322,6 +2322,8 @@ gnc_split_reg_determine_read_only( GNCSplitReg *gsr )
}
gsr->read_only = TRUE;
/* Put up a warning dialog */
args = g_malloc(sizeof(dialog_args));
args->string = string;
args->gsr = gsr;
g_timeout_add (250, gtk_callback_bug_workaround, args); /* 0.25 seconds */
}

View File

@@ -911,7 +911,7 @@ gsr2_determine_account_pr_dialog (gpointer argp)
static void
gnc_split_reg2_determine_account_pr (GNCSplitReg2 *gsr)
{
dialog_args *args = g_malloc (sizeof (dialog_args));
dialog_args *args;
GncTreeModelSplitReg *model;
model = gnc_ledger_display2_get_split_model_register (gsr->ledger);
@@ -920,6 +920,8 @@ gnc_split_reg2_determine_account_pr (GNCSplitReg2 *gsr)
return;
/* Put up a warning dialog */
args = g_malloc (sizeof (dialog_args));
args->string = _(""); /* FIXME: No string for dialog. */
args->gsr = gsr;
g_timeout_add (250, gsr2_determine_account_pr_dialog, args); /* 0.25 seconds */
}
@@ -958,7 +960,6 @@ static
void
gnc_split_reg2_determine_read_only (GNCSplitReg2 *gsr) //this works
{
dialog_args *args = g_malloc (sizeof (dialog_args));
if (qof_book_is_readonly (gnc_get_current_book()))
{
@@ -969,6 +970,7 @@ gnc_split_reg2_determine_read_only (GNCSplitReg2 *gsr) //this works
if (!gsr->read_only)
{
dialog_args *args = g_malloc (sizeof (dialog_args));
switch (gnc_split_reg2_get_placeholder (gsr))
{

View File

@@ -173,29 +173,32 @@ static void
mac_set_currency_locale(NSLocale *locale, NSString *locale_str)
{
/* If the currency doesn't match the base locale, we need to find a locale that does match, because setlocale won't know what to do with just a currency identifier. */
NSLocale *cur_locale = [[NSLocale alloc] initWithLocaleIdentifier: locale_str];
if (![[locale objectForKey: NSLocaleCurrencyCode] isEqualToString:
[[[NSLocale alloc] initWithLocaleIdentifier: locale_str] objectForKey: NSLocaleCurrencyCode]]) {
NSArray *all_locales = [NSLocale availableLocaleIdentifiers];
NSEnumerator *locale_iter = [all_locales objectEnumerator];
NSString *this_locale;
NSString *currency = [locale objectForKey: NSLocaleCurrencyCode];
NSString *money_locale = nil;
while ((this_locale = (NSString*)[locale_iter nextObject]))
{
NSLocale *templocale = [[NSLocale alloc]
initWithLocaleIdentifier: this_locale];
if ([[templocale objectForKey: NSLocaleCurrencyCode]
isEqualToString: currency])
{
money_locale = this_locale;
[templocale release];
break;
}
[templocale release];
}
if (money_locale)
setlocale(LC_MONETARY, [money_locale UTF8String]);
[cur_locale objectForKey: NSLocaleCurrencyCode]])
{
NSArray *all_locales = [NSLocale availableLocaleIdentifiers];
NSEnumerator *locale_iter = [all_locales objectEnumerator];
NSString *this_locale;
NSString *currency = [locale objectForKey: NSLocaleCurrencyCode];
NSString *money_locale = nil;
while ((this_locale = (NSString*)[locale_iter nextObject]))
{
NSLocale *templocale = [[NSLocale alloc]
initWithLocaleIdentifier: this_locale];
if ([[templocale objectForKey: NSLocaleCurrencyCode]
isEqualToString: currency])
{
money_locale = this_locale;
[templocale release];
break;
}
[templocale release];
}
if (money_locale)
setlocale(LC_MONETARY, [money_locale UTF8String]);
}
[cur_locale release];
}
/* The locale that we got from AppKit isn't a supported POSIX one, so we need to
* find something close. First see if we can find another locale for the
@@ -219,9 +222,7 @@ mac_find_close_country(NSString *locale_str, NSString *country_str,
new_locale = this_locale;
break;
}
if (new_locale)
locale_str = new_locale;
else
if (!new_locale)
while ((this_locale = (NSString*)[locale_iter nextObject]))
if ([[[NSLocale componentsFromLocaleIdentifier: this_locale]
objectForKey: NSLocaleLanguageCode]

View File

@@ -879,15 +879,15 @@ gnc_bi_import_create_bis (GtkListStore * store, QofBook * book,
static char * un_escape(char *str)
{
gchar quote = '"';
gchar *newStr = NULL;
gchar *newStr = NULL, *tmpstr = str;
int n = 0;
newStr = g_malloc(strlen(str)+1); // This must be freed in the calling code.
while(*str != '\0')
while(*tmpstr != '\0')
{
if(*str == quote)
if(*tmpstr == quote)
{
str++;
if(*str == quote)
tmpstr++;
if(*tmpstr == quote)
{
newStr[n] = quote;
}
@@ -896,9 +896,10 @@ static char * un_escape(char *str)
{
newStr[n] = *str;
}
str++;
tmpstr++;
n++;
}
}
g_free (str);
newStr[n] = '\0'; //ending the character array
return newStr;
}

View File

@@ -336,7 +336,6 @@ gnc_customer_import_create_customers (GtkListStore *store, QofBook *book, guint
// Set the customer id if one has not been chosen
if (strlen (id) == 0)
{
g_free (id);
if (g_ascii_strcasecmp (type, "CUSTOMER") == 0) id = gncCustomerNextID (book);
else if (g_ascii_strcasecmp (type, "VENDOR") == 0)id = gncVendorNextID (book);
//printf("ASSIGNED ID = %s\n",id);
@@ -413,6 +412,7 @@ gnc_customer_import_create_customers (GtkListStore *store, QofBook *book, guint
//printf("TYPE %s created with ID = %s.\n", type, id); // DEBUG
}
g_free (id);
g_free (company);
g_free (name);
g_free (addr1);

View File

@@ -230,6 +230,7 @@ make_predefined_vars_from_external_helper( gpointer key, gpointer value, gpointe
pnum->value = *(gnc_numeric*)value;
make_predefined_vars_helper( key, pnum, data );
g_free(pnum); /* make_predefined_vars_helper allocs its own copy. */
}
static var_store_ptr

View File

@@ -378,6 +378,7 @@ GncXmlBackend::write_to_file (bool make_backup)
if (!mktemp (tmp_name))
{
g_free (tmp_name);
set_error(ERR_BACKEND_MISC);
set_message("Failed to make temp file");
LEAVE ("");
@@ -388,6 +389,7 @@ GncXmlBackend::write_to_file (bool make_backup)
{
if (!backup_file ())
{
g_free (tmp_name);
LEAVE ("");
return FALSE;
}
@@ -513,6 +515,7 @@ GncXmlBackend::write_to_file (bool make_backup)
LEAVE ("");
return FALSE;
}
g_free (tmp_name);
LEAVE ("");
return TRUE;
}

View File

@@ -765,49 +765,42 @@ kvp_frame_slot_end_handler (gpointer data_for_children,
{
KvpFrame* f = (KvpFrame*) parent_data;
GSList* lp;
guint64 key_node_count;
gboolean first = TRUE;
gchar* key = NULL;
sixtp_child_result* value_cr = NULL;
KvpValue* value = NULL;
gboolean delete_value = FALSE;
sixtp_child_result *cr1 = NULL, *cr2 = NULL, *cr = NULL;
g_return_val_if_fail (f, FALSE);
if (g_slist_length (data_from_children) != 2) return (FALSE);
/* check to see that we got exactly one <key> node */
lp = data_from_children;
key_node_count = 0;
for (lp = data_from_children; lp; lp = lp->next)
cr1 = (sixtp_child_result*)data_from_children->data;
cr2 = (sixtp_child_result*)data_from_children->next->data;
if (is_child_result_from_node_named(cr1, "k"))
{
sixtp_child_result* cr = (sixtp_child_result*) lp->data;
if (is_child_result_from_node_named (cr, "k"))
{
key = (char*) cr->data;
key_node_count++;
}
else
{
if (is_child_result_from_node_named (cr, "frame"))
{
KvpFrame* frame = static_cast<KvpFrame*> (cr->data);
value = new KvpValue {frame};
delete_value = TRUE;
}
else
{
value = static_cast<KvpValue*> (cr->data);
delete_value = FALSE;
}
value_cr = cr;
}
key = (char*)cr1->data;
cr = cr2;
}
if (key_node_count != 1) return (FALSE);
value_cr->should_cleanup = TRUE;
else if (is_child_result_from_node_named(cr2, "k"))
{
key = (char*)cr2->data;
cr = cr1;
}
else
return FALSE;
if (is_child_result_from_node_named (cr, "frame"))
{
KvpFrame* frame = static_cast<KvpFrame*> (cr->data);
value = new KvpValue {frame};
delete_value = TRUE;
}
else
{
value = static_cast<KvpValue*> (cr->data);
delete_value = FALSE;
}
f->set ({key}, value);
if (delete_value)
delete value;

View File

@@ -1868,6 +1868,7 @@ gnc_xml2_find_ambiguous (const gchar* filename, GList* encodings,
if (iconv_item->iconv == (GIConv) - 1)
{
PWARN ("Unable to open IConv conversion descriptor for '%s'", enc);
g_free (iconv_item);
goto cleanup_find_ambs;
}
else
@@ -2176,7 +2177,8 @@ gnc_xml2_parse_with_subst (GncXmlBackend* xml_be, QofBook* book, GHashTable* sub
success = qof_session_load_from_xml_file_v2_full (
xml_be, book, (sixtp_push_handler) parse_with_subst_push_handler,
push_data, GNC_BOOK_XML2_FILE);
g_free (push_data);
if (success)
qof_instance_set_dirty (QOF_INSTANCE (book));

View File

@@ -267,20 +267,22 @@ sixtp_destroy_child (gpointer key, gpointer value, gpointer user_data)
gpointer lookup_value;
g_debug ("Killing sixtp child under key <%s>", key ? (char*) key : "(null)");
g_free (key);
if (!corpses)
{
g_critical ("no corpses in sixtp_destroy_child <%s>",
key ? (char*) key : "(null)");
g_free (key);
return;
}
if (!child)
{
g_critical ("no child in sixtp_destroy_child <%s>",
key ? (char*) key : "");
g_free (key);
return;
}
g_free (key);
if (!g_hash_table_lookup_extended (corpses, (gconstpointer) child,
&lookup_key, &lookup_value))

View File

@@ -146,9 +146,10 @@ gnc_environment_parse_one (const gchar *env_path)
/* Remove the "x" from our result */
if (g_strcmp0 (tmp_val, "x"))
{
new_val = g_strdup (tmp_val + sizeof (G_SEARCHPATH_SEPARATOR_S));
g_free (tmp_val);
g_free (tmp_val);
}
if (!g_setenv (env_vars[i], new_val, TRUE))
g_warning ("Couldn't properly override environment variable \"%s\". "
"This may lead to unexpected results", env_vars[i]);

View File

@@ -2457,8 +2457,8 @@ gnc_commodity_table_destroy(gnc_commodity_table * t)
t->ns_list = NULL;
g_hash_table_destroy(t->ns_table);
t->ns_table = NULL;
g_free(t);
LEAVE ("table=%p", t);
g_free(t);
}
/* =========================================================== */

View File

@@ -234,6 +234,7 @@ qof_collection_from_glist (QofIdType type, const GList *glist)
ent = QOF_INSTANCE(list->data);
if (FALSE == qof_collection_add_entity(coll, ent))
{
qof_collection_destroy(coll);
return NULL;
}
}