From 8ef8acb7c70a321963d30f2264e8f91e16427fcf Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 7 Apr 2009 21:02:14 -0700 Subject: [PATCH] Add "x" prefix to calls to plain malloc(), calloc(), strdup(), realloc(). In review commit 503f53bfdde "Read dictionary encoding from data files" I noticed uses of plain strdup() (not xstrdup()). Some greps showed that there were several other uses of strdup(), as well as calloc(), malloc(), and realloc(), in the source tree. This commit adds "x" prefixes to each of them, to ensure proper error handling. --- src/data/dictionary.c | 6 +++--- src/data/gnumeric-reader.c | 4 ++-- src/data/sys-file-reader.c | 2 +- src/language/data-io/get-data.c | 12 +++++++----- src/language/stats/t-test.q | 8 ++++---- src/libpspp/hmap.c | 6 ++++-- src/libpspp/i18n.c | 12 ++++++------ src/libpspp/message.c | 4 ++-- src/ui/gui/helper.c | 4 ++-- src/ui/gui/psppire-syntax-window.c | 4 +++- src/ui/gui/psppire-window.c | 4 ++-- src/ui/gui/psppire.c | 4 ++-- src/ui/gui/recode-dialog.c | 4 ++-- src/ui/gui/select-cases-dialog.c | 4 ++-- src/ui/gui/t-test-independent-samples-dialog.c | 12 ++++++------ src/ui/gui/widget-io.c | 5 +++-- tests/dissect-sysfile.c | 4 ++-- 17 files changed, 53 insertions(+), 46 deletions(-) diff --git a/src/data/dictionary.c b/src/data/dictionary.c index b5b6cca5..2dd1dfc4 100644 --- a/src/data/dictionary.c +++ b/src/data/dictionary.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 2007, 2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -79,7 +79,7 @@ void dict_set_encoding (struct dictionary *d, const char *enc) { if (enc) - d->encoding = strdup (enc); + d->encoding = xstrdup (enc); } const char * @@ -213,7 +213,7 @@ dict_clone (const struct dictionary *s) d->vector[i] = vector_clone (s->vector[i], s, d); if ( s->encoding) - d->encoding = strdup (s->encoding); + d->encoding = xstrdup (s->encoding); dict_set_attributes (d, dict_get_attributes (s)); diff --git a/src/data/gnumeric-reader.c b/src/data/gnumeric-reader.c index f2f4e52f..2e92e3f5 100644 --- a/src/data/gnumeric-reader.c +++ b/src/data/gnumeric-reader.c @@ -448,7 +448,7 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict) if ( idx >= n_var_specs ) { n_var_specs = idx + 1 ; - var_spec = realloc (var_spec, sizeof (*var_spec) * n_var_specs); + var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs); var_spec [idx].name = NULL; var_spec [idx].width = -1; var_spec [idx].first_value = NULL; @@ -463,7 +463,7 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict) { if ( gri->read_names ) { - var_spec [idx].name = strdup (text); + var_spec [idx].name = xstrdup (text); } } else diff --git a/src/data/sys-file-reader.c b/src/data/sys-file-reader.c index 86607bcc..eff114a5 100644 --- a/src/data/sys-file-reader.c +++ b/src/data/sys-file-reader.c @@ -781,7 +781,7 @@ read_extension_record (struct sfm_reader *r, struct dictionary *dict, /* New in SPSS 16. Contains a single string that describes the character encoding, e.g. "windows-1252". */ { - char *encoding = calloc (size, count + 1); + char *encoding = xcalloc (size, count + 1); read_string (r, encoding, count + 1); dict_set_encoding (dict, encoding); return; diff --git a/src/language/data-io/get-data.c b/src/language/data-io/get-data.c index 54bb5653..e4ab76a9 100644 --- a/src/language/data-io/get-data.c +++ b/src/language/data-io/get-data.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2007, 2008 Free Software Foundation, Inc. + Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -34,6 +34,8 @@ #include #include +#include "xalloc.h" + #include "gettext.h" #define _(msgid) gettext (msgid) #define N_(msgid) (msgid) @@ -83,7 +85,7 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds) if (!lex_force_string (lexer)) goto error; - psql.conninfo = strdup (ds_cstr (lex_tokstr (lexer))); + psql.conninfo = xstrdup (ds_cstr (lex_tokstr (lexer))); lex_get (lexer); @@ -151,7 +153,7 @@ parse_get_gnm (struct lexer *lexer, struct dataset *ds) if (!lex_force_string (lexer)) goto error; - gri.file_name = strdup (ds_cstr (lex_tokstr (lexer))); + gri.file_name = xstrdup (ds_cstr (lex_tokstr (lexer))); lex_get (lexer); @@ -170,7 +172,7 @@ parse_get_gnm (struct lexer *lexer, struct dataset *ds) if ( ! lex_force_string (lexer) ) goto error; - gri.sheet_name = strdup (ds_cstr (lex_tokstr (lexer))); + gri.sheet_name = xstrdup (ds_cstr (lex_tokstr (lexer))); gri.sheet_index = -1; } else if (lex_match_id (lexer, "INDEX")) @@ -194,7 +196,7 @@ parse_get_gnm (struct lexer *lexer, struct dataset *ds) if ( ! lex_force_string (lexer) ) goto error; - gri.cell_range = strdup (ds_cstr (lex_tokstr (lexer))); + gri.cell_range = xstrdup (ds_cstr (lex_tokstr (lexer))); } else goto error; diff --git a/src/language/stats/t-test.q b/src/language/stats/t-test.q index 5f5fc56f..466bf9e7 100644 --- a/src/language/stats/t-test.q +++ b/src/language/stats/t-test.q @@ -749,15 +749,15 @@ ssbox_independent_samples_populate (struct ssbox *ssb, const char *s; s = var_lookup_value_label (indep_var, &gp.v.g_value[0]); - val_lab[0] = s ? strdup (s) : NULL; + val_lab[0] = s ? xstrdup (s) : NULL; s = var_lookup_value_label (indep_var, &gp.v.g_value[1]); - val_lab[1] = s ? strdup (s) : NULL; + val_lab[1] = s ? xstrdup (s) : NULL; } else { - val_lab[0] = calloc (sizeof (char), MAX_SHORT_STRING + 1); - val_lab[1] = calloc (sizeof (char), MAX_SHORT_STRING + 1); + val_lab[0] = xcalloc (sizeof (char), MAX_SHORT_STRING + 1); + val_lab[1] = xcalloc (sizeof (char), MAX_SHORT_STRING + 1); memcpy (val_lab[0], gp.v.g_value[0].s, MAX_SHORT_STRING); memcpy (val_lab[1], gp.v.g_value[1].s, MAX_SHORT_STRING); } diff --git a/src/libpspp/hmap.c b/src/libpspp/hmap.c index 081d7cbb..4c97e235 100644 --- a/src/libpspp/hmap.c +++ b/src/libpspp/hmap.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2008 Free Software Foundation, Inc. + Copyright (C) 2008, 2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,6 +22,8 @@ #include #include +#include "xalloc.h" + static size_t capacity_to_mask (size_t capacity); /* Initializes MAP as a new hash map that is initially empty. */ @@ -75,7 +77,7 @@ hmap_rehash (struct hmap *map, size_t new_mask) assert ((new_mask & (new_mask + 1)) == 0); if (new_mask) - new_buckets = calloc (new_mask + 1, sizeof *new_buckets); + new_buckets = xcalloc (new_mask + 1, sizeof *new_buckets); else { new_buckets = &map->one; diff --git a/src/libpspp/i18n.c b/src/libpspp/i18n.c index c0459712..a4e9b63f 100644 --- a/src/libpspp/i18n.c +++ b/src/libpspp/i18n.c @@ -198,7 +198,7 @@ i18n_init (void) #endif /* ENABLE_NLS */ assert (default_encoding == NULL); - default_encoding = strdup (locale_charset ()); + default_encoding = xstrdup (locale_charset ()); hmapx_init (&map); } @@ -214,7 +214,7 @@ void set_default_encoding (const char *enc) { free (default_encoding); - default_encoding = strdup (enc); + default_encoding = xstrdup (enc); } @@ -228,13 +228,13 @@ set_encoding_from_locale (const char *loc) bool ok = true; char *c_encoding; char *loc_encoding; - char *tmp = strdup (setlocale (LC_CTYPE, NULL)); + char *tmp = xstrdup (setlocale (LC_CTYPE, NULL)); setlocale (LC_CTYPE, "C"); - c_encoding = strdup (locale_charset ()); + c_encoding = xstrdup (locale_charset ()); setlocale (LC_CTYPE, loc); - loc_encoding = strdup (locale_charset ()); + loc_encoding = xstrdup (locale_charset ()); if ( 0 == strcmp (loc_encoding, c_encoding)) @@ -297,7 +297,7 @@ get_system_decimal (void) { char radix_char; - char *ol = strdup (setlocale (LC_NUMERIC, NULL)); + char *ol = xstrdup (setlocale (LC_NUMERIC, NULL)); setlocale (LC_NUMERIC, ""); #if HAVE_NL_LANGINFO diff --git a/src/libpspp/message.c b/src/libpspp/message.c index 22710914..5faee1f1 100644 --- a/src/libpspp/message.c +++ b/src/libpspp/message.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -82,7 +82,7 @@ msg_dup(const struct msg *m) struct msg *new_msg = xmalloc (sizeof *m); *new_msg = *m; - new_msg->text = strdup(m->text); + new_msg->text = xstrdup(m->text); return new_msg; } diff --git a/src/ui/gui/helper.c b/src/ui/gui/helper.c index 6d90cfc4..c70f8f7c 100644 --- a/src/ui/gui/helper.c +++ b/src/ui/gui/helper.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2007 Free Software Foundation + Copyright (C) 2007, 2009 Free Software Foundation This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -170,7 +170,7 @@ convert_glib_filename_to_system_filename (const gchar *fname, GError **err) output_name = g_convert (fname, -1, target_encoding, "UTF-8", NULL, NULL, err); #else - output_name = strdup (fname); + output_name = xstrdup (fname); #endif return output_name; diff --git a/src/ui/gui/psppire-syntax-window.c b/src/ui/gui/psppire-syntax-window.c index e3aaa756..3879e5b4 100644 --- a/src/ui/gui/psppire-syntax-window.c +++ b/src/ui/gui/psppire-syntax-window.c @@ -34,6 +34,8 @@ #include "syntax-editor-source.h" #include +#include "xalloc.h" + #include #define _(msgid) gettext (msgid) #define N_(msgid) msgid @@ -224,7 +226,7 @@ append_suffix (const gchar *filename) return g_strdup_printf ("%s.sps", filename); } - return strdup (filename); + return xstrdup (filename); } /* diff --git a/src/ui/gui/psppire-window.c b/src/ui/gui/psppire-window.c index a02edb28..990e4ab5 100644 --- a/src/ui/gui/psppire-window.c +++ b/src/ui/gui/psppire-window.c @@ -137,7 +137,7 @@ psppire_window_set_property (GObject *object, name = g_value_get_string (&def); } - candidate_name = strdup (name); + candidate_name = xstrdup (name); while ( psppire_window_register_lookup (reg, candidate_name)) { @@ -411,7 +411,7 @@ psppire_window_init (PsppireWindow *window) { window->name = NULL; window->menu = NULL; - window->description = strdup (""); + window->description = xstrdup (""); window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal); diff --git a/src/ui/gui/psppire.c b/src/ui/gui/psppire.c index a1f48e7b..7a369a60 100644 --- a/src/ui/gui/psppire.c +++ b/src/ui/gui/psppire.c @@ -294,7 +294,7 @@ parse_non_options (int key, char *arg, struct argp_state *state) if ( local_is_utf8) { - utf8 = strdup (arg); + utf8 = xstrdup (arg); } else { @@ -324,7 +324,7 @@ parse_non_options (int key, char *arg, struct argp_state *state) g_free (utf8); if ( filename == NULL) - filename = strdup (arg); + filename = xstrdup (arg); psppire_window_load (PSPPIRE_WINDOW (the_data_window), filename); diff --git a/src/ui/gui/recode-dialog.c b/src/ui/gui/recode-dialog.c index e480f3c7..4bc12b50 100644 --- a/src/ui/gui/recode-dialog.c +++ b/src/ui/gui/recode-dialog.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2007 Free Software Foundation + Copyright (C) 2007, 2009 Free Software Foundation This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -71,7 +71,7 @@ new_value_copy (struct new_value *nv) struct new_value *copy = g_memdup (nv, sizeof (*copy)); if ( nv->type == NV_STRING ) - copy->v.s = strdup (nv->v.s); + copy->v.s = xstrdup (nv->v.s); return copy; } diff --git a/src/ui/gui/select-cases-dialog.c b/src/ui/gui/select-cases-dialog.c index 3c36f4ed..6492af28 100644 --- a/src/ui/gui/select-cases-dialog.c +++ b/src/ui/gui/select-cases-dialog.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2007, 2008 Free Software Foundation, Inc. + Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -388,7 +388,7 @@ generate_syntax (const struct select_cases_dialog *scd) (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml, "radiobutton-all")))) { - return strdup ("\n"); + return xstrdup ("\n"); } string = g_string_new (""); diff --git a/src/ui/gui/t-test-independent-samples-dialog.c b/src/ui/gui/t-test-independent-samples-dialog.c index e5a04a07..c24e3251 100644 --- a/src/ui/gui/t-test-independent-samples-dialog.c +++ b/src/ui/gui/t-test-independent-samples-dialog.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2007 Free Software Foundation + Copyright (C) 2007, 2009 Free Software Foundation This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -117,8 +117,8 @@ tt_groups_dialog_create (GtkBuilder *xml, GtkWindow *parent) gtk_window_set_transient_for (GTK_WINDOW (grps->dialog), parent); - grps->val[0] = strdup (""); - grps->val[1] = strdup (""); + grps->val[0] = xstrdup (""); + grps->val[1] = xstrdup (""); return grps; } @@ -340,10 +340,10 @@ run_define_groups (struct tt_indep_samples_dialog *ttd) grps->group_defn = GROUPS_VALUES; grps->val[0] = - strdup (gtk_entry_get_text (GTK_ENTRY (grps->grp_entry[0]))); + xstrdup (gtk_entry_get_text (GTK_ENTRY (grps->grp_entry[0]))); grps->val[1] = - strdup (gtk_entry_get_text (GTK_ENTRY (grps->grp_entry[1]))); + xstrdup (gtk_entry_get_text (GTK_ENTRY (grps->grp_entry[1]))); } else { @@ -352,7 +352,7 @@ run_define_groups (struct tt_indep_samples_dialog *ttd) grps->val[1] = NULL; grps->val[0] = - strdup (gtk_entry_get_text (GTK_ENTRY (grps->cut_point_entry))); + xstrdup (gtk_entry_get_text (GTK_ENTRY (grps->cut_point_entry))); } psppire_dialog_notify_change (PSPPIRE_DIALOG (ttd->dialog)); diff --git a/src/ui/gui/widget-io.c b/src/ui/gui/widget-io.c index e6471990..3771cad6 100644 --- a/src/ui/gui/widget-io.c +++ b/src/ui/gui/widget-io.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2007 Free Software Foundation + Copyright (C) 2007, 2009 Free Software Foundation This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -26,6 +26,7 @@ #include +#include "xalloc.h" /* Create a GtkLabel and pack it into BOX. The label is created using part of the string at S, and the directives @@ -90,7 +91,7 @@ widget_printf (const gchar *fmt, ...) if ( 0 != printf_parse (fmt, &d, &a) ) return NULL; - widgets = calloc (sizeof (*widgets), d.count); + widgets = xcalloc (sizeof (*widgets), d.count); va_start (ap, fmt); for (i = 0 ; i < d.count ; ++i ) { diff --git a/tests/dissect-sysfile.c b/tests/dissect-sysfile.c index 1c81c72c..62161f9c 100644 --- a/tests/dissect-sysfile.c +++ b/tests/dissect-sysfile.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2007, 2008 Free Software Foundation, Inc. + Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -723,7 +723,7 @@ static void read_character_encoding (struct sfm_reader *r, size_t size, size_t count) { const unsigned long int posn = ftell (r->file); - char *encoding = calloc (size, count + 1); + char *encoding = xcalloc (size, count + 1); read_string (r, encoding, count + 1); printf ("%08lx: Character Encoding: %s\n", posn, encoding); -- 2.30.2