diff --git a/include/cdio/sector.h b/include/cdio/sector.h index ea29997d..238012f1 100644 --- a/include/cdio/sector.h +++ b/include/cdio/sector.h @@ -265,7 +265,9 @@ typedef enum cdio_cd_minutes_sectors /** Convert a string of the form MM:SS:FF into the corresponding LBA. - CDIO_INVALID_LBA is returned if there is an error. + SS is below 60 and FF below 75, and both have one or two digits. + MM can have any number of digits, as long as the LBA fits in an + lba_t. CDIO_INVALID_LBA is returned if there is an error. */ lba_t cdio_mmssff_to_lba (const char *psz_mmssff); diff --git a/lib/driver/image/bincue.c b/lib/driver/image/bincue.c index da39384b..79749d63 100644 --- a/lib/driver/image/bincue.c +++ b/lib/driver/image/bincue.c @@ -208,7 +208,10 @@ _read_bincue (void *p_user_data, void *data, size_t size) while (size > 0) { long int rem = (long int) (this_track->datasize - p_env->pos.buff_offset); - if ((long int) size <= rem) { + /* Nothing left of this sector to read. */ + if (rem <= 0) + break; + if (size <= (size_t) rem) { this_size = cdio_stream_read(p_env->gen.data_source, buf, size, 1); final_size += this_size; memcpy (p, buf, this_size); @@ -233,8 +236,13 @@ _read_bincue (void *p_user_data, void *data, size_t size) p_env->pos.buff_offset=0; p_env->pos.lba++; - /* Have gone into next track. */ - if (p_env->pos.lba >= p_env->tocent[p_env->pos.index+1].start_lba) { + /* Have gone into next track. pos.lba counts sectors from the + start of the image, from 0, while start_lba includes the + pregap. The entry after the last track's is the lead-out, which + holds no data: stay in the last track. */ + if (p_env->pos.index + 1 < p_env->gen.i_tracks + && cdio_lsn_to_lba(p_env->pos.lba) + >= p_env->tocent[p_env->pos.index+1].start_lba) { p_env->pos.index++; this_track=&(p_env->tocent[p_env->pos.index]); skip_size = this_track->datastart + this_track->endsize; diff --git a/lib/driver/image/cdrdao.c b/lib/driver/image/cdrdao.c index f4d9d59f..ad830746 100644 --- a/lib/driver/image/cdrdao.c +++ b/lib/driver/image/cdrdao.c @@ -204,7 +204,10 @@ _read_cdrdao (void *user_data, void *data, size_t size) while (size > 0) { int rem = (int) (this_track->datasize - env->pos.buff_offset); - if (size <= rem) { + /* Nothing left of this sector to read. */ + if (rem <= 0) + break; + if (size <= (size_t) rem) { this_size = cdio_stream_read(this_track->data_source, buf, size, 1); final_size += this_size; memcpy (p, buf, this_size); @@ -229,8 +232,13 @@ _read_cdrdao (void *user_data, void *data, size_t size) env->pos.buff_offset=0; env->pos.lba++; - /* Have gone into next track. */ - if (env->pos.lba >= env->tocent[env->pos.index+1].start_lba) { + /* Have gone into next track. pos.lba counts sectors from the + start of the image, from 0, while start_lba includes the + pregap. The entry after the last track's is the lead-out, which + holds no data: stay in the last track. */ + if (env->pos.index + 1 < env->gen.i_tracks + && cdio_lsn_to_lba(env->pos.lba) + >= env->tocent[env->pos.index+1].start_lba) { env->pos.index++; this_track=&(env->tocent[env->pos.index]); skip_size = this_track->datastart + this_track->endsize; @@ -792,7 +800,7 @@ parse_tocfile (_img_private_t *cd, const char *psz_cue_name) if (cd) { off_t i_size = cdio_stream_stat(cd->tocent[i_track].data_source); if (lba) { - if ( (lba * cd->tocent[i_track].datasize) > i_size) { + if ( ((off_t) lba * cd->tocent[i_track].datasize) > i_size) { cdio_log(log_level, "%s line %d: MSF length %s exceeds end of file", psz_cue_name, i_line, psz_field); diff --git a/lib/driver/sector.c b/lib/driver/sector.c index 12f7cd95..106a6842 100644 --- a/lib/driver/sector.c +++ b/lib/driver/sector.c @@ -184,8 +184,16 @@ cdio_msf3_to_lba (unsigned int minutes, unsigned int seconds, + frames); } +/* The most minutes whose LBA, with 59 seconds and 74 frames, fits in + an lba_t */ +#define MAX_MMSSFF_MINUTES \ + ((INT32_MAX - (CDIO_CD_SECS_PER_MIN * CDIO_CD_FRAMES_PER_SEC - 1)) \ + / (CDIO_CD_SECS_PER_MIN * CDIO_CD_FRAMES_PER_SEC)) + /** Convert a string of the form MM:SS:FF into the corresponding LBA. + SS is below 60 and FF below 75, and both have one or two digits. MM + can have any number of digits, as long as the LBA fits in an lba_t. CDIO_INVALID_LBA is returned if there is an error. */ lba_t @@ -208,6 +216,12 @@ cdio_mmssff_to_lba (const char *psz_mmssff) psz_field = psz_field * 10 + (c - '0'); else return CDIO_INVALID_LBA; + /* Any number of minutes is accepted, since the string can be an + offset into a file longer than a CD, as long as the LBA fits in + an lba_t. Checking here also keeps a long string of digits from + overflowing. */ + if (psz_field > MAX_MMSSFF_MINUTES) + return CDIO_INVALID_LBA; } ret = cdio_msf3_to_lba (psz_field, 0, 0); @@ -234,12 +248,12 @@ cdio_mmssff_to_lba (const char *psz_mmssff) ret += cdio_msf3_to_lba (0, psz_field, 0); c = *psz_mmssff++; - if (isdigit(c)) + if(c >= '0' && c <= '9') psz_field = (c - '0'); else - return -1; + return CDIO_INVALID_LBA; if('\0' != (c = *psz_mmssff++)) { - if (isdigit(c)) { + if(c >= '0' && c <= '9') { psz_field = psz_field * 10 + (c - '0'); c = *psz_mmssff++; } diff --git a/test/driver/bincue.c b/test/driver/bincue.c index 7c2a3641..46a80c62 100644 --- a/test/driver/bincue.c +++ b/test/driver/bincue.c @@ -213,6 +213,105 @@ check_no_tracks(void) return 0; } +/* An INDEX or PREGAP given an MSF string out of range or that isn't + one must be rejected, and the same cue sheet with a valid one + accepted. Frames that weren't digits used to be read as -1 */ +static int +check_msf_cue(const char *psz_line, const char *psz_msf, bool b_valid) +{ + const char *cue_name = "bincue-bad-msf.cue"; + const char *bin_name = "bincue-bad-msf.bin"; + FILE *cue, *bin; + CdIo_t *p_cdio; + long i; + int ret = 0; + + /* The BIN file is named as cue sheets name it, next to the cue + sheet: an absolute DATA_DIR may start with a drive letter */ + bin = fopen(bin_name, "wb"); + if (!bin) { + printf("Can't create %s\n", bin_name); + return 1; + } + 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); + remove(bin_name); + return 1; + } + fprintf(cue, "FILE \"%s\" BINARY\n TRACK 01 AUDIO\n", bin_name); + if (0 == strcmp(psz_line, "INDEX")) + fprintf(cue, " INDEX 01 %s\n", psz_msf); + else + fprintf(cue, " PREGAP %s\n INDEX 01 00:00:00\n", psz_msf); + fclose(cue); + + p_cdio = cdio_open_bincue(cue_name); + remove(cue_name); + if (b_valid != (NULL != p_cdio)) { + printf("Incorrect: %s %s %s.\n", psz_line, psz_msf, + b_valid ? "doesn't open" : "opened"); + ret = 1; + } + if (p_cdio) + cdio_destroy(p_cdio); + remove(bin_name); + return ret; +} + +static int +check_bad_msf(const char *psz_line) +{ + static const char *const bad[] = { + "00:00:x", "477218:00:00", "954437:10:43", "00:60:00", "00:00:75", + }; + unsigned int i; + int ret = check_msf_cue(psz_line, "00:00:00", true); + + for (i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) + if (check_msf_cue(psz_line, bad[i], false)) + ret = 1; + return ret; +} + +/* cdio_read() past the end of the image stops in the last track: it + used to go on into the lead-out entry, which has no data, and from + there past the end of tocent[]. Only a memory checker sees that */ +static int +check_read_past_end(void) +{ + const size_t i_size = 710304; /* cdda.bin */ + char *buf = malloc(2 * i_size); + CdIo_t *p_cdio = cdio_open_bincue(DATA_DIR "/cdda.cue"); + ssize_t i_read; + int ret = 0; + + if (!buf || !p_cdio) { + printf("Can't open cdda.cue\n"); + free(buf); + if (p_cdio) + cdio_destroy(p_cdio); + return 1; + } + if (cdio_lseek(p_cdio, 0, SEEK_SET) < 0) { + printf("Can't seek in cdda.cue\n"); + ret = 1; + } else { + i_read = cdio_read(p_cdio, buf, 2 * i_size); + if (i_read < 0 || (size_t) i_read > i_size) { + printf("Incorrect: read %ld bytes past the end of cdda.cue\n", + (long) i_read); + ret = 1; + } + } + cdio_destroy(p_cdio); + free(buf); + return ret; +} + static int check_mode2_seek(const char *cue_name, const char *mode) { @@ -272,6 +371,10 @@ main(int argc, const char *argv[]) ret = 1; if (check_read_audio_sectors_sub()) ret = 1; + if (check_bad_msf("INDEX") || check_bad_msf("PREGAP")) + ret = 1; + if (check_read_past_end()) + 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 6547167e..709e8757 100644 --- a/test/driver/cdrdao.c +++ b/test/driver/cdrdao.c @@ -625,15 +625,135 @@ check_msf_toc(const char *psz_keyword, const char *psz_msf, bool b_valid) return ret; } +/* MSF strings that must be rejected */ +static const char *const bad_msf[] = { + "xx", + "00:00:x", /* frames not a digit */ + "00:x:00", + "x:00:00", + "477218:00:00", /* the LBA doesn't fit in an lba_t */ + "954437:10:43", /* used to wrap around to a negative LBA */ + "99999999999999999999:00:00", + "00:60:00", + "00:00:75", + "00:00:000", + "00:000:00", + "00:00", + "", +}; + /* A track keyword given an invalid MSF string must be rejected, and the same TOC with a valid one accepted */ static int check_bad_msf(const char *psz_keyword) { int ret = check_msf_toc(psz_keyword, "00:00:00", true); + unsigned int i; - if (check_msf_toc(psz_keyword, "xx", false)) + /* More minutes than a CD holds, as an offset into a long file can + have, and the largest MSF whose LBA fits in an lba_t */ + if (check_msf_toc(psz_keyword, "180:00:00", true) + || check_msf_toc(psz_keyword, "477217:59:74", true)) ret = 1; + + for (i = 0; i < sizeof(bad_msf) / sizeof(bad_msf[0]); i++) + if (bad_msf[i][0] && check_msf_toc(psz_keyword, bad_msf[i], false)) + ret = 1; + return ret; +} + +/* The length of a DATAFILE given as an invalid MSF string must be + rejected. A length whose frames aren't digits used to be accepted as + -1 sectors */ +static int +check_bad_datafile_length(void) +{ + const char *toc_name = "cdrdao-bad-length.toc"; + const char *bin_name = "cdrdao-bad-length.bin"; + unsigned int i; + int ret = 0; + + if (write_zeros(bin_name, 4 * CDIO_CD_FRAMESIZE_RAW)) + return 1; + for (i = 0; i < sizeof(bad_msf) / sizeof(bad_msf[0]); i++) { + FILE *toc; + CdIo_t *p_cdio; + + if (!bad_msf[i][0]) + continue; + toc = fopen(toc_name, "w"); + if (!toc) { + fprintf(stderr, "Unable to create %s\n", toc_name); + ret = 1; + break; + } + fprintf(toc, "CD_DA\nTRACK AUDIO\nDATAFILE \"%s\" %s\n" + "TRACK AUDIO\nDATAFILE \"%s\"\n", bin_name, bad_msf[i], bin_name); + fclose(toc); + p_cdio = cdio_open_cdrdao(toc_name); + remove(toc_name); + if (p_cdio) { + fprintf(stderr, "Incorrect: DATAFILE length %s opened.\n", bad_msf[i]); + cdio_destroy(p_cdio); + ret = 1; + } + } + remove(bin_name); + return ret; +} + +/* cdio_read() past the end of the image stops in the last track: it + used to go on into the lead-out entry, which has no data, and from + there past the end of tocent[]. Only a memory checker sees that */ +static int +check_read_past_end(void) +{ + const char *toc_name = "cdrdao-read-past-end.toc"; + const char *bin_name = "cdrdao-read-past-end.bin"; + const size_t i_size = 4 * CDIO_CD_FRAMESIZE_RAW; + char *buf; + FILE *toc; + CdIo_t *p_cdio; + ssize_t i_read; + int ret = 0; + + if (write_zeros(bin_name, i_size)) + return 1; + buf = malloc(2 * i_size); + toc = fopen(toc_name, "w"); + if (!buf || !toc) { + fprintf(stderr, "Unable to create %s\n", toc_name); + free(buf); + if (toc) + fclose(toc); + remove(bin_name); + return 1; + } + fprintf(toc, "CD_DA\nTRACK AUDIO\nFILE \"%s\" 0\n", bin_name); + fclose(toc); + + p_cdio = cdio_open_cdrdao(toc_name); + remove(toc_name); + if (!p_cdio) { + fprintf(stderr, "Can't open %s\n", toc_name); + free(buf); + remove(bin_name); + return 1; + } + if (cdio_lseek(p_cdio, 0, SEEK_SET) < 0) { + fprintf(stderr, "Can't seek in %s\n", toc_name); + ret = 1; + } else { + i_read = cdio_read(p_cdio, buf, 2 * i_size); + if (i_read < 0 || (size_t) i_read > i_size) { + fprintf(stderr, "Incorrect: read %ld bytes past the end of %s\n", + (long) i_read, toc_name); + ret = 1; + } + } + cdio_destroy(p_cdio); + free(buf); + remove(bin_name); return ret; } @@ -695,6 +815,8 @@ main(int argc, const char *argv[]) if (check_bad_msf("START") || check_bad_msf("PREGAP") || check_bad_msf("SILENCE")) ret = 1; + if (check_bad_datafile_length() || check_read_past_end()) + ret = 1; #ifdef HAVE_CHDIR if (0 == chdir(DATA_DIR)) #endif