mirror of
https://github.com/MidnightCommander/mc
synced 2025-03-26 01:32:54 +03:00
The check result of g_new(), g_new0(), g_malloc(), g_malloc0() and g_realloc() is useless.
g_new(), g_new0(), g_malloc(), g_malloc0() and g_realloc() functions never return NULL and call abort() on fail. So check result of these functions is useless. g_try_new(), g_try_new0(), g_try_malloc(), g_try_malloc0() and g_try_realloc() functions are used instead. Some minor optimization and code formatting are also performed. Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
parent
1aee8632ef
commit
dc604d811a
@ -1090,11 +1090,10 @@ edit_read_syntax_file (WEdit * edit, char ***pnames, const char *syntax_file,
|
||||
/* 1: just collecting a list of names of rule sets */
|
||||
/* Reallocate the list if required */
|
||||
if (count % NENTRIES == 0) {
|
||||
if ((tmpnames = (char**) g_realloc (*pnames, (count + NENTRIES
|
||||
+ 1) * sizeof (char*))) != NULL)
|
||||
*pnames = tmpnames;
|
||||
else
|
||||
abort ();
|
||||
tmpnames = (char**) g_try_realloc (*pnames, (count + NENTRIES + 1) * sizeof (char*));
|
||||
if (tmpnames == NULL)
|
||||
break;
|
||||
*pnames = tmpnames;
|
||||
}
|
||||
(*pnames)[count++] = g_strdup (args[2]);
|
||||
(*pnames)[count] = NULL;
|
||||
|
@ -141,13 +141,13 @@ get_paragraph (WEdit *edit, long p, long q, int indent, int *size)
|
||||
{
|
||||
unsigned char *s, *t;
|
||||
#if 0
|
||||
t = g_malloc ((q - p) + 2 * (q - p) / option_word_wrap_line_length +
|
||||
t = g_try_malloc ((q - p) + 2 * (q - p) / option_word_wrap_line_length +
|
||||
10);
|
||||
#else
|
||||
t = g_malloc (2 * (q - p) + 100);
|
||||
t = g_try_malloc (2 * (q - p) + 100);
|
||||
#endif
|
||||
if (!t)
|
||||
return 0;
|
||||
if (t == NULL)
|
||||
return NULL;
|
||||
for (s = t; p < q; p++, s++) {
|
||||
if (indent)
|
||||
if (edit_get_byte (edit, p - 1) == '\n')
|
||||
|
@ -1064,7 +1064,7 @@ vfs_smb_get_authinfo (const char *host, const char *share, const char *domain,
|
||||
return_value = 0;
|
||||
break;
|
||||
default:
|
||||
return_value = g_new (struct smb_authinfo, 1);
|
||||
return_value = g_try_new (struct smb_authinfo, 1);
|
||||
if (return_value) {
|
||||
return_value->host = g_strdup (host);
|
||||
return_value->share = g_strdup (share);
|
||||
|
@ -866,16 +866,17 @@ static WInput *input;
|
||||
static int min_end;
|
||||
static int start, end;
|
||||
|
||||
static int insert_text (WInput *in, char *text, ssize_t size)
|
||||
static int
|
||||
insert_text (WInput *in, char *text, ssize_t size)
|
||||
{
|
||||
int buff_len = str_length (in->buffer);
|
||||
|
||||
|
||||
size = min (size, (ssize_t) strlen (text)) + start - end;
|
||||
if (strlen (in->buffer) + size >= (size_t) in->current_max_size){
|
||||
/* Expand the buffer */
|
||||
char *narea = g_realloc (in->buffer, in->current_max_size
|
||||
+ size + in->field_width);
|
||||
if (narea){
|
||||
char *narea = g_try_realloc (in->buffer, in->current_max_size
|
||||
+ size + in->field_width);
|
||||
if (narea != NULL) {
|
||||
in->buffer = narea;
|
||||
in->current_max_size += size + in->field_width;
|
||||
}
|
||||
|
@ -217,11 +217,9 @@ console_init (void)
|
||||
memset (&screen_shot, 0, sizeof (screen_shot));
|
||||
screen_shot.xsize = screen_info.mv_csz;
|
||||
screen_shot.ysize = screen_info.mv_rsz;
|
||||
if ((screen_shot.buf =
|
||||
g_malloc (screen_info.mv_csz * screen_info.mv_rsz * 2)) == NULL)
|
||||
return;
|
||||
|
||||
console_flag = 1;
|
||||
screen_shot.buf = g_try_malloc (screen_info.mv_csz * screen_info.mv_rsz * 2);
|
||||
if (screen_shot.buf != NULL)
|
||||
console_flag = 1;
|
||||
}
|
||||
|
||||
static void
|
||||
|
37
src/dir.c
37
src/dir.c
@ -265,8 +265,8 @@ set_zero_dir (dir_list *list)
|
||||
{
|
||||
/* Need to grow the *list? */
|
||||
if (list->size == 0) {
|
||||
list->list = g_realloc (list->list, sizeof (file_entry) *
|
||||
(list->size + RESIZE_STEPS));
|
||||
list->list = g_try_realloc (list->list, sizeof (file_entry) *
|
||||
(list->size + RESIZE_STEPS));
|
||||
if (list->list == NULL)
|
||||
return FALSE;
|
||||
|
||||
@ -327,10 +327,9 @@ handle_dirent (dir_list *list, const char *filter, struct dirent *dp,
|
||||
|
||||
/* Need to grow the *list? */
|
||||
if (next_free == list->size) {
|
||||
list->list =
|
||||
g_realloc (list->list,
|
||||
sizeof (file_entry) * (list->size + RESIZE_STEPS));
|
||||
if (!list->list)
|
||||
list->list = g_try_realloc (list->list, sizeof (file_entry) *
|
||||
(list->size + RESIZE_STEPS));
|
||||
if (list->list == NULL)
|
||||
return -1;
|
||||
list->size += RESIZE_STEPS;
|
||||
}
|
||||
@ -390,9 +389,9 @@ handle_path (dir_list *list, const char *path,
|
||||
|
||||
/* Need to grow the *list? */
|
||||
if (next_free == list->size){
|
||||
list->list = g_realloc (list->list, sizeof (file_entry) *
|
||||
(list->size + RESIZE_STEPS));
|
||||
if (!list->list)
|
||||
list->list = g_try_realloc (list->list, sizeof (file_entry) *
|
||||
(list->size + RESIZE_STEPS));
|
||||
if (list->list == NULL)
|
||||
return -1;
|
||||
list->size += RESIZE_STEPS;
|
||||
}
|
||||
@ -488,25 +487,15 @@ static dir_list dir_copy = { 0, 0 };
|
||||
static void
|
||||
alloc_dir_copy (int size)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (dir_copy.size < size){
|
||||
if (dir_copy.list){
|
||||
|
||||
for (i = 0; i < dir_copy.size; i++) {
|
||||
if (dir_copy.size < size) {
|
||||
if (dir_copy.list) {
|
||||
int i;
|
||||
for (i = 0; i < dir_copy.size; i++)
|
||||
g_free (dir_copy.list [i].fname);
|
||||
}
|
||||
g_free (dir_copy.list);
|
||||
dir_copy.list = 0;
|
||||
}
|
||||
|
||||
dir_copy.list = g_new (file_entry, size);
|
||||
for (i = 0; i < size; i++) {
|
||||
dir_copy.list [i].fname = NULL;
|
||||
dir_copy.list [i].sort_key = NULL;
|
||||
dir_copy.list [i].second_sort_key = NULL;
|
||||
}
|
||||
|
||||
dir_copy.list = g_new0 (file_entry, size);
|
||||
dir_copy.size = size;
|
||||
}
|
||||
}
|
||||
|
@ -240,10 +240,10 @@ check_hardlinks (const char *src_name, const char *dst_name, struct stat *pstat)
|
||||
message (D_ERROR, MSG_ERROR, _(" Cannot make the hardlink "));
|
||||
return 0;
|
||||
}
|
||||
lp = (struct link *) g_malloc (sizeof (struct link) + strlen (src_name)
|
||||
+ strlen (dst_name) + 1);
|
||||
lp = (struct link *) g_try_malloc (sizeof (struct link) + strlen (src_name)
|
||||
+ strlen (dst_name) + 1);
|
||||
if (lp) {
|
||||
char *lpdstname;
|
||||
char *lpdstname;
|
||||
lp->vfs = my_vfs;
|
||||
lp->ino = ino;
|
||||
lp->dev = dev;
|
||||
|
@ -77,7 +77,7 @@ mc_fhl_new (gboolean need_auto_fill)
|
||||
|
||||
mc_fhl_t *fhl;
|
||||
|
||||
fhl = g_new0 (mc_fhl_t, 1);
|
||||
fhl = g_try_new0 (mc_fhl_t, 1);
|
||||
if (fhl == NULL)
|
||||
return NULL;
|
||||
|
||||
|
@ -3311,7 +3311,7 @@ panel_get_sortable_fields(gsize *array_size)
|
||||
|
||||
lc_index = panel_get_num_of_sortable_fields();
|
||||
|
||||
ret = g_new0 (char *, lc_index + 1);
|
||||
ret = g_try_new0 (char *, lc_index + 1);
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
|
||||
@ -3391,7 +3391,7 @@ panel_get_user_possible_fields(gsize *array_size)
|
||||
|
||||
lc_index = panel_get_num_of_user_possible_fields();
|
||||
|
||||
ret = g_new0 (char *, lc_index + 1);
|
||||
ret = g_try_new0 (char *, lc_index + 1);
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
|
||||
|
@ -258,7 +258,7 @@ mc_search_get_types_strings_array (size_t *num)
|
||||
const mc_search_type_str_t *type_str;
|
||||
const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
|
||||
|
||||
ret = g_new0 (char *, n + 1);
|
||||
ret = g_try_new0 (char *, n + 1);
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
|
||||
|
@ -134,7 +134,7 @@ mc_skin_color_get_from_ini_file (mc_skin_t * mc_skin, const gchar * group, const
|
||||
g_strfreev (values);
|
||||
return NULL;
|
||||
}
|
||||
mc_skin_color = g_new0 (mc_skin_color_t, 1);
|
||||
mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
|
||||
if (mc_skin_color == NULL) {
|
||||
g_strfreev (values);
|
||||
return NULL;
|
||||
@ -176,15 +176,14 @@ static void
|
||||
mc_skin_color_set_default_for_terminal (mc_skin_t * mc_skin)
|
||||
{
|
||||
mc_skin_color_t *mc_skin_color;
|
||||
mc_skin_color = g_new0 (mc_skin_color_t, 1);
|
||||
if (mc_skin_color == NULL)
|
||||
return;
|
||||
|
||||
mc_skin_color->fgcolor = g_strdup ("default");
|
||||
mc_skin_color->bgcolor = g_strdup ("default");
|
||||
mc_skin_color->pair_index =
|
||||
tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
|
||||
mc_skin_color_add_to_hash (mc_skin, "skin", "terminal_default_color", mc_skin_color);
|
||||
mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
|
||||
if (mc_skin_color != NULL) {
|
||||
mc_skin_color->fgcolor = g_strdup ("default");
|
||||
mc_skin_color->bgcolor = g_strdup ("default");
|
||||
mc_skin_color->pair_index =
|
||||
tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
|
||||
mc_skin_color_add_to_hash (mc_skin, "skin", "terminal_default_color", mc_skin_color);
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
@ -738,9 +738,9 @@ subshell_name_quote (const char *s)
|
||||
}
|
||||
|
||||
/* Factor 5 because we need \, 0 and 3 other digits per character. */
|
||||
d = ret = g_malloc (1 + (5 * strlen (s)) + (strlen(quote_cmd_start))
|
||||
d = ret = g_try_malloc (1 + (5 * strlen (s)) + (strlen(quote_cmd_start))
|
||||
+ (strlen(quote_cmd_end)));
|
||||
if (!d)
|
||||
if (d == NULL)
|
||||
return NULL;
|
||||
|
||||
/* Prevent interpreting leading `-' as a switch for `cd' */
|
||||
|
@ -235,10 +235,8 @@ show_tree (WTree *tree)
|
||||
}
|
||||
|
||||
g_free (tree->tree_shown);
|
||||
tree->tree_shown = g_new (tree_entry*, tree_lines);
|
||||
tree->tree_shown = g_new0 (tree_entry *, tree_lines);
|
||||
|
||||
for (i = 0; i < tree_lines; i++)
|
||||
tree->tree_shown [i] = NULL;
|
||||
if (tree->store->tree_first)
|
||||
topsublevel = tree->store->tree_first->sublevel;
|
||||
else
|
||||
|
@ -61,16 +61,17 @@ static int
|
||||
mc_tty_color_save_attr_lib (int color_pair, int color_attr)
|
||||
{
|
||||
int *attr, *key;
|
||||
attr = g_new0 (int, 1);
|
||||
attr = g_try_new0 (int, 1);
|
||||
if (attr == NULL)
|
||||
return color_attr;
|
||||
|
||||
key = g_new0 (int, 1);
|
||||
key = g_try_new (int, 1);
|
||||
if (key == NULL) {
|
||||
g_free (attr);
|
||||
return color_attr;
|
||||
}
|
||||
memcpy (key, &color_pair, sizeof (int));
|
||||
|
||||
*key = color_pair;
|
||||
|
||||
if (color_attr != -1)
|
||||
*attr = color_attr & (A_BOLD | A_REVERSE | A_UNDERLINE);
|
||||
@ -83,12 +84,10 @@ mc_tty_color_save_attr_lib (int color_pair, int color_attr)
|
||||
static int
|
||||
color_get_attr (int color_pair)
|
||||
{
|
||||
int *fnd;
|
||||
int *fnd = NULL;
|
||||
|
||||
if (mc_tty_color_color_pair_attrs == NULL)
|
||||
return 0;
|
||||
|
||||
fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair);
|
||||
if (mc_tty_color_color_pair_attrs != NULL)
|
||||
fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair);
|
||||
return (fnd != NULL) ? *fnd : 0;
|
||||
}
|
||||
|
||||
|
@ -196,7 +196,7 @@ tty_try_alloc_color_pair2 (const char *fg, const char *bg, gboolean is_temp_colo
|
||||
return mc_color_pair->pair_index;
|
||||
}
|
||||
|
||||
mc_color_pair = g_new0 (tty_color_pair_t, 1);
|
||||
mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
|
||||
if (mc_color_pair == NULL) {
|
||||
g_free (color_pair);
|
||||
return 0;
|
||||
|
@ -801,15 +801,15 @@ user_menu_cmd (WEdit *edit_widget)
|
||||
char ** new_entries;
|
||||
|
||||
menu_limit += MAX_ENTRIES;
|
||||
new_entries = g_realloc (entries, sizeof (new_entries[0]) * menu_limit);
|
||||
new_entries = g_try_realloc (entries, sizeof (new_entries[0]) * menu_limit);
|
||||
|
||||
if (new_entries == 0)
|
||||
if (new_entries == NULL)
|
||||
break;
|
||||
|
||||
entries = new_entries;
|
||||
new_entries += menu_limit;
|
||||
while (--new_entries >= &entries[menu_lines])
|
||||
*new_entries = 0;
|
||||
*new_entries = NULL;
|
||||
}
|
||||
if (col == 0 && !entries [menu_lines]){
|
||||
if (*p == '#'){
|
||||
|
@ -634,7 +634,7 @@ putenv (char *string)
|
||||
|
||||
if (*ep == NULL){
|
||||
static char **last_environ = NULL;
|
||||
char **new_environ = g_new (char *, size + 2);
|
||||
char **new_environ = g_try_new (char *, size + 2);
|
||||
if (new_environ == NULL)
|
||||
return -1;
|
||||
(void) memcpy ((void *) new_environ, (void *) __environ,
|
||||
|
@ -226,12 +226,12 @@ mcview_nroff_seq_new_num (mcview_t * view, off_t lc_index)
|
||||
{
|
||||
mcview_nroff_t *nroff;
|
||||
|
||||
nroff = g_malloc0 (sizeof (mcview_nroff_t));
|
||||
if (nroff == NULL)
|
||||
return NULL;
|
||||
nroff->index = lc_index;
|
||||
nroff->view = view;
|
||||
mcview_nroff_seq_info (nroff);
|
||||
nroff = g_try_malloc0 (sizeof (mcview_nroff_t));
|
||||
if (nroff != NULL) {
|
||||
nroff->index = lc_index;
|
||||
nroff->view = view;
|
||||
mcview_nroff_seq_info (nroff);
|
||||
}
|
||||
return nroff;
|
||||
}
|
||||
|
||||
|
@ -57,8 +57,8 @@ vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *i
|
||||
{
|
||||
struct vfs_s_inode *ino;
|
||||
|
||||
ino = g_new0 (struct vfs_s_inode, 1);
|
||||
if (!ino)
|
||||
ino = g_try_new0 (struct vfs_s_inode, 1);
|
||||
if (ino == NULL)
|
||||
return NULL;
|
||||
|
||||
if (initstat)
|
||||
@ -70,7 +70,7 @@ vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *i
|
||||
|
||||
super->ino_usage++;
|
||||
total_inodes++;
|
||||
|
||||
|
||||
CALL (init_inode) (me, ino);
|
||||
|
||||
return ino;
|
||||
|
@ -1053,7 +1053,7 @@ again:
|
||||
|
||||
port = ntohs (port);
|
||||
|
||||
addr = g_malloc (NI_MAXHOST);
|
||||
addr = g_try_malloc (NI_MAXHOST);
|
||||
if (addr == NULL)
|
||||
ERRNOR (ENOMEM, -1);
|
||||
|
||||
|
@ -184,9 +184,9 @@ static void
|
||||
smbfs_auth_add (const char *host, const char *share, const char *domain,
|
||||
const char *user, const char *param_password)
|
||||
{
|
||||
struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
|
||||
|
||||
if (!auth)
|
||||
struct smb_authinfo *auth = g_try_new (struct smb_authinfo, 1);
|
||||
|
||||
if (auth == NULL)
|
||||
return;
|
||||
|
||||
/* Don't check for NULL, g_strdup already does. */
|
||||
|
28
vfs/vfs.c
28
vfs/vfs.c
@ -749,29 +749,29 @@ mc_opendir (const char *dirname)
|
||||
|
||||
if (dname != NULL) {
|
||||
vfs = vfs_get_class (dname);
|
||||
info = vfs->opendir ? (*vfs->opendir)(vfs, dname) : NULL;
|
||||
g_free (dname);
|
||||
|
||||
if (!info){
|
||||
errno = vfs->opendir ? ferrno (vfs) : E_NOTSUPP;
|
||||
info = vfs->opendir ? (*vfs->opendir)(vfs, dname) : NULL;
|
||||
g_free (dname);
|
||||
|
||||
if (info == NULL) {
|
||||
errno = vfs->opendir ? ferrno (vfs) : E_NOTSUPP;
|
||||
g_free (canon);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dirinfo = g_new (struct vfs_dirinfo, 1);
|
||||
dirinfo->info = info;
|
||||
|
||||
|
||||
encoding = vfs_get_encoding (canon);
|
||||
g_free (canon);
|
||||
dirinfo->converter = (encoding != NULL) ? str_crt_conv_from (encoding) :
|
||||
str_cnv_from_term;
|
||||
if (dirinfo->converter == INVALID_CONV) dirinfo->converter =str_cnv_from_term;
|
||||
|
||||
|
||||
handle = vfs_new_handle (vfs, dirinfo);
|
||||
|
||||
handlep = g_new (int, 1);
|
||||
*handlep = handle;
|
||||
return (DIR *) handlep;
|
||||
handlep = g_new (int, 1);
|
||||
*handlep = handle;
|
||||
return (DIR *) handlep;
|
||||
} else {
|
||||
g_free (canon);
|
||||
return NULL;
|
||||
@ -801,7 +801,7 @@ mc_readdir (DIR *dirp)
|
||||
* structures, holding dirent size. But we don't use it in libc infrastructure.
|
||||
* TODO: to make simpler homemade dirent-alike structure.
|
||||
*/
|
||||
mc_readdir_result = (struct dirent *)malloc(sizeof(struct dirent) + NAME_MAX + 1);
|
||||
mc_readdir_result = (struct dirent *) g_malloc (sizeof(struct dirent) + NAME_MAX + 1);
|
||||
}
|
||||
|
||||
if (!dirp) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user