Skip to content

Validate MSF strings strictly, and stop cdio_read() at the last track - #121

Merged
rocky merged 4 commits into
libcdio:mainfrom
nicosp:strict-msf
Nov 17, 2026
Merged

rocky merged 4 commits into
libcdio:mainfrom
nicosp:strict-msf

Conversation

@nicosp

@nicosp nicosp commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Makes MSF strings in cdrdao TOC files and CUE sheets fully validated, and stops cdio_read() on BIN/CUE and cdrdao images at the last track.

What went wrong

  • cdio_mmssff_to_lba() returned -1 instead of CDIO_INVALID_LBA when the frames field wasn't a digit, as in 00:00:x. Its callers only compare the result with CDIO_INVALID_LBA, so they accepted such a string as -1 sectors. That covers:
    • in a cdrdao TOC: a FILE or DATAFILE length, START, PREGAP and SILENCE (71035b8 meant to reject invalid MSF strings for the last three);
    • in a CUE sheet: INDEX and PREGAP.
  • The minutes had no limit. A large value such as 954437:10:43 wrapped around to a negative LBA, and a longer string of digits overflowed an int.
  • cdrdao's length check could overflow. It multiplied a FILE or DATAFILE length by the sector size as an int before comparing it with the file's size.
  • cdio_read() went past the last track. _read_bincue() and _read_cdrdao() moved to the next tocent[] entry without checking that there was one. Past the last track they moved into the lead-out entry, whose data size is 0, so the request never shrank and they went on past the end of tocent[]. They also compared the position, which counts from 0, with a start LBA that includes the 150-sector pregap, so they changed track 150 sectors early.

Changes

  • cdio_mmssff_to_lba() returns CDIO_INVALID_LBA for every error. It rejects minutes whose LBA doesn't fit in an lba_t, which also stops the digits from overflowing.
  • Minutes above 99 are still accepted. In a TOC file they can be an offset into a file longer than a CD, and test/data/t3.toc relies on 180:0:0 parsing.
  • The cdrdao length check is done in off_t.
  • cdio_read() stays in the last track, compares LBA with LBA, and stops when nothing is left of the current sector.

Tests

  • cdrdao test: START, PREGAP and SILENCE are given MSF strings with non-digit fields, out-of-range seconds or frames, too many digits, or an LBA that doesn't fit in an lba_t. The same strings are also given as a DATAFILE length followed by another track. All must be rejected, while 180:00:00 and 477217:59:74 are still accepted.
  • bincue test: the same check for INDEX and PREGAP.
  • Both tests also read twice the size of the image with cdio_read() after seeking to its start. That part only shows a difference under a memory checker.

make -C test check and make -C test/driver check pass, and the bincue and cdrdao driver tests are clean under valgrind. Without the library changes, the new MSF checks fail.

🤖 Generated with Claude Code

nicosp and others added 3 commits November 17, 2026 09:11
cdio_mmssff_to_lba() returned -1 instead of CDIO_INVALID_LBA when the
frames field wasn't a digit, as in "00:00:x". Its callers only compare
the result with CDIO_INVALID_LBA, so they accepted such a string as -1
sectors: as a FILE or DATAFILE length or START, PREGAP and SILENCE in a
cdrdao TOC (71035b8 meant to reject invalid MSF strings there), and
as INDEX and PREGAP in a CUE sheet.

The minutes had no limit either, so a large value such as
"954437:10:43" wrapped around to a negative LBA, and a longer string
of digits overflowed an int.

Return CDIO_INVALID_LBA for every error, and reject minutes whose LBA
doesn't fit in an lba_t, which also stops the digits from overflowing.
Minutes above 99 are still accepted: in a TOC file they can be an
offset into a file longer than a CD, as test/data/t3.toc has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The length of a FILE or DATAFILE, in sectors, was multiplied by the
sector size as an int before being compared with the size of the file.
A length of more than about 900000 sectors, which an MSF string can
now still give, overflowed it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
_read_bincue() and _read_cdrdao() moved on to the next entry of
tocent[] whenever the position reached its start, without checking
that there was a next track. Past the last track they moved into the
lead-out entry, which has a data size of 0, so the rest of the request
never shrank and the position went on past the end of tocent[]. A
cdio_read() of more than the image after cdio_lseek() read beyond the
driver's data, and could take a sector size from there.

The test for the next track also compared the position, which counts
sectors from 0, with the track's start LBA, which includes the 150
sectors of the pregap, so the read moved on to the next track 150
sectors early.

Stay in the last track, compare LBA with LBA, and stop when nothing is
left of the current sector.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nicosp
nicosp requested a review from rocky November 17, 2026 07:14
The cdrdao test now gives START, PREGAP and SILENCE MSF strings whose
frames or seconds aren't digits, whose seconds or frames are out of
range, with too many digits, or whose LBA doesn't fit in an lba_t, and
gives them as the length of a DATAFILE followed by another track. All
must be rejected, while 180:00:00 and 477217:59:74 are still accepted.
The bincue test does the same for INDEX and PREGAP.

Both tests also read twice the size of the image with cdio_read()
after seeking to its start. That only shows a difference under a
memory checker such as valgrind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread lib/driver/sector.c
if('\0' != (c = *psz_mmssff++)) {
if (isdigit(c)) {
if(c >= '0' && c <= '9') {
psz_field = psz_field * 10 + (c - '0');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another bug in the existing code, noticed in looking at this PR:

psz_field is an int, so a long string like 2147483648:00:00 can overflow psz_field * 10 + (c - '0').

Do you want to fix this in this PR, or leave it for another PR?

@nicosp nicosp Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already covered by the previous checks. psz_field is capped at MAX_MMSSFF_MINUTES (477217) so it will never overflow at that point.
The largest possible value is: "477217:59:74"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. My mistake. Thanks for checking.

@rocky

rocky commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

@Nicsp Thanks for the fixes, as before. LGTM.
Decide whether we want to address the additional problem uncovered by this PR. If not, then feel free to merge.

@nicosp
nicosp requested a review from rocky November 17, 2026 12:10
@rocky
rocky merged commit c74bb9a into libcdio:main Nov 17, 2026
9 checks passed
@nicosp
nicosp deleted the strict-msf branch November 19, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants