From 6dbc3b9f9243ca4fa4ba6edca07e38b1c4e03f01 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Tue, 8 Jun 2010 10:26:17 +0200
Subject: [PATCH] BUG: incorrect range check in interpolationLookUpTable

---
 .../interpolationLookUpTable.C                | 46 +++++++++----------
 .../interpolationLookUpTable.H                | 10 ++--
 2 files changed, 27 insertions(+), 29 deletions(-)

diff --git a/src/OpenFOAM/interpolations/interpolationLookUpTable/interpolationLookUpTable.C b/src/OpenFOAM/interpolations/interpolationLookUpTable/interpolationLookUpTable.C
index 90f6c8e54de..c4c1d1d65f6 100644
--- a/src/OpenFOAM/interpolations/interpolationLookUpTable/interpolationLookUpTable.C
+++ b/src/OpenFOAM/interpolations/interpolationLookUpTable/interpolationLookUpTable.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2008-2009 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2008-2010 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -23,7 +23,6 @@ License
 
 \*---------------------------------------------------------------------------*/
 
-#include "IFstream.H"
 
 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
 
@@ -239,7 +238,9 @@ Foam::interpolationLookUpTable<Type>::interpolationLookUpTable()
 template<class Type>
 Foam::interpolationLookUpTable<Type>::interpolationLookUpTable
 (
-    const fileName& fn, const word& instance, const fvMesh& mesh
+    const fileName& fn,
+    const word& instance,
+    const fvMesh& mesh
 )
 :
     List<scalarField>(),
@@ -357,11 +358,11 @@ void Foam::interpolationLookUpTable<Type>::write
 
     control.writeHeader(os);
 
-    os.writeKeyword("fields");
-    os << entries_ << token::END_STATEMENT << nl;
+    os.writeKeyword("fields")
+        << entries_ << token::END_STATEMENT << nl;
 
-    os.writeKeyword("output");
-    os << output_ << token::END_STATEMENT << nl;
+    os.writeKeyword("output")
+        << output_ << token::END_STATEMENT << nl;
 
     if (this->size() == 0)
     {
@@ -370,8 +371,8 @@ void Foam::interpolationLookUpTable<Type>::write
             "Foam::interpolationTable<Type>::write()"
         )   << "table is empty" << nl << exit(FatalError);
     }
-    os.writeKeyword("values");
-    os << *this << token::END_STATEMENT << nl;
+    os.writeKeyword("values")
+        << *this << token::END_STATEMENT << nl;
 }
 
 
@@ -381,8 +382,7 @@ template<class Type>
 Foam::scalarField&
 Foam::interpolationLookUpTable<Type>::operator[](const label i)
 {
-    label ii = i;
-    label n  = this->size();
+    const label n = this->size();
 
     if (n <= 1)
     {
@@ -391,22 +391,22 @@ Foam::interpolationLookUpTable<Type>::operator[](const label i)
             "Foam::interpolationLookUpTable<Type>::operator[](const label)"
         )   << "table has (" << n << ") columns" << nl << exit(FatalError);
     }
-    else if (ii < 0)
+    else if (i < 0)
     {
         FatalErrorIn
         (
             "Foam::interpolationLookUpTable<Type>::operator[](const label)"
-        )   << "index (" << ii << ") underflow" << nl << exit(FatalError);
+        )   << "index (" << i << ") underflow" << nl << exit(FatalError);
     }
-    else if (ii > n)
+    else if (i >= n)
     {
         FatalErrorIn
         (
             "Foam::interpolationLookUpTable<Type>::operator[](const label)"
-        )   << "index (" << ii << ") overflow" << nl << exit(FatalError);
+        )   << "index (" << i << ") overflow" << nl << exit(FatalError);
     }
 
-    return List<scalarField>::operator[](ii);
+    return List<scalarField>::operator[](i);
 }
 
 
@@ -414,8 +414,7 @@ template<class Type>
 const Foam::scalarField&
 Foam::interpolationLookUpTable<Type>::operator[](const label i) const
 {
-    label ii = i;
-    label n  = this->size();
+    const label n = this->size();
 
     if (n <= 1)
     {
@@ -425,26 +424,25 @@ Foam::interpolationLookUpTable<Type>::operator[](const label i) const
             "(const label) const"
         )   << "table has (" << n << ") columns" << nl << exit(FatalError);
     }
-    else if (ii < 0)
+    else if (i < 0)
     {
         FatalErrorIn
         (
             "Foam::interpolationLookUpTable<Type>::operator[]"
             "(const label) const"
-        )   << "index (" << ii << ") underflow" << nl << exit(FatalError);
+        )   << "index (" << i << ") underflow" << nl << exit(FatalError);
     }
-
-    else if (ii > n)
+    else if (i >= n)
     {
         FatalErrorIn
         (
             "Foam::interpolationLookUpTable<Type>::operator[]"
             "(const label) const"
-        )   << "index (" << ii << ") overflow" << nl
+        )   << "index (" << i << ") overflow" << nl
             << exit(FatalError);
     }
 
-    return List<scalarField>::operator[](ii);
+    return List<scalarField>::operator[](i);
 }
 
 
diff --git a/src/OpenFOAM/interpolations/interpolationLookUpTable/interpolationLookUpTable.H b/src/OpenFOAM/interpolations/interpolationLookUpTable/interpolationLookUpTable.H
index bfbd7ba065a..266541ac4cf 100644
--- a/src/OpenFOAM/interpolations/interpolationLookUpTable/interpolationLookUpTable.H
+++ b/src/OpenFOAM/interpolations/interpolationLookUpTable/interpolationLookUpTable.H
@@ -88,10 +88,10 @@ private:
         //- Output dictionaries
         List<dictionary> output_;
 
-        //- Input indices from the look up table
+        //- Input indices from the lookup table
         List<label> entryIndices_;
 
-        //- Output Indeces from the Look Up Table
+        //- Output indices from the lookup Table
         List<label> outputIndices_;
 
         //- Field names and indices
@@ -118,7 +118,7 @@ private:
         //- Check range of lookup value
         bool checkRange(const scalar, const label) const;
 
-        //- Interpolate function return an scalar
+        //- Interpolate function returning a scalar
         scalar interpolate
         (
             const label lo,
@@ -159,13 +159,13 @@ public:
 
     // Member Functions
 
-        //- Return true if the filed exists in the table
+        //- Return true if the field exists in the table
         bool found(const word& fieldName) const;
 
         //- Return the output list given a single input scalar
         const List<scalar>& lookUp(const scalar);
 
-        //- Write Look Up Table to filename.
+        //- Write lookup table to filename.
         void write
         (
             Ostream&,
-- 
GitLab