From d9a190ce3659666d38b99c12ac8564993a5f25e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20=27Flameeyes=27=20Petten=C3=B2?= Date: Tue, 18 Dec 2007 20:57:33 +0100 Subject: Use calloc() when the allocated size would be counted by multiplying the size of an item for the number of items. --- src/input/input_cdda.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'src/input/input_cdda.c') diff --git a/src/input/input_cdda.c b/src/input/input_cdda.c index df4617e22..8929a8af7 100644 --- a/src/input/input_cdda.c +++ b/src/input/input_cdda.c @@ -422,10 +422,9 @@ static int read_cdrom_toc(int fd, cdrom_toc *toc) { toc->total_tracks = toc->last_track - toc->first_track + 1; /* allocate space for the toc entries */ - toc->toc_entries = - (cdrom_toc_entry *)malloc(toc->total_tracks * sizeof(cdrom_toc_entry)); + toc->toc_entries = calloc(toc->total_tracks, sizeof(cdrom_toc_entry)); if (!toc->toc_entries) { - perror("malloc"); + perror("calloc"); return -1; } @@ -533,10 +532,9 @@ static int read_cdrom_toc(int fd, cdrom_toc *toc) { toc->total_tracks = toc->last_track - toc->first_track + 1; /* allocate space for the toc entries */ - toc->toc_entries = - (cdrom_toc_entry *)malloc(toc->total_tracks * sizeof(cdrom_toc_entry)); + toc->toc_entries = calloc(toc->total_tracks, sizeof(cdrom_toc_entry)); if (!toc->toc_entries) { - perror("malloc"); + perror("calloc"); return -1; } @@ -646,10 +644,9 @@ static int read_cdrom_toc(int fd, cdrom_toc *toc) { toc->total_tracks = toc->last_track - toc->first_track + 1; /* allocate space for the toc entries */ - toc->toc_entries = - (cdrom_toc_entry *)malloc(toc->total_tracks * sizeof(cdrom_toc_entry)); + toc->toc_entries = calloc(toc->total_tracks, sizeof(cdrom_toc_entry)); if (!toc->toc_entries) { - perror("malloc"); + perror("calloc"); return -1; } @@ -839,10 +836,9 @@ static int read_cdrom_toc(cdda_input_plugin_t *this_gen, cdrom_toc *toc) { /* allocate space for the toc entries */ - toc->toc_entries = - (cdrom_toc_entry *)malloc(toc->total_tracks * sizeof(cdrom_toc_entry)); + toc->toc_entries = calloc(toc->total_tracks, sizeof(cdrom_toc_entry)); if (!toc->toc_entries) { - perror("malloc"); + perror("calloc"); return -1; } @@ -1085,10 +1081,9 @@ static int network_read_cdrom_toc(xine_stream_t *stream, int fd, cdrom_toc *toc) toc->total_tracks = toc->last_track - toc->first_track + 1; /* allocate space for the toc entries */ - toc->toc_entries = - (cdrom_toc_entry *)malloc(toc->total_tracks * sizeof(cdrom_toc_entry)); + toc->toc_entries = calloc(toc->total_tracks, sizeof(cdrom_toc_entry)); if (!toc->toc_entries) { - perror("malloc"); + perror("calloc"); return -1; } -- cgit v1.2.3 From 9c866afab8308a58ce03555a61daa4380c09d73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20=27Flameeyes=27=20Petten=C3=B2?= Date: Tue, 18 Dec 2007 20:59:29 +0100 Subject: Use zeroed allocation instead of memset, and don't zero out a string that is going to be fully sprintf'd. --- src/input/input_cdda.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'src/input/input_cdda.c') diff --git a/src/input/input_cdda.c b/src/input/input_cdda.c index 8929a8af7..e658e30ca 100644 --- a/src/input/input_cdda.c +++ b/src/input/input_cdda.c @@ -1287,15 +1287,12 @@ static void _cdda_mkdir_safe(xine_t *xine, char *path) { */ static void _cdda_mkdir_recursive_safe(xine_t *xine, char *path) { char *p, *pp; - char buf[XINE_PATH_MAX + XINE_NAME_MAX + 1]; - char buf2[XINE_PATH_MAX + XINE_NAME_MAX + 1]; + char buf[XINE_PATH_MAX + XINE_NAME_MAX + 1] = { 0, }; + char buf2[XINE_PATH_MAX + XINE_NAME_MAX + 1] = { 0, }; if(path == NULL) return; - memset(&buf, 0, sizeof(buf)); - memset(&buf2, 0, sizeof(buf2)); - snprintf(buf, sizeof(buf), "%s", path); pp = buf; while((p = xine_strsep(&pp, "/")) != NULL) { @@ -1438,7 +1435,6 @@ static int _cdda_load_cached_cddb_infos(cdda_input_plugin_t *this) { while((pdir = readdir(dir)) != NULL) { char discid[9]; - memset(&discid, 0, sizeof(discid)); snprintf(discid, sizeof(discid), "%08lx", this->cddb.disc_id); if(!strcasecmp(pdir->d_name, discid)) { -- cgit v1.2.3 From 4ccb69e98ecdca80bad9e656d686b0086ca3149a Mon Sep 17 00:00:00 2001 From: Darren Salt Date: Tue, 18 Dec 2007 21:55:52 +0000 Subject: Clean up recursive mkdir(). --- src/input/input_cdda.c | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) (limited to 'src/input/input_cdda.c') diff --git a/src/input/input_cdda.c b/src/input/input_cdda.c index e658e30ca..ed6f2b4f9 100644 --- a/src/input/input_cdda.c +++ b/src/input/input_cdda.c @@ -1283,39 +1283,29 @@ static void _cdda_mkdir_safe(xine_t *xine, char *path) { } /* - * Make recursive directory creation + * Make recursive directory creation (given an absolute pathname) */ -static void _cdda_mkdir_recursive_safe(xine_t *xine, char *path) { - char *p, *pp; - char buf[XINE_PATH_MAX + XINE_NAME_MAX + 1] = { 0, }; - char buf2[XINE_PATH_MAX + XINE_NAME_MAX + 1] = { 0, }; - - if(path == NULL) +static void _cdda_mkdir_recursive_safe (xine_t *xine, char *path) +{ + if (!path) return; - snprintf(buf, sizeof(buf), "%s", path); - pp = buf; - while((p = xine_strsep(&pp, "/")) != NULL) { - if(p && strlen(p)) { - -#ifdef WIN32 - if (*buf2 != '\0') { -#endif - - int size = strlen(buf2); - snprintf(buf2 + size, sizeof(buf2) - size, "/%s", p); - -#ifdef WIN32 - } - else { - snprintf(buf2, sizeof(buf2), "%s", p); - } + char buf[strlen (path) + 1]; + strcpy (buf, path); + char *p = strchr (buf, '/') ? : buf; -#endif /* WIN32 */ + do + { + while (*p++ == '/') /**/; + p = strchr (p, '/'); + if (p) + *p = 0; + _cdda_mkdir_safe (xine, buf); + if (p) + *p = '/'; + } while (p); - _cdda_mkdir_safe(xine, buf2); - } - } + return 0; } /* -- cgit v1.2.3