ENH: extend size of NullObject for safer reinterpret cast

- previously had a single pointer/value zeros (8 bytes), this meant
  that the reinterpret cast to a List would yield a reference that
  could be unsafe under certain conditions.

  Eg,
     const labelList& myList = labelList::null();

     Info<< myList.size() << nl; // OK since size is the first parameter

     SubList<label>(myList, 0);  // Unsafe

  The SubList usage is unsafe since it passes in pointer and size into
  the underlying UList. However, the pointer from the labelList::null()
  will be whatever happens to be around in memory immediately after the
  NullObject singleton. This is mostly not a problem if the List size
  is always checked, but does mean that the data pointer is rather
  dubious.

- Increase the size of the nullObject singleton to 32 bytes of zeros
  to ensure that most reinterpret casting will not result in objects
  that reference arbitrary memory.

  The 32-byte data size is rather arbitrary, but covers most basic
  containers.
This commit is contained in:
Mark Olesen
2019-02-07 11:13:13 +01:00
committed by Andrew Heather
parent 2016f88eba
commit eaa3da72c5
2 changed files with 132 additions and 41 deletions

View File

@ -1,11 +1,49 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2014 OpenFOAM Foundation
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-nullObject
Description
Tests of nullObject
\*---------------------------------------------------------------------------*/
#include "nullObject.H"
#include "List.H"
#include "HashSet.H"
#include "faceList.H"
#include "pointField.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
using namespace Foam;
class SimpleClass
{
public:
//- Null constructor
@ -14,6 +52,26 @@ public:
};
template<class T>
void printInfo(const UList<T>& list)
{
typedef unsigned long ptrval;
std::cout
<< nl
<< "List : addr: " << ptrval(&list)
<< " (null: " << isNull(list) << ")" << nl
<< " size: " << list.size() << " empty: " << list.empty() << nl
<< " data: " << ptrval(list.cdata())
<< " begin=" << ptrval(list.begin())
<< " end=" << ptrval(list.end()) << nl;
Info<< list << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main()
{
// Test pointer and reference to a class
@ -23,31 +81,34 @@ int main()
typedef unsigned long ptrval;
Info<<"nullObject address=" << ptrval(&(nullObjectPtr)) << endl;
Info<<"sizeof(nullObject)" << " == "
<< sizeof(NullObject::nullObject)
<< " vs. sizeof(void*)" << " == " << sizeof(void*)
<< endl;
std::cout
<< "nullObject addr=" << ptrval(&(nullObjectPtr)) << nl
<< " sizeof(nullObject) = " << sizeof(NullObject::nullObject) << nl
<< " sizeof(void*) = " << sizeof(void*) << nl
<< " sizeof(labelList) = " << sizeof(labelList) << nl
<< " sizeof(wordHashSet) = " << sizeof(wordHashSet) << nl << nl;
Info<<"nullObject pointer:" << ptrval(nullObjectPtr->pointer()) << endl;
Info<<"nullObject value:" << nullObjectPtr->value() << endl;
std::cout
<< "nullObject" << nl
<< " pointer:" << ptrval(nullObjectPtr->pointer()) << nl
<< " value:" << nullObjectPtr->value() << nl << nl;
if (notNull(ptrToClass))
{
Info<< "Pass: ptrToClass is not null" << endl;
Info<< "Pass: ptrToClass is not null" << nl;
}
else
{
Info<< "FAIL: refToClass is null" << endl;
Info<< "FAIL: refToClass is null" << nl;
}
if (notNull(refToClass))
{
Info<< "Pass: refToClass is not null" << endl;
Info<< "Pass: refToClass is not null" << nl;
}
else
{
Info<< "FAIL: refToClass is null" << endl;
Info<< "FAIL: refToClass is null" << nl;
}
@ -58,24 +119,42 @@ int main()
if (isNull(ptrToNull))
{
Info<< "Pass: ptrToNull is null" << endl;
Info<< "Pass: ptrToNull is null" << nl;
}
else
{
Info<< "FAIL: ptrToNull is not null" << endl;
Info<< "FAIL: ptrToNull is not null" << nl;
}
if (isNull(refToNull))
{
Info<< "Pass: refToNull is null" << endl;
Info<< "Pass: refToNull is null" << nl;
}
else
{
Info<< "FAIL: refToNull is not null" << endl;
Info<< "FAIL: refToNull is not null" << nl;
}
// Clean-up
delete ptrToClass;
// Test List casting
{
labelList list1;
labelList list2({1, 2, 3});
printInfo(list1);
printInfo(list2);
printInfo(labelList::null());
printInfo(faceList::null());
printInfo(pointField::null());
}
Info<< nl;
return 0;
}
// ************************************************************************* //