From a104ae530b4da99fe37632714e9c29db95e923d1 Mon Sep 17 00:00:00 2001
From: marco <marco@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Fri, 16 Jul 2010 07:26:12 +0000
Subject: [PATCH] readded boundary checks to Vec{2,3,4}::operator[]

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2572 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/geom/src/vec2.hh | 8 +++++++-
 modules/geom/src/vec3.hh | 8 +++++++-
 modules/geom/src/vec4.hh | 8 +++++++-
 3 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/modules/geom/src/vec2.hh b/modules/geom/src/vec2.hh
index 391e073ff..8432856ea 100644
--- a/modules/geom/src/vec2.hh
+++ b/modules/geom/src/vec2.hh
@@ -19,6 +19,7 @@
 #ifndef GEOM_VEC2_H
 #define GEOM_VEC2_H
 
+#include <stdexcept>
 #include <cstddef> // for size_t
 #include <ostream>
 #include <vector>
@@ -82,13 +83,18 @@ public:
   //! element access
   Real& operator[](std::size_t indx)
   {
+    if (indx>1) {
+      throw std::out_of_range("Index must be in the range [0-1]");
+    }
     return (&x)[indx];
   }
   
   //! const element access
   const Real& operator[](std::size_t indx) const
   {
-    
+    if (indx>1) {
+      throw std::out_of_range("Index must be in the range [0-1]");
+    }
     return (&x)[indx];
   }
   
diff --git a/modules/geom/src/vec3.hh b/modules/geom/src/vec3.hh
index 627de4c1e..5283146a1 100644
--- a/modules/geom/src/vec3.hh
+++ b/modules/geom/src/vec3.hh
@@ -19,6 +19,7 @@
 #ifndef GEOM_VEC3_H
 #define GEOM_VEC3_H
 
+#include <stdexcept>
 #include <cstddef> // for size_t
 #include <ostream>
 #include <vector>
@@ -88,13 +89,18 @@ public:
   //! element access
   Real& operator[](std::size_t indx)
   {
+    if (indx>2) {
+      throw std::out_of_range("Index must be in the range [0-2]");
+    }
     return (&x)[indx];
   }
   
   //! const element access
   const Real& operator[](std::size_t indx) const
   {
-    
+    if (indx>2) {
+      throw std::out_of_range("Index must be in the range [0-2]");
+    }
     return (&x)[indx];
   }
   //! element access
diff --git a/modules/geom/src/vec4.hh b/modules/geom/src/vec4.hh
index ad3367b97..5442fc203 100644
--- a/modules/geom/src/vec4.hh
+++ b/modules/geom/src/vec4.hh
@@ -19,6 +19,7 @@
 #ifndef GEOM_VEC4_H
 #define GEOM_VEC4_H
 
+#include <stdexcept>
 #include <cstddef> // for size_t
 #include <ostream>
 
@@ -82,13 +83,18 @@ public:
   //! element access
   Real& operator[](std::size_t indx)
   {
+    if (indx>3) {
+      throw std::out_of_range("Index must be in the range [0-3]");
+    }
     return (&x)[indx];
   }
   
   //! const element access
   const Real& operator[](std::size_t indx) const
   {
-    
+    if (indx>3) {
+      throw std::out_of_range("Index must be in the range [0-3]");
+    }
     return (&x)[indx];
   }
   //! element access
-- 
GitLab