From 379c86c85fd0781c262718c93e5e93dfe24df3fb Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Fri, 11 May 2018 14:41:22 +0100 Subject: [PATCH] NamedEnum: Check range of names array index to avoid warning from gcc --- src/OpenFOAM/containers/NamedEnum/NamedEnum.C | 23 ++++++++++++++++++- src/OpenFOAM/containers/NamedEnum/NamedEnum.H | 8 ++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/OpenFOAM/containers/NamedEnum/NamedEnum.C b/src/OpenFOAM/containers/NamedEnum/NamedEnum.C index 05733eadf..f96bea14f 100644 --- a/src/OpenFOAM/containers/NamedEnum/NamedEnum.C +++ b/src/OpenFOAM/containers/NamedEnum/NamedEnum.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -120,4 +120,25 @@ Foam::wordList Foam::NamedEnum::words() } +template +const char* Foam::NamedEnum::operator[](const Enum e) const +{ + unsigned int ue = unsigned(e); + + if (ue < nEnum) + { + return names[ue]; + } + else + { + FatalErrorInFunction + << "names array index " << ue << " out of range 0-" + << nEnum - 1 + << exit(FatalError); + + return names[0]; + } +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/NamedEnum/NamedEnum.H b/src/OpenFOAM/containers/NamedEnum/NamedEnum.H index e5ed68ddb..7d9fae597 100644 --- a/src/OpenFOAM/containers/NamedEnum/NamedEnum.H +++ b/src/OpenFOAM/containers/NamedEnum/NamedEnum.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -38,7 +38,6 @@ SourceFiles #include "HashTable.H" #include "stringList.H" #include "wordList.H" -#include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -108,10 +107,7 @@ public: } //- Return the name of the given enumeration element - const char* operator[](const Enum e) const - { - return names[unsigned(e)]; - } + const char* operator[](const Enum e) const; };