From 336f5fa9f62e8e13c7fad1bb745fa57e28bf535a Mon Sep 17 00:00:00 2001
From: stefan <stefan@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Fri, 7 May 2010 15:19:32 +0000
Subject: [PATCH] Fixed, alignment and sequence impls (added more bounds
 checks)

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2203 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/seq/base/src/alignment_handle.cc      | 23 ++++++++++++-------
 modules/seq/base/src/alignment_handle.hh      |  3 +--
 modules/seq/base/src/impl/sequence_impl.cc    |  3 +++
 .../seq/base/src/impl/sequence_list_impl.hh   | 17 ++++++++++----
 4 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/modules/seq/base/src/alignment_handle.cc b/modules/seq/base/src/alignment_handle.cc
index ee21b144c..3ad612da9 100644
--- a/modules/seq/base/src/alignment_handle.cc
+++ b/modules/seq/base/src/alignment_handle.cc
@@ -62,9 +62,9 @@ int AlignmentHandle::GetResidueIndex(int seq_index, int pos) const
 void AlignmentHandle::AddSequence(const ConstSequenceHandle& sequence)
 {
   this->CheckValidity();
-  if (impl_->GetCount()>0 &&
-      impl_->GetSequence(0)->GetLength()!=sequence.GetLength()) {
-    throw InvalidAlignment();
+  if (!sequence.IsValid() || (impl_->GetCount()>0 &&
+      impl_->GetSequence(0)->GetLength()!=sequence.GetLength())) {
+    throw InvalidSequence();
   }
   return impl_->AddSequence(sequence.Impl());
 }
@@ -124,7 +124,7 @@ int AlignmentHandle::GetCount() const
 
 AlignmentHandle AlignmentFromSequenceList(const SequenceList& seq_list)
 {
-  if (seq_list.SequencesHaveEqualLength()) {
+  if (seq_list.IsValid() && seq_list.SequencesHaveEqualLength()) {
     return AlignmentHandle(seq_list.Impl());
   }
   throw InvalidAlignment();
@@ -188,10 +188,14 @@ void AlignmentHandle::ShiftRegion(int start, int end, int amount,
 {
   this->CheckValidity();
   int cnt=0;
-  for (impl::SequenceListImpl::Iterator i=impl_->Begin(),
-       e=impl_->End(); i!=e; ++i, ++cnt) {
-    if (master==-1 || cnt==master) {
-      (*i)->ShiftRegion(start, end, amount);
+  if(master!=-1){
+    impl::SequenceImplPtr handle = this->GetSequence(master).Impl();
+    handle->ShiftRegion(start, end, amount);
+  }
+  else{
+    for (impl::SequenceListImpl::Iterator i=impl_->Begin(),
+         e=impl_->End(); i!=e; ++i, ++cnt) {
+        (*i)->ShiftRegion(start, end, amount);
     }
   }
 }
@@ -199,6 +203,9 @@ void AlignmentHandle::ShiftRegion(int start, int end, int amount,
 AlignedRegion AlignmentHandle::MakeRegion(int start, int n, int master) const
 {
   this->CheckValidity();
+  if(start<0 || n < 0 || start >= n || start + n >= this->GetLength()){
+    throw std::out_of_range("Region not valid");
+  }
   return AlignedRegion(*this, start, start+n, master);
 }
 
diff --git a/modules/seq/base/src/alignment_handle.hh b/modules/seq/base/src/alignment_handle.hh
index e007b4fb3..998618b52 100644
--- a/modules/seq/base/src/alignment_handle.hh
+++ b/modules/seq/base/src/alignment_handle.hh
@@ -121,8 +121,7 @@ public:
   ///      If set to -1, no master sequence is defined and the operations will
   ///      affect all sequences
   ///
-  /// This method does not throw any exceptions, even if the aligned region is
-  /// out of bounds.
+  /// If the aligned region is out of bounds, a std::out_of_bounds exeception will be thrown.
   AlignedRegion MakeRegion(int start, int n, int master=-1) const;
 
   /// \brief get number of sequences in alignment
diff --git a/modules/seq/base/src/impl/sequence_impl.cc b/modules/seq/base/src/impl/sequence_impl.cc
index 070211924..5ce66685e 100644
--- a/modules/seq/base/src/impl/sequence_impl.cc
+++ b/modules/seq/base/src/impl/sequence_impl.cc
@@ -280,6 +280,9 @@ void SequenceImpl::Replace(const String& str,int start, int end)
 
 void SequenceImpl::ShiftRegion(int start, int end, int amount)
 {
+  if(start <= end && end + amount > this->GetLength() || start + amount <= 0){
+    throw std::out_of_range("Region not valid");
+  }
   String str1=seq_string_.substr(start, end-start);
   if (amount<0) {
     String str2=seq_string_.substr(start+amount, -amount);
diff --git a/modules/seq/base/src/impl/sequence_list_impl.hh b/modules/seq/base/src/impl/sequence_list_impl.hh
index 0911aa9b8..93bc07035 100644
--- a/modules/seq/base/src/impl/sequence_list_impl.hh
+++ b/modules/seq/base/src/impl/sequence_list_impl.hh
@@ -41,12 +41,21 @@ public:
   /// \brief get number of sequences in list
   int GetCount() const { return list_.size(); }
   
-  SequenceImplPtr& GetSequence(int i) 
-  { 
-    return list_[i]; 
+  SequenceImplPtr& GetSequence(int i) {
+    unsigned int index = static_cast<unsigned int>(i);
+    if (index<list_.size()) {
+      return list_[index];
+    }
+    throw Error("Index not covered SequenceList");
   }
   
-  const SequenceImplPtr& GetSequence(int i) const { return list_[i]; }  
+  const SequenceImplPtr& GetSequence(unsigned int i) const {
+    unsigned int index = static_cast<unsigned int>(i);
+    if (index<list_.size()) {
+      return list_[index];
+    }
+    throw Error("Index not covered SequenceList");
+  }
   
   int GetPos(int seq_index, int residue_index) const;
 
-- 
GitLab