diff --git a/lib/driver/image/bincue.c b/lib/driver/image/bincue.c index 79749d63..f76a6497 100644 --- a/lib/driver/image/bincue.c +++ b/lib/driver/image/bincue.c @@ -289,6 +289,7 @@ parse_cuefile (_img_private_t *cd, const char *psz_cue_name) unsigned int i_line=0; /* line number in file of psz_line. */ int i = -1; /* Position in tocent. Same as cd->gen.i_tracks - 1 */ + int i_first_track = 1; /* Number of the first track */ char *psz_keyword, *psz_field, *psz_cue_name_dup; cdio_log_level_t log_level = (NULL == cd) ? CDIO_LOG_INFO : CDIO_LOG_WARN; cdtext_field_t cdtext_key; @@ -366,7 +367,10 @@ parse_cuefile (_img_private_t *cd, const char *psz_cue_name) } } - if (cd) cd->psz_mcn = strdup (psz_field); + if (cd) { + free(cd->psz_mcn); + cd->psz_mcn = strdup (psz_field); + } if (NULL != (psz_field = strtok_r(NULL, " \t\n\r", &saveptr))) { goto format_error; } @@ -460,9 +464,13 @@ parse_cuefile (_img_private_t *cd, const char *psz_cue_name) char *dirname = cdio_dirname(psz_cue_name); char *filename = cdio_abspath(dirname, psz_field); if (cd) { + /* The "source" argument and an earlier FILE are replaced */ + free(cd->gen.source_name); cd->gen.source_name = strdup(filename); - if (i >= 0) + if (i >= 0) { + free(cd->tocent[i].filename); cd->tocent[i].filename = strdup(filename); + } } free(filename); free(dirname); @@ -488,14 +496,18 @@ parse_cuefile (_img_private_t *cd, const char *psz_cue_name) "Track number out of range 1 to %d, got %s.", CDIO_CD_MAX_TRACKS, psz_field); goto err_exit; } - if(cd) { - if (-1 == i) { + /* Track numbers must follow each other, so the last one is + at most 99 too. */ + if (-1 == i) { + i_first_track = i_track; + if (cd) cd->gen.i_first_track = i_track; - } else if(cd->gen.i_first_track + i + 1 != i_track) { - cdio_log(log_level, - "Track number out of sequence. Expected %d, got %d", - cd->gen.i_first_track + i + 1, i_track); - } + } else if (i_first_track + i + 1 != i_track) { + cdio_log(log_level, + "%s line %d: track number out of sequence. " + "Expected %d, got %d", psz_cue_name, i_line, + i_first_track + i + 1, i_track); + goto err_exit; } } if (NULL != (psz_field = strtok_r(NULL, " \t\n\r", &saveptr))) { @@ -767,7 +779,10 @@ parse_cuefile (_img_private_t *cd, const char *psz_cue_name) } else if (0 == strcmp("ISRC", psz_keyword)) { if (0 <= i) { if (NULL != (psz_field = strtok_r(NULL, " \t\n\r", &saveptr))) { - if (cd) cd->tocent[i].isrc = strdup (psz_field); + if (cd) { + free(cd->tocent[i].isrc); + cd->tocent[i].isrc = strdup (psz_field); + } } else { goto format_error; } diff --git a/lib/driver/image/cdrdao.c b/lib/driver/image/cdrdao.c index ad830746..7eff01f8 100644 --- a/lib/driver/image/cdrdao.c +++ b/lib/driver/image/cdrdao.c @@ -304,6 +304,19 @@ get_disc_last_lsn_cdrdao (void *p_user_data) psz_cue_name, i_line, psz_keyword) +/* Free the file a track read so far, when FILE or DATAFILE is given + again for the same track */ +static void +free_track_file(track_info_t *p_tocent) +{ + free(p_tocent->filename); + p_tocent->filename = NULL; + if (p_tocent->data_source) { + cdio_stdio_destroy(p_tocent->data_source); + p_tocent->data_source = NULL; + } +} + static bool parse_tocfile (_img_private_t *cd, const char *psz_cue_name) { @@ -380,7 +393,10 @@ parse_tocfile (_img_private_t *cd, const char *psz_cue_name) goto err_exit; } } - if (NULL != cd) cd->psz_mcn = strdup (psz_field); + if (NULL != cd) { + free(cd->psz_mcn); + cd->psz_mcn = strdup (psz_field); + } } } else { cdio_log(log_level, @@ -709,8 +725,10 @@ parse_tocfile (_img_private_t *cd, const char *psz_cue_name) if (i_track < 0) goto not_in_global_section; if (NULL != (psz_field = strtok_r(NULL, "\"\t\n\r", &saveptr))) { - if (NULL != cd) + if (NULL != cd) { + free(cd->tocent[i_track].isrc); cd->tocent[i_track].isrc = strdup(psz_field); + } } else { goto format_error; } @@ -748,6 +766,7 @@ parse_tocfile (_img_private_t *cd, const char *psz_cue_name) char *psz_filename = cdio_abspath(psz_dirname, psz_field); free(psz_dirname); if (cd) { + free_track_file(&cd->tocent[i_track]); cd->tocent[i_track].filename = strdup (psz_filename); /* To do: do something about reusing existing files. */ if (!(cd->tocent[i_track].data_source = cdio_stdio_new (psz_filename))) { @@ -827,6 +846,7 @@ parse_tocfile (_img_private_t *cd, const char *psz_cue_name) char *psz_dirname = cdio_dirname(psz_cue_name); char *psz_filename = cdio_abspath(psz_dirname, psz_field); if (cd) { + free_track_file(&cd->tocent[i_track]); cd->tocent[i_track].filename = strdup(psz_filename); /* To do: do something about reusing existing files. */ if (!(cd->tocent[i_track].data_source = cdio_stdio_new (psz_filename))) { @@ -1074,6 +1094,9 @@ parse_tocfile (_img_private_t *cd, const char *psz_cue_name) psz_cue_name, i_line, psz_keyword); err_exit: + /* Let the caller free the tracks read so far */ + if (NULL != cd) + cd->gen.i_tracks = i_track+1; fclose (fp); return false; } diff --git a/lib/driver/image/nrg.c b/lib/driver/image/nrg.c index 950ae644..9863724f 100644 --- a/lib/driver/image/nrg.c +++ b/lib/driver/image/nrg.c @@ -507,9 +507,12 @@ parse_nrg (_img_private_t *p_env, const char *psz_nrg_name, disc_mode = _dao_common->unknown[1]; /* We include an extra 0 byte so these can be used as C strings.*/ + free(p_env->psz_mcn); p_env->psz_mcn = calloc(1, CDIO_MCN_SIZE+1); - if (p_env->psz_mcn == NULL) + if (p_env->psz_mcn == NULL) { + free(footer_buf); return false; + } memcpy(p_env->psz_mcn, &(_dao_common->psz_mcn), CDIO_MCN_SIZE); p_env->psz_mcn[CDIO_MCN_SIZE] = '\0'; @@ -619,7 +622,12 @@ parse_nrg (_img_private_t *p_env, const char *psz_nrg_name, } else { _dao_array_common = &_ientries->track_info[i].common; } + free(p_env->tocent[i].isrc); p_env->tocent[i].isrc = calloc(1, CDIO_ISRC_SIZE+1); + if (p_env->tocent[i].isrc == NULL) { + free(footer_buf); + return false; + } memcpy(p_env->tocent[i].isrc, _dao_array_common->psz_isrc, CDIO_ISRC_SIZE); p_env->tocent[i].isrc[CDIO_ISRC_SIZE] = '\0'; if (p_env->tocent[i].isrc[0]) { @@ -922,6 +930,8 @@ parse_nrg (_img_private_t *p_env, const char *psz_nrg_name, free(footer_buf); return false; } + /* A later CD TEXT chunk replaces an earlier one */ + cdtext_destroy(p_env->gen.cdtext); p_env->gen.cdtext = cdtext_init (); if(0 !=cdtext_data_init (p_env->gen.cdtext, wdata, len)) { diff --git a/lib/driver/image_common.c b/lib/driver/image_common.c index 0c55c9ce..c5281ac3 100644 --- a/lib/driver/image_common.c +++ b/lib/driver/image_common.c @@ -241,6 +241,17 @@ _get_track_msf_image(void *p_user_data, track_t i_track, msf_t *msf) return false; } +/* Return the tocent[] entry of track number i_track, or NULL if the + image has no such track. */ +static const track_info_t * +get_tocent_image(const _img_private_t *p_env, track_t i_track) +{ + if (i_track < p_env->gen.i_first_track + || i_track >= p_env->gen.i_first_track + p_env->gen.i_tracks) + return NULL; + return &p_env->tocent[i_track - p_env->gen.i_first_track]; +} + /** Return number of channels in track: 2 or 4; -2 if not implemented or -1 for error. Not meaningful if track is not an audio track. @@ -248,9 +259,10 @@ _get_track_msf_image(void *p_user_data, track_t i_track, msf_t *msf) int get_track_channels_image(const void *p_user_data, track_t i_track) { - const _img_private_t *p_env = p_user_data; - return ( p_env->tocent[i_track-p_env->gen.i_first_track].flags - & FOUR_CHANNEL_AUDIO ) ? 4 : 2; + const track_info_t *p_tocent = get_tocent_image(p_user_data, i_track); + + if (!p_tocent) return -1; + return ( p_tocent->flags & FOUR_CHANNEL_AUDIO ) ? 4 : 2; } /** Return 1 if copy is permitted on the track, 0 if not, or -1 for error. @@ -259,9 +271,11 @@ get_track_channels_image(const void *p_user_data, track_t i_track) track_flag_t get_track_copy_permit_image(void *p_user_data, track_t i_track) { - const _img_private_t *p_env = p_user_data; - return ( p_env->tocent[i_track-p_env->gen.i_first_track].flags - & COPY_PERMITTED ) ? CDIO_TRACK_FLAG_TRUE : CDIO_TRACK_FLAG_FALSE; + const track_info_t *p_tocent = get_tocent_image(p_user_data, i_track); + + if (!p_tocent) return CDIO_TRACK_FLAG_ERROR; + return ( p_tocent->flags & COPY_PERMITTED ) + ? CDIO_TRACK_FLAG_TRUE : CDIO_TRACK_FLAG_FALSE; } /** Return 1 if track has pre-emphasis, 0 if not, or -1 for error. @@ -272,9 +286,11 @@ get_track_copy_permit_image(void *p_user_data, track_t i_track) track_flag_t get_track_preemphasis_image(const void *p_user_data, track_t i_track) { - const _img_private_t *p_env = p_user_data; - return ( p_env->tocent[i_track-p_env->gen.i_first_track].flags - & PRE_EMPHASIS ) ? CDIO_TRACK_FLAG_TRUE : CDIO_TRACK_FLAG_FALSE; + const track_info_t *p_tocent = get_tocent_image(p_user_data, i_track); + + if (!p_tocent) return CDIO_TRACK_FLAG_ERROR; + return ( p_tocent->flags & PRE_EMPHASIS ) + ? CDIO_TRACK_FLAG_TRUE : CDIO_TRACK_FLAG_FALSE; } /** Return the starting LBA for the pregap for track number i_track. @@ -284,11 +300,12 @@ get_track_preemphasis_image(const void *p_user_data, track_t i_track) lba_t get_track_pregap_lba_image(const void *p_user_data, track_t i_track) { - const _img_private_t *p_env = p_user_data; + const track_info_t *p_tocent = get_tocent_image(p_user_data, i_track); lba_t pregap, start_lba; - pregap = p_env->tocent[i_track-p_env->gen.i_first_track].pregap; - start_lba = p_env->tocent[i_track-p_env->gen.i_first_track].start_lba; + if (!p_tocent) return CDIO_INVALID_LBA; + pregap = p_tocent->pregap; + start_lba = p_tocent->start_lba; /* avoid initializing pregap to CDIO_INVALID_LBA by letting calloc do the work. also, nero files have the pregap set equal @@ -311,8 +328,8 @@ get_track_pregap_lba_image(const void *p_user_data, track_t i_track) char * get_track_isrc_image(const void *p_user_data, track_t i_track) { - const _img_private_t *p_env = p_user_data; - char *isrc = p_env->tocent[i_track-p_env->gen.i_first_track].isrc; + const track_info_t *p_tocent = get_tocent_image(p_user_data, i_track); + char *isrc = p_tocent ? p_tocent->isrc : NULL; if (isrc && isrc[0]) { return strdup(isrc); diff --git a/test/driver/bincue.c b/test/driver/bincue.c index 46a80c62..a6535597 100644 --- a/test/driver/bincue.c +++ b/test/driver/bincue.c @@ -312,6 +312,161 @@ check_read_past_end(void) return ret; } +/* Write a cue sheet whose FILE is a 4-sector BIN file of zeros, followed + by psz_body, and open it. Both files are in the current directory and + are removed again; the BIN file only once p_cdio is destroyed, which + remove_bin_cue() does. */ +static const char *bin_cue_bin = "bincue-tracks.bin"; + +static CdIo_t * +open_bin_cue(const char *psz_body) +{ + const char *cue_name = "bincue-tracks.cue"; + FILE *cue, *bin; + CdIo_t *p_cdio; + long i; + + bin = fopen(bin_cue_bin, "wb"); + if (!bin) { + printf("Can't create %s\n", bin_cue_bin); + return NULL; + } + for (i = 0; i < 4 * CDIO_CD_FRAMESIZE_RAW; i++) + fputc(0, bin); + fclose(bin); + cue = fopen(cue_name, "w"); + if (!cue) { + printf("Can't create %s\n", cue_name); + return NULL; + } + fprintf(cue, "FILE \"%s\" BINARY\n%s", bin_cue_bin, psz_body); + fclose(cue); + + p_cdio = cdio_open_bincue(cue_name); + remove(cue_name); + return p_cdio; +} + +static void +remove_bin_cue(CdIo_t *p_cdio) +{ + if (p_cdio) + cdio_destroy(p_cdio); + remove(bin_cue_bin); +} + +/* The track getters must reject a track number the image doesn't have, + also below the first track: they used to index the table of tracks + with it */ +static int +check_track_range(void) +{ + static const track_t bad[] = { 1, 98, 100 }; + CdIo_t *p_cdio = open_bin_cue(" TRACK 99 AUDIO\n INDEX 01 00:00:00\n"); + unsigned int i; + int ret = 0; + + if (!p_cdio) { + printf("Can't open a cue sheet whose only track is 99\n"); + remove_bin_cue(NULL); + return 1; + } + if (CDIO_TRACK_FLAG_ERROR == cdio_get_track_copy_permit(p_cdio, 99) + || -1 == cdio_get_track_channels(p_cdio, 99)) { + printf("Incorrect: track 99 is rejected.\n"); + ret = 1; + } + for (i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) { + char *isrc = cdio_get_track_isrc(p_cdio, bad[i]); + + if (isrc + || -1 != cdio_get_track_channels(p_cdio, bad[i]) + || CDIO_TRACK_FLAG_ERROR != cdio_get_track_copy_permit(p_cdio, bad[i]) + || CDIO_TRACK_FLAG_ERROR != cdio_get_track_preemphasis(p_cdio, bad[i]) + || CDIO_INVALID_LBA != cdio_get_track_pregap_lba(p_cdio, bad[i])) { + printf("Incorrect: track %u of a disc with only track 99 accepted.\n", + (unsigned int) bad[i]); + ret = 1; + } + free(isrc); + } + remove_bin_cue(p_cdio); + return ret; +} + +/* Track numbers must follow each other. A first track of 99 followed by + 2 and 3 used to give a disc whose last track is 101 */ +static int +check_track_sequence(void) +{ + static const char *const bad[] = { + " TRACK 99 AUDIO\n INDEX 01 00:00:00\n" + " TRACK 02 AUDIO\n INDEX 01 00:00:01\n" + " TRACK 03 AUDIO\n INDEX 01 00:00:02\n", + " TRACK 01 AUDIO\n INDEX 01 00:00:00\n" + " TRACK 03 AUDIO\n INDEX 01 00:00:01\n", + " TRACK 02 AUDIO\n INDEX 01 00:00:00\n" + " TRACK 01 AUDIO\n INDEX 01 00:00:01\n", + }; + unsigned int i; + int ret = 0; + CdIo_t *p_cdio = open_bin_cue(" TRACK 98 AUDIO\n INDEX 01 00:00:00\n" + " TRACK 99 AUDIO\n INDEX 01 00:00:01\n"); + + if (!p_cdio || 99 != cdio_get_last_track_num(p_cdio)) { + printf("Incorrect: tracks 98 and 99 don't open.\n"); + ret = 1; + } + remove_bin_cue(p_cdio); + for (i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) { + p_cdio = open_bin_cue(bad[i]); + if (p_cdio) { + printf("Incorrect: tracks out of sequence opened (case %u).\n", i); + ret = 1; + } + remove_bin_cue(p_cdio); + } + return ret; +} + +/* A keyword given twice replaces the first value. The first value + used to be leaked, which only a memory checker sees */ +static int +check_repeated_keywords(void) +{ + CdIo_t *p_cdio = open_bin_cue("CATALOG 0000000000001\n" + "CATALOG 0000000000002\n" + "FILE \"bincue-tracks.bin\" BINARY\n" + " TRACK 01 AUDIO\n" + " ISRC USAAA0000001\n" + " ISRC USAAA0000002\n" + " INDEX 01 00:00:00\n"); + char *psz; + int ret = 0; + + if (!p_cdio) { + printf("Incorrect: repeated CATALOG, FILE and ISRC don't open.\n"); + remove_bin_cue(NULL); + return 1; + } + psz = cdio_get_track_isrc(p_cdio, 1); + if (!psz || strcmp(psz, "USAAA0000002")) { + printf("Incorrect: ISRC is %s, not the last one given.\n", + psz ? psz : "(none)"); + ret = 1; + } + free(psz); + psz = cdio_get_mcn(p_cdio); + if (!psz || strcmp(psz, "0000000000002")) { + printf("Incorrect: CATALOG is %s, not the last one given.\n", + psz ? psz : "(none)"); + ret = 1; + } + free(psz); + remove_bin_cue(p_cdio); + return ret; +} + static int check_mode2_seek(const char *cue_name, const char *mode) { @@ -375,6 +530,9 @@ main(int argc, const char *argv[]) ret = 1; if (check_read_past_end()) ret = 1; + if (check_track_range() || check_track_sequence() + || check_repeated_keywords()) + ret = 1; if (check_mode2_seek("mode2-2048.cue", "MODE2/2048") || check_mode2_seek("mode2-2324.cue", "MODE2/2324")) ret = 1; diff --git a/test/driver/cdrdao.c b/test/driver/cdrdao.c index 709e8757..51bd538d 100644 --- a/test/driver/cdrdao.c +++ b/test/driver/cdrdao.c @@ -757,6 +757,86 @@ check_read_past_end(void) return ret; } +/* Write psz_body to a TOC file next to a 4-sector data file and open + it. Both files are removed again. */ +static CdIo_t * +open_toc(const char *psz_body) +{ + const char *toc_name = "cdrdao-keywords.toc"; + FILE *toc = fopen(toc_name, "w"); + CdIo_t *p_cdio; + + if (!toc) { + fprintf(stderr, "Unable to create %s\n", toc_name); + return NULL; + } + fprintf(toc, "%s", psz_body); + fclose(toc); + p_cdio = cdio_open_cdrdao(toc_name); + remove(toc_name); + return p_cdio; +} + +/* A keyword given twice replaces the first value, and a TOC file that + fails after its first track is rejected. The first value, and the + tracks read before the error, used to be leaked, which only a memory + checker sees */ +static int +check_repeated_keywords(void) +{ + const char *bin_name = "cdrdao-keywords.bin"; + CdIo_t *p_cdio; + char *psz; + int ret = 0; + + if (write_zeros(bin_name, 4 * CDIO_CD_FRAMESIZE_RAW)) + return 1; + p_cdio = open_toc("CD_DA\n" + "CATALOG \"0000000000001\"\n" + "CATALOG \"0000000000002\"\n" + "TRACK AUDIO\n" + "ISRC \"USAAA0000001\"\n" + "ISRC \"USAAA0000002\"\n" + "FILE \"cdrdao-keywords.bin\" 0\n" + "DATAFILE \"cdrdao-keywords.bin\"\n"); + if (!p_cdio) { + fprintf(stderr, + "Incorrect: repeated CATALOG, FILE and ISRC don't open.\n"); + remove(bin_name); + return 1; + } + psz = cdio_get_track_isrc(p_cdio, 1); + if (!psz || strcmp(psz, "USAAA0000002")) { + fprintf(stderr, "Incorrect: ISRC is %s, not the last one given.\n", + psz ? psz : "(none)"); + ret = 1; + } + free(psz); + psz = cdio_get_mcn(p_cdio); + if (!psz || strcmp(psz, "0000000000002")) { + fprintf(stderr, "Incorrect: CATALOG is %s, not the last one given.\n", + psz ? psz : "(none)"); + ret = 1; + } + free(psz); + cdio_destroy(p_cdio); + + /* The second track is longer than the file */ + p_cdio = open_toc("CD_DA\n" + "TRACK AUDIO\n" + "ISRC \"USAAA0000001\"\n" + "FILE \"cdrdao-keywords.bin\" 0\n" + "TRACK AUDIO\n" + "FILE \"cdrdao-keywords.bin\" 0 50:00:00\n"); + if (p_cdio) { + fprintf(stderr, "Incorrect: a track longer than its file opened.\n"); + cdio_destroy(p_cdio); + ret = 1; + } + remove(bin_name); + return ret; +} + int main(int argc, const char *argv[]) { @@ -817,6 +897,8 @@ main(int argc, const char *argv[]) ret = 1; if (check_bad_datafile_length() || check_read_past_end()) ret = 1; + if (check_repeated_keywords()) + ret = 1; #ifdef HAVE_CHDIR if (0 == chdir(DATA_DIR)) #endif