From: Mikko Rasa Date: Thu, 10 Apr 2014 19:02:39 +0000 (+0300) Subject: Make sure TrackOffsetIters make sense X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=30c14e235f19442753e5eeeb8a86cbc22960afb1;p=r2c2.git Make sure TrackOffsetIters make sense Disallow creation of iterators that have a path not connected to the entry endpoint, or an out of range offset. --- diff --git a/source/libr2c2/trackoffsetiter.cpp b/source/libr2c2/trackoffsetiter.cpp index f8e8a2d..9676985 100644 --- a/source/libr2c2/trackoffsetiter.cpp +++ b/source/libr2c2/trackoffsetiter.cpp @@ -24,25 +24,50 @@ TrackOffsetIter::TrackOffsetIter(Track *t, unsigned e, float o): _track(t, e), _offset(_track ? o : 0), _path((_track && _offset) ? _track->get_active_path() : -1) -{ } +{ + validate(); +} TrackOffsetIter::TrackOffsetIter(Track *t, unsigned e, unsigned p, float o): _track(t, e), _offset(o), _path(p) -{ } +{ + validate(); +} TrackOffsetIter::TrackOffsetIter(const TrackIter &t, float o): _track(t), _offset(_track ? o : 0), _path((_track && _offset) ? _track->get_active_path() : -1) -{ } +{ + validate(); +} TrackOffsetIter::TrackOffsetIter(const TrackIter &t, unsigned p, float o): _track(t), _offset(o), _path(p) -{ } +{ + validate(); +} + +void TrackOffsetIter::validate() +{ + if(!_track) + return; + + if(_offset<0) + throw invalid_argument("TrackOffsetIter::TrackOffsetIter"); + + if(_path>=0) + { + if(!_track.endpoint().has_path(_path)) + throw invalid_argument("TrackOffsetIter::TrackOffsetIter"); + if(_offset>_track->get_type().get_path_length(_path)) + throw invalid_argument("TrackOffsetIter::TrackOffsetIter"); + } +} BlockIter TrackOffsetIter::block_iter() const { diff --git a/source/libr2c2/trackoffsetiter.h b/source/libr2c2/trackoffsetiter.h index 776f7a2..3156c08 100644 --- a/source/libr2c2/trackoffsetiter.h +++ b/source/libr2c2/trackoffsetiter.h @@ -29,6 +29,10 @@ public: TrackOffsetIter(const TrackIter &, float = 0); TrackOffsetIter(const TrackIter &, unsigned, float); +private: + void validate(); + +public: Track *track() const { return _track.track(); } unsigned entry() const { return _track.entry(); } const TrackIter &track_iter() const { return _track; }