Add marco RequireNotNull

This is used to fix a number of possible nullptr dereferences
This commit is contained in:
Noah Hilverling 2018-01-30 11:26:07 +01:00 committed by Jean Flach
parent 6f7739b1cb
commit 3ba5090867
22 changed files with 92 additions and 9 deletions

View File

@ -30,6 +30,7 @@ static double ArrayLen(void)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
return self->GetLength();
}
@ -37,6 +38,7 @@ static void ArraySet(int index, const Value& value)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
self->Set(index, value);
}
@ -44,6 +46,7 @@ static Value ArrayGet(int index)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
return self->Get(index);
}
@ -51,6 +54,7 @@ static void ArrayAdd(const Value& value)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
self->Add(value);
}
@ -58,6 +62,7 @@ static void ArrayRemove(int index)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
self->Remove(index);
}
@ -65,6 +70,7 @@ static bool ArrayContains(const Value& value)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
return self->Contains(value);
}
@ -72,6 +78,7 @@ static void ArrayClear(void)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
self->Clear();
}
@ -87,6 +94,7 @@ static Array::Ptr ArraySort(const std::vector<Value>& args)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
Array::Ptr arr = self->ShallowClone();
@ -110,6 +118,7 @@ static Array::Ptr ArrayShallowClone(void)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
return self->ShallowClone();
}
@ -117,6 +126,7 @@ static Value ArrayJoin(const Value& separator)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
Value result;
bool first = true;
@ -139,6 +149,7 @@ static Array::Ptr ArrayReverse(void)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
return self->Reverse();
}
@ -146,6 +157,7 @@ static Array::Ptr ArrayMap(const Function::Ptr& function)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
if (vframe->Sandboxed && !function->IsSideEffectFree())
BOOST_THROW_EXCEPTION(ScriptError("Map function must be side-effect free."));
@ -166,6 +178,7 @@ static Value ArrayReduce(const Function::Ptr& function)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
if (vframe->Sandboxed && !function->IsSideEffectFree())
BOOST_THROW_EXCEPTION(ScriptError("Reduce function must be side-effect free."));
@ -190,6 +203,7 @@ static Array::Ptr ArrayFilter(const Function::Ptr& function)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
if (vframe->Sandboxed && !function->IsSideEffectFree())
BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
@ -211,6 +225,7 @@ static bool ArrayAny(const Function::Ptr& function)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
if (vframe->Sandboxed && !function->IsSideEffectFree())
BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
@ -230,6 +245,7 @@ static bool ArrayAll(const Function::Ptr& function)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
if (vframe->Sandboxed && !function->IsSideEffectFree())
BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
@ -248,6 +264,7 @@ static Array::Ptr ArrayUnique(void)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
RequireNotNull(self);
std::set<Value> result;

View File

@ -29,6 +29,7 @@ static void ConfigObjectModifyAttribute(const String& attr, const Value& value)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
ConfigObject::Ptr self = vframe->Self;
RequireNotNull(self);
return self->ModifyAttribute(attr, value);
}
@ -36,6 +37,7 @@ static void ConfigObjectRestoreAttribute(const String& attr)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
ConfigObject::Ptr self = vframe->Self;
RequireNotNull(self);
return self->RestoreAttribute(attr);
}

View File

@ -29,6 +29,7 @@ static String DateTimeFormat(const String& format)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
DateTime::Ptr self = static_cast<DateTime::Ptr>(vframe->Self);
RequireNotNull(self);
return self->Format(format);
}

View File

@ -29,6 +29,7 @@ static double DictionaryLen(void)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
RequireNotNull(self);
return self->GetLength();
}
@ -36,6 +37,7 @@ static void DictionarySet(const String& key, const Value& value)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
RequireNotNull(self);
self->Set(key, value);
}
@ -43,6 +45,7 @@ static Value DictionaryGet(const String& key)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
RequireNotNull(self);
return self->Get(key);
}
@ -50,6 +53,7 @@ static void DictionaryRemove(const String& key)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
RequireNotNull(self);
self->Remove(key);
}
@ -57,6 +61,7 @@ static bool DictionaryContains(const String& key)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
RequireNotNull(self);
return self->Contains(key);
}
@ -64,6 +69,7 @@ static Dictionary::Ptr DictionaryShallowClone(void)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
RequireNotNull(self);
return self->ShallowClone();
}
@ -72,6 +78,8 @@ static Array::Ptr DictionaryKeys(void)
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
Array::Ptr keys = new Array();
RequireNotNull(self);
ObjectLock olock(self);
for (const Dictionary::Pair& kv : self) {
keys->Add(kv.first);
@ -84,6 +92,8 @@ static Array::Ptr DictionaryValues(void)
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
Array::Ptr keys = new Array();
RequireNotNull(self);
ObjectLock olock(self);
for (const Dictionary::Pair& kv : self) {
keys->Add(kv.second);

View File

@ -32,6 +32,7 @@ static Value FunctionCall(const std::vector<Value>& args)
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
RequireNotNull(self);
std::vector<Value> uargs(args.begin() + 1, args.end());
return self->Invoke(args[0], uargs);
@ -41,6 +42,7 @@ static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
RequireNotNull(self);
std::vector<Value> uargs;

View File

@ -29,6 +29,7 @@ static String ObjectToString(void)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Object::Ptr self = static_cast<Object::Ptr>(vframe->Self);
RequireNotNull(self);
return self->ToString();
}
@ -36,6 +37,7 @@ static void ObjectNotifyAttribute(const String& attribute)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Object::Ptr self = static_cast<Object::Ptr>(vframe->Self);
RequireNotNull(self);
self->NotifyField(self->GetReflectionType()->GetFieldId(attribute));
}
@ -43,6 +45,7 @@ static Object::Ptr ObjectClone(void)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Object::Ptr self = static_cast<Object::Ptr>(vframe->Self);
RequireNotNull(self);
return self->Clone();
}

View File

@ -261,3 +261,8 @@ INITIALIZE_ONCE([]() {
});
#endif /* I2_LEAK_DEBUG */
void icinga::RequireNotNullInternal(const intrusive_ptr<Object>& object, const char *description)
{
if (!object)
BOOST_THROW_EXCEPTION(std::invalid_argument("Pointer must not be null: " + String(description)));
}

View File

@ -62,6 +62,10 @@ extern I2_BASE_API Value Empty;
DECLARE_PTR_TYPEDEFS(klass); \
IMPL_TYPE_LOOKUP();
#define RequireNotNull(ptr) RequireNotNullInternal(ptr, #ptr)
I2_BASE_API void RequireNotNullInternal(const intrusive_ptr<Object>& object, const char *description);
template<typename T>
intrusive_ptr<Object> DefaultObjectFactory(const std::vector<Value>& args)
{

View File

@ -37,7 +37,8 @@ static void TypeRegisterAttributeHandler(const String& fieldName, const Function
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Type::Ptr self = static_cast<Type::Ptr>(vframe->Self);
RequireNotNull(self);
int fid = self->GetFieldId(fieldName);
self->RegisterAttributeHandler(fid, boost::bind(&InvokeAttributeHandlerHelper, callback, _1, _2));
}

View File

@ -30,6 +30,7 @@ static void CheckableProcessCheckResult(const CheckResult::Ptr& cr)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Checkable::Ptr self = vframe->Self;
RequireNotNull(self);
self->ProcessCheckResult(cr);
}

View File

@ -40,6 +40,9 @@ Value MacroProcessor::ResolveMacros(const Value& str, const ResolverList& resolv
const MacroProcessor::EscapeCallback& escapeFn, const Dictionary::Ptr& resolvedMacros,
bool useResolvedMacros, int recursionLevel)
{
if (useResolvedMacros)
RequireNotNull(resolvedMacros);
Value result;
if (str.IsEmpty())
@ -454,6 +457,9 @@ Value MacroProcessor::ResolveArguments(const Value& command, const Dictionary::P
const MacroProcessor::ResolverList& resolvers, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel)
{
if (useResolvedMacros)
RequireNotNull(resolvedMacros);
Value resolvedCommand;
if (!arguments || command.IsObjectType<Array>() || command.IsObjectType<Function>())
resolvedCommand = MacroProcessor::ResolveMacros(command, resolvers, cr, NULL,

View File

@ -38,6 +38,9 @@ REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterCheck, &ClusterCheckTask::ScriptFunc
void ClusterCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
RequireNotNull(checkable);
RequireNotNull(cr);
if (resolvedMacros && !useResolvedMacros)
return;

View File

@ -34,6 +34,9 @@ REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterZoneCheck, &ClusterZoneCheckTask::Sc
void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
RequireNotNull(checkable);
RequireNotNull(cr);
ApiListener::Ptr listener = ApiListener::GetInstance();
if (!listener) {

View File

@ -34,6 +34,9 @@ REGISTER_SCRIPTFUNCTION_NS(Internal, ExceptionCheck, &ExceptionCheckTask::Script
void ExceptionCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
RequireNotNull(service);
RequireNotNull(cr);
if (resolvedMacros && !useResolvedMacros)
return;

View File

@ -35,6 +35,9 @@ REGISTER_SCRIPTFUNCTION_NS(Internal, IcingaCheck, &IcingaCheckTask::ScriptFunc,
void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
RequireNotNull(service);
RequireNotNull(cr);
if (resolvedMacros && !useResolvedMacros)
return;

View File

@ -32,9 +32,12 @@ using namespace icinga;
REGISTER_SCRIPTFUNCTION_NS(Internal, NullCheck, &NullCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
void NullCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
void NullCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
RequireNotNull(checkable);
RequireNotNull(cr);
if (resolvedMacros && !useResolvedMacros)
return;
@ -48,5 +51,5 @@ void NullCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult:
cr->SetPerformanceData(perfdata);
cr->SetState(ServiceOK);
service->ProcessCheckResult(cr);
checkable->ProcessCheckResult(cr);
}

View File

@ -25,5 +25,7 @@ using namespace icinga;
REGISTER_SCRIPTFUNCTION_NS(Internal, NullEvent, &NullEventTask::ScriptFunc, "checkable:resolvedMacros:useResolvedMacros");
void NullEventTask::ScriptFunc(const Checkable::Ptr&, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{ }
void NullEventTask::ScriptFunc(const Checkable::Ptr& checkable, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
RequireNotNull(checkable);
}

View File

@ -38,6 +38,9 @@ REGISTER_SCRIPTFUNCTION_NS(Internal, PluginCheck, &PluginCheckTask::ScriptFunc,
void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
RequireNotNull(checkable);
RequireNotNull(cr);
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
Host::Ptr host;

View File

@ -36,6 +36,8 @@ REGISTER_SCRIPTFUNCTION_NS(Internal, PluginEvent, &PluginEventTask::ScriptFunc,
void PluginEventTask::ScriptFunc(const Checkable::Ptr& checkable,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
RequireNotNull(checkable);
EventCommand::Ptr commandObj = checkable->GetEventCommand();
Host::Ptr host;

View File

@ -39,6 +39,9 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification,
const String& author, const String& comment, const Dictionary::Ptr& resolvedMacros,
bool useResolvedMacros)
{
RequireNotNull(notification);
RequireNotNull(user);
NotificationCommand::Ptr commandObj = notification->GetCommand();
NotificationType type = static_cast<NotificationType>(itype);

View File

@ -32,9 +32,12 @@ using namespace icinga;
REGISTER_SCRIPTFUNCTION_NS(Internal, RandomCheck, &RandomCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
void RandomCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
void RandomCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
RequireNotNull(checkable);
RequireNotNull(cr);
if (resolvedMacros && !useResolvedMacros)
return;
@ -48,5 +51,5 @@ void RandomCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResul
cr->SetPerformanceData(perfdata);
cr->SetState(static_cast<ServiceState>(Utility::Random() % 4));
service->ProcessCheckResult(cr);
checkable->ProcessCheckResult(cr);
}

View File

@ -25,15 +25,18 @@ using namespace icinga;
REGISTER_SCRIPTFUNCTION_NS(Internal, EmptyTimePeriod, &TimePeriodTask::EmptyTimePeriodUpdate, "tp:begin:end");
REGISTER_SCRIPTFUNCTION_NS(Internal, EvenMinutesTimePeriod, &TimePeriodTask::EvenMinutesTimePeriodUpdate, "tp:begin:end");
Array::Ptr TimePeriodTask::EmptyTimePeriodUpdate(const TimePeriod::Ptr&, double, double)
Array::Ptr TimePeriodTask::EmptyTimePeriodUpdate(const TimePeriod::Ptr& tp, double, double)
{
RequireNotNull(tp);
Array::Ptr segments = new Array();
return segments;
}
Array::Ptr TimePeriodTask::EvenMinutesTimePeriodUpdate(const TimePeriod::Ptr&, double begin, double end)
Array::Ptr TimePeriodTask::EvenMinutesTimePeriodUpdate(const TimePeriod::Ptr& tp, double begin, double end)
{
Array::Ptr segments = new Array();
RequireNotNull(tp);
for (long t = begin / 60 - 1; t * 60 < end; t++) {
if ((t % 2) == 0) {