Skip to content

Commit 8887335

Browse files
Static analysis fixes (#2531)
This changelist addresses a handful of static analysis warnings flagged by PVS-Studio: - Fix a potential division by zero in `Graph::findAvgY`. - Replace `auto` with `const auto&` in loop variables for efficiency.
1 parent dd699d8 commit 8887335

File tree

8 files changed

+10
-10
lines changed

8 files changed

+10
-10
lines changed

source/MaterialXCore/Unit.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void LinearUnitConverter::write(DocumentPtr doc) const
7272
unitDef->setUnitType(_unitType);
7373

7474
// Add in units to the definition
75-
for (auto unitScale : _unitScale)
75+
for (const auto& unitScale : _unitScale)
7676
{
7777
const string& unitName = unitScale.first;
7878
UnitPtr unitPtr = unitDef->addUnit(unitName);
@@ -223,7 +223,7 @@ void UnitConverterRegistry::clearUnitConverters()
223223

224224
int UnitConverterRegistry::getUnitAsInteger(const string& unitName) const
225225
{
226-
for (auto it : _unitConverters)
226+
for (const auto& it : _unitConverters)
227227
{
228228
int value = it.second->getUnitAsInteger(unitName);
229229
if (value >= 0)
@@ -235,7 +235,7 @@ int UnitConverterRegistry::getUnitAsInteger(const string& unitName) const
235235

236236
void UnitConverterRegistry::write(DocumentPtr doc) const
237237
{
238-
for (auto it : _unitConverters)
238+
for (const auto& it : _unitConverters)
239239
{
240240
it.second->write(doc);
241241
}

source/MaterialXGenOsl/OslShaderGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ void OslShaderGenerator::registerShaderMetadata(const DocumentPtr& doc, GenConte
235235
{ ValueElement::UI_STEP_ATTRIBUTE, "sensitivity" },
236236
{ ValueElement::DOC_ATTRIBUTE, "help" }
237237
};
238-
for (auto it : nameRemapping)
238+
for (const auto& it : nameRemapping)
239239
{
240240
ShaderMetadata* data = registry->findMetadata(it.first);
241241
if (data)

source/MaterialXGenShader/GenContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ShaderNodeImplPtr GenContext::findNodeImplementation(const string& name) const
5353

5454
void GenContext::getNodeImplementationNames(StringSet& names)
5555
{
56-
for (auto it : _nodeImpls)
56+
for (const auto& it : _nodeImpls)
5757
{
5858
names.insert(it.first);
5959
}

source/MaterialXGenShader/UnitSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void ScalarUnitNode::emitFunctionDefinition(const ShaderNode& node, GenContext&
5656
unitScales.reserve(_scalarUnitConverter->getUnitScale().size());
5757
auto unitScaleMap = _scalarUnitConverter->getUnitScale();
5858
unitScales.resize(unitScaleMap.size());
59-
for (auto unitScale : unitScaleMap)
59+
for (const auto& unitScale : unitScaleMap)
6060
{
6161
int location = _scalarUnitConverter->getUnitAsInteger(unitScale.first);
6262
unitScales[location] = unitScale.second;

source/MaterialXGraphEditor/Graph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ float Graph::findAvgY(const std::vector<UiNodePtr>& nodes)
507507
total += ((size.y + pos.y) + pos.y) / 2;
508508
count++;
509509
}
510-
return (total / count);
510+
return count ? (total / count) : 0.0f;
511511
}
512512

513513
void Graph::findYSpacing(float startY)

source/MaterialXGraphEditor/RenderView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ void RenderView::initContext(mx::GenContext& context)
604604
// Create the list of supported distance units.
605605
auto unitScales = _distanceUnitConverter->getUnitScale();
606606
_distanceUnitOptions.resize(unitScales.size());
607-
for (auto unitScale : unitScales)
607+
for (const auto& unitScale : unitScales)
608608
{
609609
int location = _distanceUnitConverter->getUnitAsInteger(unitScale.first);
610610
_distanceUnitOptions[location] = unitScale.first;

source/MaterialXRender/ImageHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ bool ImageHandler::unbindImage(ImagePtr)
159159

160160
void ImageHandler::unbindImages()
161161
{
162-
for (auto iter : _imageCache)
162+
for (const auto& iter : _imageCache)
163163
{
164164
unbindImage(iter.second);
165165
}

source/MaterialXRenderGlsl/GLTextureHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void GLTextureHandler::releaseRenderResources(ImagePtr image)
139139
{
140140
if (!image)
141141
{
142-
for (auto iter : _imageCache)
142+
for (const auto& iter : _imageCache)
143143
{
144144
if (iter.second)
145145
{

0 commit comments

Comments
 (0)