Ticket #1863: 1863-fixing-some-memory-alloc-calls.patch

File 1863-fixing-some-memory-alloc-calls.patch, 17.8 KB (added by vit_r, 9 years ago)

fixing in seventeen files some memory alloc-calls

  • edit/syntax.c

    From bb8fc03985d0ea59520da968d4b2cbbb923d741e Mon Sep 17 00:00:00 2001
    From: Vit Rosin <vit_r@list.ru>
    Date: Sat, 5 Dec 2009 23:36:18 +0000
    Subject: [PATCH]  fixing some memory alloc-calls
    
    ---
     edit/syntax.c                |    2 +-
     src/complete.c               |    2 +-
     src/dir.c                    |    6 +++---
     src/man2hlp.c                |    6 +++---
     src/mountlist.c              |   30 +++++++++++++++---------------
     src/user.c                   |    4 ++--
     src/viewer/nroff.c           |    2 +-
     vfs/cpio.c                   |    6 +++---
     vfs/mcfsutil.c               |    3 +++
     vfs/mcserv.c                 |    2 +-
     vfs/samba/lib/charset.c      |    2 +-
     vfs/samba/lib/interface.c    |   10 ++++++----
     vfs/samba/lib/util.c         |    6 +++---
     vfs/samba/lib/util_str.c     |    7 ++++---
     vfs/samba/libsmb/clientgen.c |    6 +++---
     vfs/samba/param/params.c     |    2 +-
     vfs/vfs.c                    |    2 +-
     17 files changed, 52 insertions(+), 46 deletions(-)
    
    diff --git a/edit/syntax.c b/edit/syntax.c
    index 9055d6a..f28eef9 100644
    a b edit_read_syntax_file (WEdit * edit, char ***pnames, const char *syntax_file, 
    10891089/* 1: just collecting a list of names of rule sets */ 
    10901090/* Reallocate the list if required */ 
    10911091            if (count % NENTRIES == 0) { 
    1092                 if ((tmpnames = (char**) g_realloc (*pnames, (count + NENTRIES 
     1092                if ((tmpnames = (char**) g_try_realloc (*pnames, (count + NENTRIES 
    10931093                    + 1) * sizeof (char*))) != NULL) 
    10941094                    *pnames = tmpnames; 
    10951095                else 
  • src/complete.c

    diff --git a/src/complete.c b/src/complete.c
    index a719b98..3f741b1 100644
    a b static int insert_text (WInput *in, char *text, ssize_t size) 
    873873    size = min (size, (ssize_t) strlen (text)) + start - end; 
    874874    if (strlen (in->buffer) + size >= (size_t) in->current_max_size){ 
    875875    /* Expand the buffer */ 
    876         char *narea = g_realloc (in->buffer, in->current_max_size  
     876        char *narea = g_try_realloc (in->buffer, in->current_max_size 
    877877                + size + in->field_width); 
    878878        if (narea){ 
    879879            in->buffer = narea; 
  • src/dir.c

    diff --git a/src/dir.c b/src/dir.c
    index 74af764..c32db60 100644
    a b set_zero_dir (dir_list *list) 
    265265{ 
    266266    /* Need to grow the *list? */ 
    267267    if (list->size == 0) { 
    268         list->list = g_realloc (list->list, sizeof (file_entry) * 
     268        list->list = g_try_realloc (list->list, sizeof (file_entry) * 
    269269                              (list->size + RESIZE_STEPS)); 
    270270        if (list->list == NULL) 
    271271            return FALSE; 
    handle_dirent (dir_list *list, const char *filter, struct dirent *dp, 
    328328    /* Need to grow the *list? */ 
    329329    if (next_free == list->size) { 
    330330        list->list = 
    331             g_realloc (list->list, 
     331            g_try_realloc (list->list, 
    332332                       sizeof (file_entry) * (list->size + RESIZE_STEPS)); 
    333333        if (!list->list) 
    334334            return -1; 
    handle_path (dir_list *list, const char *path, 
    390390 
    391391    /* Need to grow the *list? */ 
    392392    if (next_free == list->size){ 
    393         list->list = g_realloc (list->list, sizeof (file_entry) * 
     393        list->list = g_try_realloc (list->list, sizeof (file_entry) * 
    394394                              (list->size + RESIZE_STEPS)); 
    395395        if (!list->list) 
    396396            return -1; 
  • src/man2hlp.c

    diff --git a/src/man2hlp.c b/src/man2hlp.c
    index aa95df6..6018f11 100644
    a b handle_node (char *buffer, int is_sh) 
    354354                if (!cnode) { 
    355355                    cnode = &nodes; 
    356356                } else { 
    357                     cnode->next = malloc (sizeof (nodes)); 
     357                    cnode->next = g_malloc (sizeof (nodes)); 
    358358                    cnode = cnode->next; 
    359359                } 
    360360                cnode->node = strdup (buffer); 
    handle_link (char *buffer) 
    649649        link_flag = 0; 
    650650        /* Add to the linked list */ 
    651651        if (current_link) { 
    652             current_link->next = malloc (sizeof (links)); 
     652            current_link->next = g_malloc (sizeof (links)); 
    653653            current_link = current_link->next; 
    654654            current_link->next = NULL; 
    655655        } else { 
    main (int argc, char **argv) 
    773773                        if (!cnode) { 
    774774                            cnode = &nodes; 
    775775                        } else { 
    776                             cnode->next = malloc (sizeof (nodes)); 
     776                            cnode->next = g_malloc (sizeof (nodes)); 
    777777                            cnode = cnode->next; 
    778778                        } 
    779779                        cnode->node = strdup (lc_node + 2); 
  • src/mountlist.c

    diff --git a/src/mountlist.c b/src/mountlist.c
    index 847bf39..bf97da5 100644
    a b read_filesystem_list (int need_fs_type, int all_fs) 
    320320    (void) all_fs; 
    321321 
    322322    /* Start the list off with a dummy entry. */ 
    323     me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); 
     323    me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); 
    324324    me->me_next = NULL; 
    325325    mlist = mtail = me; 
    326326 
    read_filesystem_list (int need_fs_type, int all_fs) 
    340340                            || !strcmp (mnt->mnt_type, "auto"))) 
    341341                continue; 
    342342 
    343             me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); 
     343            me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); 
    344344            me->me_devname = strdup (mnt->mnt_fsname); 
    345345            me->me_mountdir = strdup (mnt->mnt_dir); 
    346346            me->me_type = strdup (mnt->mnt_type); 
    read_filesystem_list (int need_fs_type, int all_fs) 
    374374        if (entries < 0) 
    375375            return NULL; 
    376376        while (entries-- > 0) { 
    377             me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); 
     377            me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); 
    378378            me->me_devname = strdup (fsp->f_mntfromname); 
    379379            me->me_mountdir = strdup (fsp->f_mntonname); 
    380380#ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME 
    read_filesystem_list (int need_fs_type, int all_fs) 
    402402        if (entries < 0) 
    403403            return NULL; 
    404404        for (; entries-- > 0; fsp++) { 
    405             me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); 
     405            me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); 
    406406            me->me_devname = strdup (fsp->f_mntfromname); 
    407407            me->me_mountdir = strdup (fsp->f_mntonname); 
    408408            me->me_type = strdup (fsp->f_fstypename); 
    read_filesystem_list (int need_fs_type, int all_fs) 
    423423 
    424424        while ((val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY, 
    425425                              NULL)) > 0) { 
    426             me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); 
     426            me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); 
    427427            me->me_devname = strdup (fsd.fd_req.devname); 
    428428            me->me_mountdir = strdup (fsd.fd_req.path); 
    429429            me->me_type = gt_names[fsd.fd_req.fstype]; 
    read_filesystem_list (int need_fs_type, int all_fs) 
    449449            return (NULL); 
    450450 
    451451        bufsize = (1 + numsys) * sizeof (struct statfs); 
    452         stats = (struct statfs *) malloc (bufsize); 
     452        stats = (struct statfs *) g_malloc (bufsize); 
    453453        numsys = getfsstat (stats, bufsize, MNT_WAIT); 
    454454 
    455455        if (numsys < 0) { 
    read_filesystem_list (int need_fs_type, int all_fs) 
    457457            return (NULL); 
    458458        } 
    459459        for (counter = 0; counter < numsys; counter++) { 
    460             me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); 
     460            me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); 
    461461            me->me_devname = strdup (stats[counter].f_mntfromname); 
    462462            me->me_mountdir = strdup (stats[counter].f_mntonname); 
    463463            me->me_type = mnt_names[stats[counter].f_type]; 
    read_filesystem_list (int need_fs_type, int all_fs) 
    484484            return NULL; 
    485485 
    486486        while (fread (&mnt, sizeof mnt, 1, fp) > 0) { 
    487             me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); 
     487            me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); 
    488488#ifdef GETFSTYP                 /* SVR3.  */ 
    489489            me->me_devname = strdup (mnt.mt_dev); 
    490490#else 
    491             me->me_devname = malloc (strlen (mnt.mt_dev) + 6); 
     491            me->me_devname = g_malloc (strlen (mnt.mt_dev) + 6); 
    492492            strcpy (me->me_devname, "/dev/"); 
    493493            strcpy (me->me_devname + 5, mnt.mt_dev); 
    494494#endif 
    read_filesystem_list (int need_fs_type, int all_fs) 
    521521    { 
    522522        struct mntent **mnttbl = getmnttbl (), **ent; 
    523523        for (ent = mnttbl; *ent; ent++) { 
    524             me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); 
     524            me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); 
    525525            me->me_devname = strdup ((*ent)->mt_resource); 
    526526            me->me_mountdir = strdup ((*ent)->mt_directory); 
    527527            me->me_type = strdup ((*ent)->mt_fstype); 
    read_filesystem_list (int need_fs_type, int all_fs) 
    548548            return NULL; 
    549549 
    550550        while ((ret = getmntent (fp, &mnt)) == 0) { 
    551             me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); 
     551            me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); 
    552552            me->me_devname = strdup (mnt.mnt_special); 
    553553            me->me_mountdir = strdup (mnt.mnt_mountp); 
    554554            me->me_type = strdup (mnt.mnt_fstype); 
    read_filesystem_list (int need_fs_type, int all_fs) 
    574574 
    575575        /* Ask how many bytes to allocate for the mounted filesystem info.  */ 
    576576        mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize); 
    577         entries = malloc (bufsize); 
     577        entries = g_malloc (bufsize); 
    578578 
    579579        /* Get the list of mounted filesystems.  */ 
    580580        mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries); 
    read_filesystem_list (int need_fs_type, int all_fs) 
    582582        for (thisent = entries; thisent < entries + bufsize; 
    583583             thisent += vmp->vmt_length) { 
    584584            vmp = (struct vmount *) thisent; 
    585             me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); 
     585            me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); 
    586586            if (vmp->vmt_flags & MNT_REMOTE) { 
    587587                char *host, *path; 
    588588 
    589589                /* Prepend the remote pathname.  */ 
    590590                host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off; 
    591591                path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off; 
    592                 me->me_devname = malloc (strlen (host) + strlen (path) + 2); 
     592                me->me_devname = g_malloc (strlen (host) + strlen (path) + 2); 
    593593                strcpy (me->me_devname, host); 
    594594                strcat (me->me_devname, ":"); 
    595595                strcat (me->me_devname, path); 
    read_filesystem_list(int need_fs_type, int all_fs) 
    644644                if (me->me_type) free(me->me_type); 
    645645        } 
    646646        else 
    647                 me = (struct mount_entry *)malloc(sizeof(struct mount_entry)); 
     647                me = (struct mount_entry *) g_malloc (sizeof(struct mount_entry)); 
    648648 
    649649        if (!getcwd(dir, _POSIX_PATH_MAX)) return (NULL); 
    650650 
  • src/user.c

    diff --git a/src/user.c b/src/user.c
    index 10e1dbc..d04c1bd 100644
    a b user_menu_cmd (WEdit *edit_widget) 
    801801            char ** new_entries; 
    802802 
    803803            menu_limit += MAX_ENTRIES; 
    804             new_entries = g_realloc (entries, sizeof (new_entries[0]) * menu_limit); 
     804            new_entries = g_try_realloc (entries, sizeof (new_entries[0]) * menu_limit); 
    805805 
    806             if (new_entries == 0) 
     806            if (new_entries == NULL) 
    807807                break; 
    808808 
    809809            entries = new_entries; 
  • src/viewer/nroff.c

    diff --git a/src/viewer/nroff.c b/src/viewer/nroff.c
    index 73d4668..56fdc03 100644
    a b mcview_nroff_seq_new_num (mcview_t * view, off_t lc_index) 
    226226{ 
    227227    mcview_nroff_t *nroff; 
    228228 
    229     nroff = g_malloc0 (sizeof (mcview_nroff_t)); 
     229    nroff = g_try_malloc0 (sizeof (mcview_nroff_t)); 
    230230    if (nroff == NULL) 
    231231        return NULL; 
    232232    nroff->index = lc_index; 
  • vfs/cpio.c

    diff --git a/vfs/cpio.c b/vfs/cpio.c
    index 0af9cda..2bc3600 100644
    a b static ssize_t cpio_read_bin_head(struct vfs_class *me, struct vfs_s_super *supe 
    433433                super->name); 
    434434        return STATUS_FAIL; 
    435435    } 
    436     name = g_malloc(u.buf.c_namesize); 
     436    name = g_malloc (u.buf.c_namesize); 
    437437    if((len = mc_read(super->u.arch.fd, name, u.buf.c_namesize)) < u.buf.c_namesize) { 
    438438        g_free(name); 
    439439        return STATUS_EOF; 
    static ssize_t cpio_read_oldc_head(struct vfs_class *me, struct vfs_s_super *sup 
    491491                    super->name); 
    492492        return STATUS_FAIL; 
    493493    } 
    494     name = g_malloc(hd.c_namesize); 
     494    name = g_malloc (hd.c_namesize); 
    495495    if((len = mc_read(super->u.arch.fd, name, hd.c_namesize)) == -1 || 
    496496       (unsigned long) len < hd.c_namesize) { 
    497497        g_free (name); 
    cpio_read_crc_head (struct vfs_class *me, struct vfs_s_super *super) 
    559559        return STATUS_FAIL; 
    560560    } 
    561561 
    562     name = g_malloc(hd.c_namesize); 
     562    name = g_malloc (hd.c_namesize); 
    563563    len = mc_read (super->u.arch.fd, name, hd.c_namesize); 
    564564 
    565565    if ((len == -1) || ((unsigned long) len < hd.c_namesize)) { 
  • vfs/mcfsutil.c

    diff --git a/vfs/mcfsutil.c b/vfs/mcfsutil.c
    index d3c2e04..4e776ea 100644
    a b rpc_get (int sock, ...) 
    185185 
    186186            /* Don't use glib functions here - this code is used by mcserv */ 
    187187            text = malloc (len + 1); 
     188            if (!text) 
     189                return 0; 
     190 
    188191            if (socket_read_block (sock, text, len) == 0) { 
    189192                free (text); 
    190193                va_end (ap); 
  • vfs/mcserv.c

    diff --git a/vfs/mcserv.c b/vfs/mcserv.c
    index 5fedc55..877b5cb 100644
    a b do_readdir (void) 
    514514        rpc_send (msock, RPC_BLOCK, length, dirent->d_name, RPC_END); 
    515515        fname_len = 
    516516            strlen (mcfs_DIR.names[handle]) + strlen (dirent->d_name) + 2; 
    517         fname = malloc (fname_len); 
     517        fname = g_malloc (fname_len); 
    518518        snprintf (fname, fname_len, "%s/%s", mcfs_DIR.names[handle], 
    519519                  dirent->d_name); 
    520520        n = lstat (fname, &st); 
  • vfs/samba/lib/charset.c

    diff --git a/vfs/samba/lib/charset.c b/vfs/samba/lib/charset.c
    index 35f9126..8f70def 100644
    a b multiple of 4.\n", codepage_file_name)); 
    289289  } 
    290290 
    291291  /* Allocate space for the code page file and read it all in. */ 
    292   if((cp_p = (codepage_p)malloc( size  + 4 )) == NULL) 
     292  if ((cp_p = (codepage_p) malloc ( size  + 4 )) == NULL) 
    293293  { 
    294294    DEBUG(0,("load_client_codepage: malloc fail.\n")); 
    295295    goto clean_and_exit; 
  • vfs/samba/lib/interface.c

    diff --git a/vfs/samba/lib/interface.c b/vfs/samba/lib/interface.c
    index 5acad19..f8e8687 100644
    a b static void interpret_interfaces(char *s, struct interface **interfaces, 
    151151      if (i) continue; 
    152152    } 
    153153 
    154     iface = (struct interface *)malloc(sizeof(*iface)); 
    155     if (!iface) return; 
     154    iface = (struct interface *) malloc (sizeof(*iface)); 
     155    if (!iface)  
     156      return; 
    156157 
    157158    iface->ip = ip; 
    158159 
    static void interpret_interfaces(char *s, struct interface **interfaces, 
    181182  if (*interfaces) return; 
    182183 
    183184  /* setup a default interface */ 
    184   iface = (struct interface *)malloc(sizeof(*iface)); 
    185   if (!iface) return; 
     185  iface = (struct interface *) malloc (sizeof(*iface)); 
     186  if (!iface)  
     187    return; 
    186188 
    187189  iface->next = NULL; 
    188190 
  • vfs/samba/lib/util.c

    diff --git a/vfs/samba/lib/util.c b/vfs/samba/lib/util.c
    index da2fa83..e31f485 100644
    a b void *Realloc(void *p,size_t size) 
    16481648  } 
    16491649 
    16501650  if (!p) 
    1651     ret = (void *)malloc(size); 
     1651    ret = (void *) malloc (size); 
    16521652  else 
    1653     ret = (void *)realloc(p,size); 
     1653    ret = (void *) realloc (p,size); 
    16541654 
    16551655#ifdef MEM_MAN 
    16561656  { 
    void set_namearray(name_compare_entry **ppname_array, char *namelist) 
    24772477  if(num_entries == 0) 
    24782478    return; 
    24792479 
    2480   if(( (*ppname_array) = (name_compare_entry *)malloc(  
     2480  if(( (*ppname_array) = (name_compare_entry *) g_malloc ( 
    24812481           (num_entries + 1) * sizeof(name_compare_entry))) == NULL) 
    24822482        { 
    24832483    DEBUG(0,("set_namearray: malloc fail\n")); 
  • vfs/samba/lib/util_str.c

    diff --git a/vfs/samba/lib/util_str.c b/vfs/samba/lib/util_str.c
    index 74b5303..c241dfd 100644
    a b char **toktocliplist(int *ctok, char *sep) 
    101101  *ctok=ictok; 
    102102  s=last_ptr; 
    103103 
    104   if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL; 
     104  if (!(ret = iret = g_malloc (ictok * sizeof(char *))))  
     105    return NULL; 
    105106   
    106107  while(ictok--) {     
    107108    *iret++=s; 
    BOOL string_init(char **dest,const char *src) 
    951952  if (l == 0) 
    952953    { 
    953954      if (!null_string) { 
    954         if((null_string = (char *)malloc(1)) == NULL) { 
     955        if ((null_string = (char *) malloc (1)) == NULL) { 
    955956          DEBUG(0,("string_init: malloc fail for null_string.\n")); 
    956957          return False; 
    957958        } 
    BOOL string_init(char **dest,const char *src) 
    961962    } 
    962963  else 
    963964    { 
    964       (*dest) = (char *)malloc(l+1); 
     965      (*dest) = (char *) g_malloc (l+1); 
    965966      if ((*dest) == NULL) { 
    966967              DEBUG(0,("Out of memory in string_init\n")); 
    967968              return False; 
  • vfs/samba/libsmb/clientgen.c

    diff --git a/vfs/samba/libsmb/clientgen.c b/vfs/samba/libsmb/clientgen.c
    index 0dd0710..05cceb2 100644
    a b initialise a client structure 
    23642364struct cli_state *cli_initialise(struct cli_state *cli) 
    23652365{ 
    23662366        if (!cli) { 
    2367                 cli = (struct cli_state *)malloc(sizeof(*cli)); 
     2367                cli = (struct cli_state *) malloc (sizeof(*cli)); 
    23682368                if (!cli) 
    23692369                        return NULL; 
    23702370                ZERO_STRUCTP(cli); 
    struct cli_state *cli_initialise(struct cli_state *cli) 
    23862386        cli->timeout = 20000; /* Timeout is in milliseconds. */ 
    23872387        cli->bufsize = CLI_BUFFER_SIZE+4; 
    23882388        cli->max_xmit = cli->bufsize; 
    2389         cli->outbuf = (char *)malloc(cli->bufsize); 
    2390         cli->inbuf = (char *)malloc(cli->bufsize); 
     2389        cli->outbuf = (char *) malloc (cli->bufsize); 
     2390        cli->inbuf = (char *) malloc (cli->bufsize); 
    23912391        if (!cli->outbuf || !cli->inbuf) 
    23922392        { 
    23932393                return False; 
  • vfs/samba/param/params.c

    diff --git a/vfs/samba/param/params.c b/vfs/samba/param/params.c
    index 46bbfc3..49c6a4c 100644
    a b BOOL pm_process( const char *FileName, 
    544544  else                                        /* If we don't have a buffer   */ 
    545545    {                                         /* allocate one, then parse,   */ 
    546546    bSize = BUFR_INC;                         /* then free.                  */ 
    547     bufr = (char *)malloc( bSize ); 
     547    bufr = (char *) malloc ( bSize ); 
    548548    if( NULL == bufr ) 
    549549      { 
    550550      DEBUG(0,("%s memory allocation failure.\n", func)); 
  • vfs/vfs.c

    diff --git a/vfs/vfs.c b/vfs/vfs.c
    index f8eb7a1..c489058 100644
    a b mc_readdir (DIR *dirp) 
    795795         * structures, holding dirent size. But we don't use it in libc infrastructure. 
    796796         * TODO: to make simpler homemade dirent-alike structure. 
    797797         */ 
    798         mc_readdir_result = (struct dirent *)malloc(sizeof(struct dirent) + NAME_MAX + 1); 
     798        mc_readdir_result = (struct dirent *) g_malloc (sizeof(struct dirent) + NAME_MAX + 1); 
    799799    } 
    800800 
    801801    if (!dirp) {