[libdvdnav-devel] [PATCH 1/2] Do not needlessly cast the return value of calloc/malloc.

Jean-Baptiste Kempf jb at videolan.org
Sat Nov 30 20:04:16 CET 2013


LGTM.

On 30 Nov, Diego Biurrun wrote :
> ---
>  msvc/contrib/dirent/dirent.c |  4 +--
>  src/dvd_input.c              |  4 +--
>  src/dvd_reader.c             | 14 +++++-----
>  src/ifo_read.c               | 64 ++++++++++++++++++++++----------------------
>  4 files changed, 43 insertions(+), 43 deletions(-)
> 
> diff --git a/msvc/contrib/dirent/dirent.c b/msvc/contrib/dirent/dirent.c
> index 00289ed..0eddd90 100644
> --- a/msvc/contrib/dirent/dirent.c
> +++ b/msvc/contrib/dirent/dirent.c
> @@ -46,8 +46,8 @@ DIR *opendir(const char *name)
>          const char *all = /* the root directory is a special case... */
>              strchr("/\\", name[base_length - 1]) ? "*" : "/*";
>  
> -        if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
> -           (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
> +        if((dir = malloc(sizeof *dir)) != 0 &&
> +           (dir->name = malloc(base_length + strlen(all) + 1)) != 0)
>          {
>              strcat(strcpy(dir->name, name), all);
>  
> diff --git a/src/dvd_input.c b/src/dvd_input.c
> index d744eb5..f7e22cf 100644
> --- a/src/dvd_input.c
> +++ b/src/dvd_input.c
> @@ -84,7 +84,7 @@ static dvd_input_t css_open(const char *target)
>    dvd_input_t dev;
>  
>    /* Allocate the handle structure */
> -  dev = (dvd_input_t) malloc(sizeof(*dev));
> +  dev = malloc(sizeof(*dev));
>    if(dev == NULL) {
>      fprintf(stderr, "libdvdread: Could not allocate memory.\n");
>      return NULL;
> @@ -159,7 +159,7 @@ static dvd_input_t file_open(const char *target)
>    dvd_input_t dev;
>  
>    /* Allocate the library structure */
> -  dev = (dvd_input_t) malloc(sizeof(*dev));
> +  dev = malloc(sizeof(*dev));
>    if(dev == NULL) {
>      fprintf(stderr, "libdvdread: Could not allocate memory.\n");
>      return NULL;
> diff --git a/src/dvd_reader.c b/src/dvd_reader.c
> index 02c10d2..64105ee 100644
> --- a/src/dvd_reader.c
> +++ b/src/dvd_reader.c
> @@ -241,7 +241,7 @@ static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css )
>      return NULL;
>    }
>  
> -  dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
> +  dvd = malloc( sizeof( dvd_reader_t ) );
>    if( !dvd ) {
>      dvdinput_close(dev);
>      return NULL;
> @@ -269,7 +269,7 @@ static dvd_reader_t *DVDOpenPath( const char *path_root )
>  {
>    dvd_reader_t *dvd;
>  
> -  dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
> +  dvd = malloc( sizeof( dvd_reader_t ) );
>    if( !dvd ) return NULL;
>    dvd->isImageFile = 0;
>    dvd->dev = 0;
> @@ -632,7 +632,7 @@ static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *dvd, char *filename )
>      return NULL;
>    }
>  
> -  dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
> +  dvd_file = malloc( sizeof( dvd_file_t ) );
>    if( !dvd_file ) {
>      fprintf( stderr, "libdvdnav:DVDOpenFileUDF:malloc failed\n" );
>      return NULL;
> @@ -727,7 +727,7 @@ static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename )
>      return NULL;
>    }
>  
> -  dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
> +  dvd_file = malloc( sizeof( dvd_file_t ) );
>    if( !dvd_file ) {
>      fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvd_file malloc failed\n" );
>      dvdinput_close(dev);
> @@ -767,7 +767,7 @@ static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu )
>    start = UDFFindFile( dvd, filename, &len );
>    if( start == 0 ) return NULL;
>  
> -  dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
> +  dvd_file = malloc( sizeof( dvd_file_t ) );
>    if( !dvd_file ) return NULL;
>    dvd_file->dvd = dvd;
>    /*Hack*/ dvd_file->css_title = title << 1 | menu;
> @@ -810,7 +810,7 @@ static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu )
>    dvd_file_t *dvd_file;
>    int i;
>  
> -  dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
> +  dvd_file = malloc( sizeof( dvd_file_t ) );
>    if( !dvd_file ) return NULL;
>    dvd_file->dvd = dvd;
>    /*Hack*/ dvd_file->css_title = title << 1 | menu;
> @@ -1315,7 +1315,7 @@ ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size )
>    numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) +
>      ( ( ( seek_byte + byte_size ) % DVD_VIDEO_LB_LEN ) ? 1 : 0 );
>  
> -  secbuf_base = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN + 2048 );
> +  secbuf_base = malloc( numsec * DVD_VIDEO_LB_LEN + 2048 );
>    secbuf = (unsigned char *)(((uintptr_t)secbuf_base & ~((uintptr_t)2047)) + 2048);
>    if( !secbuf_base ) {
>      fprintf( stderr, "libdvdread: Can't allocate memory "
> diff --git a/src/ifo_read.c b/src/ifo_read.c
> index 059a377..e556659 100644
> --- a/src/ifo_read.c
> +++ b/src/ifo_read.c
> @@ -292,7 +292,7 @@ ifo_handle_t *ifoOpen(dvd_reader_t *dvd, int title) {
>    int bup_file_opened = 0;
>    char ifo_filename[13];
>  
> -  ifofile = (ifo_handle_t *)malloc(sizeof(ifo_handle_t));
> +  ifofile = malloc(sizeof(ifo_handle_t));
>    if(!ifofile)
>      return NULL;
>  
> @@ -361,7 +361,7 @@ ifoOpen_try_bup:
>    /* Try BUP instead */
>    ifoClose(ifofile);
>  
> -  ifofile = (ifo_handle_t *)malloc(sizeof(ifo_handle_t));
> +  ifofile = malloc(sizeof(ifo_handle_t));
>    if(!ifofile)
>      return NULL;
>  
> @@ -427,7 +427,7 @@ ifoOpen_fail:
>  ifo_handle_t *ifoOpenVMGI(dvd_reader_t *dvd) {
>    ifo_handle_t *ifofile;
>  
> -  ifofile = (ifo_handle_t *)malloc(sizeof(ifo_handle_t));
> +  ifofile = malloc(sizeof(ifo_handle_t));
>    if(!ifofile)
>      return NULL;
>  
> @@ -454,7 +454,7 @@ ifo_handle_t *ifoOpenVMGI(dvd_reader_t *dvd) {
>  ifo_handle_t *ifoOpenVTSI(dvd_reader_t *dvd, int title) {
>    ifo_handle_t *ifofile;
>  
> -  ifofile = (ifo_handle_t *)malloc(sizeof(ifo_handle_t));
> +  ifofile = malloc(sizeof(ifo_handle_t));
>    if(!ifofile)
>      return NULL;
>  
> @@ -519,7 +519,7 @@ void ifoClose(ifo_handle_t *ifofile) {
>  static int ifoRead_VMG(ifo_handle_t *ifofile) {
>    vmgi_mat_t *vmgi_mat;
>  
> -  vmgi_mat = (vmgi_mat_t *)malloc(sizeof(vmgi_mat_t));
> +  vmgi_mat = malloc(sizeof(vmgi_mat_t));
>    if(!vmgi_mat)
>      return 0;
>  
> @@ -611,7 +611,7 @@ static int ifoRead_VTS(ifo_handle_t *ifofile) {
>    vtsi_mat_t *vtsi_mat;
>    int i;
>  
> -  vtsi_mat = (vtsi_mat_t *)malloc(sizeof(vtsi_mat_t));
> +  vtsi_mat = malloc(sizeof(vtsi_mat_t));
>    if(!vtsi_mat)
>      return 0;
>  
> @@ -742,7 +742,7 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t *ifofile,
>  
>    if(cmd_tbl->nr_of_pre != 0) {
>      unsigned int pre_cmds_size  = cmd_tbl->nr_of_pre * COMMAND_DATA_SIZE;
> -    cmd_tbl->pre_cmds = (vm_cmd_t *)malloc(pre_cmds_size);
> +    cmd_tbl->pre_cmds = malloc(pre_cmds_size);
>      if(!cmd_tbl->pre_cmds)
>        return 0;
>  
> @@ -754,7 +754,7 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t *ifofile,
>  
>    if(cmd_tbl->nr_of_post != 0) {
>      unsigned int post_cmds_size = cmd_tbl->nr_of_post * COMMAND_DATA_SIZE;
> -    cmd_tbl->post_cmds = (vm_cmd_t *)malloc(post_cmds_size);
> +    cmd_tbl->post_cmds = malloc(post_cmds_size);
>      if(!cmd_tbl->post_cmds) {
>        if(cmd_tbl->pre_cmds)
>          free(cmd_tbl->pre_cmds);
> @@ -770,7 +770,7 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t *ifofile,
>  
>    if(cmd_tbl->nr_of_cell != 0) {
>      unsigned int cell_cmds_size = cmd_tbl->nr_of_cell * COMMAND_DATA_SIZE;
> -    cmd_tbl->cell_cmds = (vm_cmd_t *)malloc(cell_cmds_size);
> +    cmd_tbl->cell_cmds = malloc(cell_cmds_size);
>      if(!cmd_tbl->cell_cmds) {
>        if(cmd_tbl->pre_cmds)
>          free(cmd_tbl->pre_cmds);
> @@ -985,7 +985,7 @@ int ifoRead_FP_PGC(ifo_handle_t *ifofile) {
>    if(ifofile->vmgi_mat->first_play_pgc == 0)
>      return 1;
>  
> -  ifofile->first_play_pgc = (pgc_t *)calloc(1, sizeof(pgc_t));
> +  ifofile->first_play_pgc = calloc(1, sizeof(pgc_t));
>    if(!ifofile->first_play_pgc)
>      return 0;
>  
> @@ -1041,7 +1041,7 @@ int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
>    if(!DVDFileSeek_(ifofile->file, ifofile->vmgi_mat->tt_srpt * DVD_BLOCK_LEN))
>      return 0;
>  
> -  tt_srpt = (tt_srpt_t *)malloc(sizeof(tt_srpt_t));
> +  tt_srpt = malloc(sizeof(tt_srpt_t));
>    if(!tt_srpt)
>      return 0;
>  
> @@ -1058,7 +1058,7 @@ int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
>  
>    info_length = tt_srpt->last_byte + 1 - TT_SRPT_SIZE;
>  
> -  tt_srpt->title = (title_info_t *)malloc(info_length);
> +  tt_srpt->title = malloc(info_length);
>    if(!tt_srpt->title) {
>      free(tt_srpt);
>      ifofile->tt_srpt = 0;
> @@ -1306,7 +1306,7 @@ int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) {
>    if(!DVDFileSeek_(ifofile->file, ifofile->vmgi_mat->ptl_mait * DVD_BLOCK_LEN))
>      return 0;
>  
> -  ptl_mait = (ptl_mait_t *)malloc(sizeof(ptl_mait_t));
> +  ptl_mait = malloc(sizeof(ptl_mait_t));
>    if(!ptl_mait)
>      return 0;
>  
> @@ -1330,7 +1330,7 @@ int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) {
>                <= ptl_mait->last_byte + 1 - PTL_MAIT_SIZE);
>  
>    info_length = ptl_mait->nr_of_countries * sizeof(ptl_mait_country_t);
> -  ptl_mait->countries = (ptl_mait_country_t *)malloc(info_length);
> +  ptl_mait->countries = malloc(info_length);
>    if(!ptl_mait->countries) {
>      free(ptl_mait);
>      ifofile->ptl_mait = NULL;
> @@ -1375,7 +1375,7 @@ int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) {
>        return 0;
>      }
>      info_length = (ptl_mait->nr_of_vtss + 1) * sizeof(pf_level_t);
> -    pf_temp = (uint16_t *)malloc(info_length);
> +    pf_temp = malloc(info_length);
>      if(!pf_temp) {
>        free_ptl_mait(ptl_mait, i);
>        ifofile->ptl_mait = NULL;
> @@ -1392,7 +1392,7 @@ int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) {
>      for (j = 0; j < ((ptl_mait->nr_of_vtss + 1) * 8); j++) {
>        B2N_16(pf_temp[j]);
>      }
> -    ptl_mait->countries[i].pf_ptl_mai = (pf_level_t *)malloc(info_length);
> +    ptl_mait->countries[i].pf_ptl_mai = malloc(info_length);
>      if(!ptl_mait->countries[i].pf_ptl_mai) {
>        free(pf_temp);
>        free_ptl_mait(ptl_mait, i);
> @@ -1453,7 +1453,7 @@ int ifoRead_VTS_TMAPT(ifo_handle_t *ifofile) {
>    if(!DVDFileSeek_(ifofile->file, offset))
>      return 0;
>  
> -  vts_tmapt = (vts_tmapt_t *)malloc(sizeof(vts_tmapt_t));
> +  vts_tmapt = malloc(sizeof(vts_tmapt_t));
>    if(!vts_tmapt)
>      return 0;
>  
> @@ -1473,7 +1473,7 @@ int ifoRead_VTS_TMAPT(ifo_handle_t *ifofile) {
>  
>    info_length = vts_tmapt->nr_of_tmaps * 4;
>  
> -  vts_tmap_srp = (uint32_t *)malloc(info_length);
> +  vts_tmap_srp = malloc(info_length);
>    if(!vts_tmap_srp) {
>      free(vts_tmapt);
>      ifofile->vts_tmapt = NULL;
> @@ -1497,7 +1497,7 @@ int ifoRead_VTS_TMAPT(ifo_handle_t *ifofile) {
>  
>    info_length = vts_tmapt->nr_of_tmaps * sizeof(vts_tmap_t);
>  
> -  vts_tmapt->tmap = (vts_tmap_t *)malloc(info_length);
> +  vts_tmapt->tmap = malloc(info_length);
>    if(!vts_tmapt->tmap) {
>      free(vts_tmap_srp);
>      free(vts_tmapt);
> @@ -1529,7 +1529,7 @@ int ifoRead_VTS_TMAPT(ifo_handle_t *ifofile) {
>  
>      info_length = vts_tmapt->tmap[i].nr_of_entries * sizeof(map_ent_t);
>  
> -    vts_tmapt->tmap[i].map_ent = (map_ent_t *)malloc(info_length);
> +    vts_tmapt->tmap[i].map_ent = malloc(info_length);
>      if(!vts_tmapt->tmap[i].map_ent) {
>        ifoFree_VTS_TMAPT(ifofile);
>        return 0;
> @@ -1577,7 +1577,7 @@ int ifoRead_TITLE_C_ADT(ifo_handle_t *ifofile) {
>    if(ifofile->vtsi_mat->vts_c_adt == 0) /* mandatory */
>      return 0;
>  
> -  ifofile->vts_c_adt = (c_adt_t *)malloc(sizeof(c_adt_t));
> +  ifofile->vts_c_adt = malloc(sizeof(c_adt_t));
>    if(!ifofile->vts_c_adt)
>      return 0;
>  
> @@ -1609,7 +1609,7 @@ int ifoRead_C_ADT(ifo_handle_t *ifofile) {
>      return 0;
>    }
>  
> -  ifofile->menu_c_adt = (c_adt_t *)malloc(sizeof(c_adt_t));
> +  ifofile->menu_c_adt = malloc(sizeof(c_adt_t));
>    if(!ifofile->menu_c_adt)
>      return 0;
>  
> @@ -1651,7 +1651,7 @@ static int ifoRead_C_ADT_internal(ifo_handle_t *ifofile,
>      c_adt->nr_of_vobs = info_length / sizeof(cell_adr_t);
>    }
>  
> -  c_adt->cell_adr_table = (cell_adr_t *)malloc(info_length);
> +  c_adt->cell_adr_table = malloc(info_length);
>    if(!c_adt->cell_adr_table)
>      return 0;
>  
> @@ -1711,7 +1711,7 @@ int ifoRead_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) {
>    if(ifofile->vtsi_mat->vts_vobu_admap == 0) /* mandatory */
>      return 0;
>  
> -  ifofile->vts_vobu_admap = (vobu_admap_t *)malloc(sizeof(vobu_admap_t));
> +  ifofile->vts_vobu_admap = malloc(sizeof(vobu_admap_t));
>    if(!ifofile->vts_vobu_admap)
>      return 0;
>  
> @@ -1743,7 +1743,7 @@ int ifoRead_VOBU_ADMAP(ifo_handle_t *ifofile) {
>      return 0;
>    }
>  
> -  ifofile->menu_vobu_admap = (vobu_admap_t *)malloc(sizeof(vobu_admap_t));
> +  ifofile->menu_vobu_admap = malloc(sizeof(vobu_admap_t));
>    if(!ifofile->menu_vobu_admap)
>      return 0;
>  
> @@ -1776,7 +1776,7 @@ static int ifoRead_VOBU_ADMAP_internal(ifo_handle_t *ifofile,
>       Titles with a VOBS that has no VOBUs. */
>    CHECK_VALUE(info_length % sizeof(uint32_t) == 0);
>  
> -  vobu_admap->vobu_start_sectors = (uint32_t *)malloc(info_length);
> +  vobu_admap->vobu_start_sectors = malloc(info_length);
>    if(!vobu_admap->vobu_start_sectors) {
>      return 0;
>    }
> @@ -1828,7 +1828,7 @@ int ifoRead_PGCIT(ifo_handle_t *ifofile) {
>    if(ifofile->vtsi_mat->vts_pgcit == 0) /* mandatory */
>      return 0;
>  
> -  ifofile->vts_pgcit = (pgcit_t *)calloc(1, sizeof(pgcit_t));
> +  ifofile->vts_pgcit = calloc(1, sizeof(pgcit_t));
>    if(!ifofile->vts_pgcit)
>      return 0;
>  
> @@ -1991,7 +1991,7 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
>      return 0;
>    }
>  
> -  ifofile->pgci_ut = (pgci_ut_t *)malloc(sizeof(pgci_ut_t));
> +  ifofile->pgci_ut = malloc(sizeof(pgci_ut_t));
>    if(!ifofile->pgci_ut)
>      return 0;
>  
> @@ -2186,7 +2186,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
>    if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN))
>      return 0;
>  
> -  vts_atrt = (vts_atrt_t *)malloc(sizeof(vts_atrt_t));
> +  vts_atrt = malloc(sizeof(vts_atrt_t));
>    if(!vts_atrt)
>      return 0;
>  
> @@ -2208,7 +2208,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
>                VTS_ATRT_SIZE < vts_atrt->last_byte + 1);
>  
>    info_length = vts_atrt->nr_of_vtss * sizeof(uint32_t);
> -  data = (uint32_t *)malloc(info_length);
> +  data = malloc(info_length);
>    if(!data) {
>      free(vts_atrt);
>      ifofile->vts_atrt = 0;
> @@ -2230,7 +2230,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
>    }
>  
>    info_length = vts_atrt->nr_of_vtss * sizeof(vts_attributes_t);
> -  vts_atrt->vts = (vts_attributes_t *)malloc(info_length);
> +  vts_atrt->vts = malloc(info_length);
>    if(!vts_atrt->vts) {
>      free(data);
>      free(vts_atrt);
> @@ -2286,7 +2286,7 @@ int ifoRead_TXTDT_MGI(ifo_handle_t *ifofile) {
>                     ifofile->vmgi_mat->txtdt_mgi * DVD_BLOCK_LEN))
>      return 0;
>  
> -  txtdt_mgi = (txtdt_mgi_t *)malloc(sizeof(txtdt_mgi_t));
> +  txtdt_mgi = malloc(sizeof(txtdt_mgi_t));
>    if(!txtdt_mgi) {
>      return 0;
>    }
> -- 
> 1.8.3.2
> 
> _______________________________________________
> libdvdnav-devel mailing list
> libdvdnav-devel at videolan.org
> https://mailman.videolan.org/listinfo/libdvdnav-devel

-- 
With my kindest regards,

-- 
Jean-Baptiste Kempf
http://www.jbkempf.com/ - +33 672 704 734
Sent from my Electronic Device


More information about the libdvdnav-devel mailing list