Skip to content

Commit

Permalink
Re #11827 Fix coverity issues
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelalvarezbanos committed May 29, 2015
1 parent 9a6f394 commit 618b083
Show file tree
Hide file tree
Showing 21 changed files with 103 additions and 23 deletions.
4 changes: 3 additions & 1 deletion Code/Mantid/Framework/API/src/FunctionFactory.cpp
Expand Up @@ -219,7 +219,9 @@ CompositeFunction_sptr FunctionFactoryImpl::createComposite(
}
}

cfun->applyTies();
if (cfun) {
cfun->applyTies();
}
return cfun;
}

Expand Down
10 changes: 7 additions & 3 deletions Code/Mantid/Framework/Algorithms/src/CorelliCrossCorrelate.cpp
Expand Up @@ -149,9 +149,13 @@ void CorelliCrossCorrelate::exec() {

// Read in the TDC timings for the correlation chopper and apply the timing
// offset.
std::vector<DateAndTime> tdc =
dynamic_cast<ITimeSeriesProperty *>(
inputWS->run().getLogData("chopper4_TDC"))->timesAsVector();
auto chopperTdc = dynamic_cast<ITimeSeriesProperty *>(
inputWS->run().getLogData("chopper4_TDC"));
if (!chopperTdc) {
throw std::runtime_error("chopper4_TDC not found");
}
std::vector<DateAndTime> tdc = chopperTdc->timesAsVector();

int offset_int = getProperty("TimingOffset");
const int64_t offset = static_cast<int64_t>(offset_int);
for (unsigned long i = 0; i < tdc.size(); ++i)
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/GenerateEventsFilter.cpp
Expand Up @@ -1886,6 +1886,9 @@ DateAndTime GenerateEventsFilter::findRunEnd() {
Kernel::TimeSeriesProperty<double> *protonchargelog =
dynamic_cast<Kernel::TimeSeriesProperty<double> *>(
m_dataWS->run().getProperty("proton_charge"));
if (!protonchargelog) {
throw std::runtime_error("proton_charge log not found");
}

if (protonchargelog->size() > 1) {
Kernel::DateAndTime tmpendtime = protonchargelog->lastTime();
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/RingProfile.cpp
Expand Up @@ -551,6 +551,9 @@ void RingProfile::getBinForPixel(const API::MatrixWorkspace_sptr ws,

API::NumericAxis *oldAxis2 = dynamic_cast<API::NumericAxis *>(ws->getAxis(1));
// assumption y position is the ws->getAxis(1)(spectrum_index)
if (!oldAxis2) {
throw std::logic_error("Failed to cast workspace axis to NumericAxis");
}

// calculate ypos, the difference of y - centre and the square of this
// difference
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Crystal/src/PeakClusterProjection.cpp
Expand Up @@ -28,6 +28,9 @@ PeakTransform_sptr makePeakTransform(IMDWorkspace const *const mdWS) {
} else if (mdCoordinates == Mantid::Kernel::HKL) {
peakTransformFactory = boost::make_shared<PeakTransformHKLFactory>();
}
if (!peakTransformFactory) {
throw std::runtime_error("Failed to get a valid PeakTransformFactory");
}
const std::string xDim = mdWS->getDimension(0)->getName();
const std::string yDim = mdWS->getDimension(1)->getName();
PeakTransform_sptr peakTransform =
Expand Down
6 changes: 5 additions & 1 deletion Code/Mantid/Framework/Crystal/src/PredictPeaks.cpp
Expand Up @@ -210,7 +210,11 @@ void PredictPeaks::exec() {
}
}
// Find the run number
runNumber = inWS->getRunNumber();
if (inWS ) {
runNumber = inWS->getRunNumber();
} else {
throw std::runtime_error("Failed to get run number");
}

wlMin = getProperty("WavelengthMin");
wlMax = getProperty("WavelengthMax");
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/CalculateMSVesuvio.cpp
Expand Up @@ -263,6 +263,9 @@ void CalculateMSVesuvio::cacheInputs() {
break;
}
// Bounding box in detector frame
if (!detPixel) {
throw std::runtime_error("Failed to get detector");
}
Geometry::Object_const_sptr pixelShape = detPixel->shape();
if (!pixelShape || !pixelShape->hasValidShape()) {
throw std::invalid_argument("Detector pixel has no defined shape!");
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/Fit.cpp
Expand Up @@ -83,6 +83,9 @@ void Fit::setDomainType() {
Kernel::Property *prop = getPointerToProperty("Minimizer");
auto minimizerProperty =
dynamic_cast<Kernel::PropertyWithValue<std::string> *>(prop);
if (!minimizerProperty) {
throw std::logic_error("Failed to cast Property to PropertyWithValue");
}
std::vector<std::string> minimizerOptions =
API::FuncMinimizerFactory::Instance().getKeys();
if (m_domainType != IDomainCreator::Simple) {
Expand Down
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/PlotPeakByLogValue.cpp
Expand Up @@ -236,6 +236,10 @@ void PlotPeakByLogValue::exec() {
}
TimeSeriesProperty<double> *logp =
dynamic_cast<TimeSeriesProperty<double> *>(prop);
if (!logp) {
throw std::runtime_error("Failed to cast " + logName +
" to TimeSeriesProperty");
}
logValue = logp->lastValue();
}

Expand Down
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/Geometry/src/ComponentParser.cpp
Expand Up @@ -70,6 +70,10 @@ void ComponentParser::endElement(const Poco::XML::XMLString &,
if (!m_current.empty())
current = m_current.back();

if (!current) {
throw std::runtime_error("Failed to find last component");
}

if (localName == "pos") {
V3D pos;
pos.fromString(m_innerText);
Expand Down
18 changes: 12 additions & 6 deletions Code/Mantid/Framework/Geometry/src/Instrument/CompAssembly.cpp
Expand Up @@ -168,9 +168,13 @@ int CompAssembly::remove(IComponent *comp) {
* @return m_children.size()
*/
int CompAssembly::nelements() const {
if (m_map)
return dynamic_cast<const CompAssembly *>(m_base)->nelements();
else
if (m_map) {
auto compAss = dynamic_cast<const CompAssembly *>(m_base);
if (!compAss) {
throw std::logic_error("Failed to cast base component to CompAssembly");
}
return compAss->nelements();
} else
return static_cast<int>(m_children.size());
}

Expand All @@ -187,11 +191,13 @@ int CompAssembly::nelements() const {
boost::shared_ptr<IComponent> CompAssembly::getChild(const int i) const {
if (m_map) {
// Get the child of the base (unparametrized) assembly
boost::shared_ptr<IComponent> child_base =
dynamic_cast<const CompAssembly *>(m_base)->getChild(i);
auto child_base = dynamic_cast<const CompAssembly *>(m_base);
if (!child_base) {
throw std::logic_error("Failed to cast base component to CompAssembly");
}
// And build up a parametrized version of it using the factory, and return
// that
return ParComponentFactory::create(child_base, m_map);
return ParComponentFactory::create(child_base->getChild(i), m_map);
} else {
if (i < 0 || i > static_cast<int>(m_children.size() - 1)) {
throw std::runtime_error("CompAssembly::getChild() range not valid");
Expand Down
Expand Up @@ -1127,6 +1127,10 @@ void InstrumentDefinitionParser::appendAssembly(
pType->getAttribute("outline") != "no") {
Geometry::ObjCompAssembly *objAss =
dynamic_cast<Geometry::ObjCompAssembly *>(ass);
if (!objAss) {
throw std::logic_error(
"Failed to cast ICompAssembly object to ObjCompAssembly");
}
if (pType->getAttribute("object_created") == "no") {
pType->setAttribute("object_created", "yes");
boost::shared_ptr<Geometry::Object> obj = objAss->createOutline();
Expand Down
20 changes: 14 additions & 6 deletions Code/Mantid/Framework/Geometry/src/Instrument/ObjCompAssembly.cpp
Expand Up @@ -173,9 +173,14 @@ int ObjCompAssembly::addCopy(IComponent *comp, const std::string &n) {
* @return group.size()
*/
int ObjCompAssembly::nelements() const {
if (m_map)
return dynamic_cast<const ObjCompAssembly *>(m_base)->nelements();
else
if (m_map) {
auto objCompAss = dynamic_cast<const ObjCompAssembly *>(m_base);
if (!objCompAss) {
throw std::logic_error(
"Failed to cast base component to ObjCompAssembly");
}
return objCompAss->nelements();
} else
return static_cast<int>(group.size());
}

Expand All @@ -194,9 +199,12 @@ boost::shared_ptr<IComponent> ObjCompAssembly::operator[](int i) const {
}

if (m_map) {
boost::shared_ptr<IComponent> child_base =
dynamic_cast<const ObjCompAssembly *>(m_base)->operator[](i);
return ParComponentFactory::create(child_base, m_map);
auto child_base = dynamic_cast<const ObjCompAssembly *>(m_base);
if (!child_base) {
throw std::logic_error(
"Failed to cast base component to ObjCompAssembly");
}
return ParComponentFactory::create(child_base->operator[](i), m_map);
} else {
// Unparamterized - return the normal one
return boost::shared_ptr<IComponent>(group[i], NoDeleting());
Expand Down
10 changes: 7 additions & 3 deletions Code/Mantid/Framework/Geometry/src/Instrument/ObjComponent.cpp
Expand Up @@ -46,9 +46,13 @@ ObjComponent::~ObjComponent() {}
/** Return the shape of the component
*/
const Object_const_sptr ObjComponent::shape() const {
if (m_map)
return dynamic_cast<const ObjComponent *>(m_base)->m_shape;
else
if (m_map) {
auto base = dynamic_cast<const ObjComponent *>(m_base);
if (!base) {
throw std::logic_error("Failed to cast base component to ObjComponent");
}
return base->m_shape;
} else
return m_shape;
}

Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Geometry/src/Objects/Rules.cpp
Expand Up @@ -416,6 +416,9 @@ int Rule::removeItem(Rule *&TRule, const int SurfN)
} else // Basic surf object
{
SurfPoint *SX = dynamic_cast<SurfPoint *>(Ptr);
if (!SX) {
throw std::logic_error("Failed to cast Rule object to SurfPoint");
}
SX->setKeyN(0);
SX->setKey(0);
return cnt + 1;
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Kernel/src/LogParser.cpp
Expand Up @@ -248,6 +248,9 @@ LogParser::LogParser(const Kernel::Property *log) : m_nOfPeriods(1) {
Kernel::TimeSeriesProperty<bool> *LogParser::createPeriodLog(int period) const {
Kernel::TimeSeriesProperty<int> *periods =
dynamic_cast<Kernel::TimeSeriesProperty<int> *>(m_periods.get());
if (!periods) {
throw std::logic_error("Failed to cast periods to TimeSeriesProperty");
}
std::ostringstream ostr;
ostr << period;
Kernel::TimeSeriesProperty<bool> *p =
Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/Framework/MDAlgorithms/src/BinMD.cpp
Expand Up @@ -351,7 +351,8 @@ void BinMD::binByIterating(typename MDEventWorkspace<MDE, nd>::sptr ws) {

// Now the implicit function
if (implicitFunction) {
prog->report("Applying implicit function.");
if (prog)
prog->report("Applying implicit function.");
signal_t nan = std::numeric_limits<signal_t>::quiet_NaN();
outWS->applyImplicitFunction(implicitFunction, nan, nan);
}
Expand Down
Expand Up @@ -269,6 +269,9 @@ void ConvertToReflectometryQ::exec() {
Property *p = run.getLogData("stheta");
auto incidentThetas =
dynamic_cast<Mantid::Kernel::TimeSeriesProperty<double> *>(p);
if (!incidentThetas) {
throw std::runtime_error("stheta log not found");
}
incidentTheta =
incidentThetas->valuesAsVector().back(); // Not quite sure what to do
// with the time series for
Expand Down
9 changes: 7 additions & 2 deletions Code/Mantid/Framework/MDAlgorithms/src/FindPeaksMD.cpp
Expand Up @@ -403,8 +403,13 @@ void FindPeaksMD::findPeaks(typename MDEventWorkspace<MDE, nd>::sptr ws) {

try {
auto p = this->createPeak(Q, binCount);
if (m_addDetectors)
addDetectors(*p, *dynamic_cast<MDBoxBase<MDE, nd> *>(box));
if (m_addDetectors) {
auto mdBox = dynamic_cast<MDBoxBase<MDE, nd> *>(box);
if (!mdBox) {
throw std::runtime_error("Failed to cast box to MDBoxBase");
}
addDetectors(*p, *mdBox);
}
if (p->getDetectorID() != -1) peakWS->addPeak(*p);
} catch (std::exception &e) {
g_log.notice() << "Error creating peak at " << Q << " because of '"
Expand Down
Expand Up @@ -293,6 +293,11 @@ void IntegrateMDHistoWorkspace::exec() {
boost::scoped_ptr<MDHistoWorkspaceIterator> outIterator(
dynamic_cast<MDHistoWorkspaceIterator *>(outIterators[i]));

if (!outIterator) {
throw std::logic_error(
"Failed to cast iterator to MDHistoWorkspaceIterator");
}

do {

Mantid::Kernel::VMD outIteratorCenter = outIterator->getCenter();
Expand Down
5 changes: 5 additions & 0 deletions Code/Mantid/Framework/MDAlgorithms/src/SmoothMD.cpp
Expand Up @@ -133,6 +133,11 @@ SmoothMD::hatSmooth(IMDHistoWorkspace_const_sptr toSmooth,
boost::scoped_ptr<MDHistoWorkspaceIterator> iterator(
dynamic_cast<MDHistoWorkspaceIterator *>(iterators[it]));

if (!iterator) {
throw std::logic_error(
"Failed to cast IMDIterator to MDHistoWorkspaceIterator");
}

do {
// Gets all vertex-touching neighbours
size_t iteratorIndex = iterator->getLinearIndex();
Expand Down

0 comments on commit 618b083

Please sign in to comment.