Skip to content

Commit 9ecb5d1

Browse files
Fix potential dangling pointer usages.
Based on the fix for #273 (commit 667844c), this commit attempts to fix the same pattern in all of the mmSolver code (with MString at least). The issue is that the MString may be deallocated before the `.asChar()` can be called, so the underlying string that is returned is corrupted. This change is intended to fix that problem. GitHub issue #273.
1 parent 667844c commit 9ecb5d1

11 files changed

+141
-87
lines changed

src/mmSolver/adjust/adjust_base.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,8 @@ bool solve_v1(SolverOptions &solverOptions, CameraPtrList &cameraList,
13631363
for (MarkerPtrListCIt mit = unusedMarkerList.cbegin();
13641364
mit != unusedMarkerList.cend(); ++mit) {
13651365
MarkerPtr marker = *mit;
1366-
const char *markerName = marker->getLongNodeName().asChar();
1366+
MString markerNodeName = marker->getLongNodeName();
1367+
const char *markerName = markerNodeName.asChar();
13671368
MMSOLVER_MAYA_WRN("-> " << markerName);
13681369
}
13691370
}
@@ -1374,7 +1375,8 @@ bool solve_v1(SolverOptions &solverOptions, CameraPtrList &cameraList,
13741375
for (AttrPtrListCIt ait = unusedAttrList.cbegin();
13751376
ait != unusedAttrList.cend(); ++ait) {
13761377
AttrPtr attr = *ait;
1377-
const char *attrName = attr->getLongName().asChar();
1378+
MString attrLongName = attr->getLongName();
1379+
const char *attrName = attrLongName.asChar();
13781380
MMSOLVER_MAYA_WRN("-> " << attrName);
13791381
}
13801382
}
@@ -1555,8 +1557,8 @@ bool solve_v2(SolverOptions &solverOptions, CameraPtrList &cameraList,
15551557
for (MarkerPtrListCIt mit = unusedMarkerList.cbegin();
15561558
mit != unusedMarkerList.cend(); ++mit) {
15571559
MarkerPtr marker = *mit;
1558-
const char *markerName = marker->getLongNodeName().asChar();
1559-
MMSOLVER_MAYA_WRN("-> " << markerName);
1560+
const MString markerNodeName = marker->getLongNodeName();
1561+
MMSOLVER_MAYA_WRN("-> " << markerNodeName.asChar());
15601562
}
15611563
}
15621564

@@ -1566,8 +1568,8 @@ bool solve_v2(SolverOptions &solverOptions, CameraPtrList &cameraList,
15661568
for (AttrPtrListCIt ait = unusedAttrList.cbegin();
15671569
ait != unusedAttrList.cend(); ++ait) {
15681570
AttrPtr attr = *ait;
1569-
const char *attrName = attr->getLongName().asChar();
1570-
MMSOLVER_MAYA_WRN("-> " << attrName);
1571+
const MString attrName = attr->getLongName();
1572+
MMSOLVER_MAYA_WRN("-> " << attrName.asChar());
15711573
}
15721574
}
15731575

src/mmSolver/adjust/adjust_relationships.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,9 @@ int countUpNumberOfUnknownParameters(
329329
out_staticAttrList.push_back(attr);
330330
}
331331
} else {
332-
const char *attrName = attr->getName().asChar();
333-
MMSOLVER_MAYA_ERR("attr is not animated or free: " << attrName);
332+
const MString attrName = attr->getName();
333+
MMSOLVER_MAYA_ERR(
334+
"attr is not animated or free: " << attrName.asChar());
334335
}
335336
i++;
336337
}
@@ -392,9 +393,12 @@ void findMarkerToAttributeRelationship(const MarkerPtrList &markerList,
392393
BundlePtr bundle = marker->getBundle();
393394

394395
// Get node names.
395-
const char *markerName = marker->getNodeName().asChar();
396-
const char *camName = cam->getTransformNodeName().asChar();
397-
const char *bundleName = bundle->getNodeName().asChar();
396+
const MString markerNodeName = marker->getNodeName();
397+
const MString camNodeName = cam->getTransformNodeName();
398+
const MString bundleNodeName = bundle->getNodeName();
399+
const char *markerName = markerNodeName.asChar();
400+
const char *camName = camNodeName.asChar();
401+
const char *bundleName = bundleNodeName.asChar();
398402

399403
// Find list of plug names that are affected by the bundle.
400404
cmd = "";

src/mmSolver/adjust/adjust_results.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,8 @@ struct AffectsResult {
614614
AttrPtr attr = attrList[attrIndex];
615615

616616
// Get node names.
617-
const char *markerName = marker->getNodeName().asChar();
617+
const MString markerNodeName = marker->getNodeName();
618+
const char *markerName = markerNodeName.asChar();
618619

619620
// Get attribute full path.
620621
MPlug plug = attr->getPlug();

src/mmSolver/mayahelper/maya_marker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ MStatus Marker::getEnable(bool &value, const MTime &time,
229229

230230
MStatus Marker::getWeight(double &value, const MTime &time,
231231
const int timeEvalMode) {
232-
MStatus status;
232+
MStatus status = MS::kSuccess;
233233
MPlug plug = m_weight.getPlug();
234234
if (plug.isNull() == true) {
235235
value = 1.0;

src/mmSolver/mayahelper/maya_scene_graph.cpp

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,30 @@ bool attribute_has_complex_connection(MFnDependencyNode &depend_node,
139139
bool is_writable = source_attr_fn.isWritable();
140140
if (is_readable && !is_writable) {
141141
// This means the attribute is an 'output attribute'.
142+
const MString source_node_name = source_node_fn.name();
143+
const MString source_attr_name = source_attr_fn.name();
144+
const MString node_name = depend_node.name();
142145
MMSOLVER_MAYA_WRN(
143146
"MM Scene Graph: Complex attribute connection detected from "
144-
<< "\"" << source_node_fn.name().asChar() << "."
145-
<< source_attr_fn.name().asChar() << "\""
147+
<< "\"" << source_node_name.asChar() << "."
148+
<< source_attr_name.asChar() << "\""
146149
<< " to "
147-
<< "\"" << depend_node.name().asChar() << "." << name.asChar()
148-
<< "\": "
150+
<< "\"" << node_name.asChar() << "." << name.asChar() << "\": "
149151
<< " attr_is_readable=" << is_readable
150152
<< " attr_is_writable=" << is_writable);
151153
return true;
152154
}
153155

154156
// This means the attribute is an 'output attribute'.
157+
const MString source_node_name = source_node_fn.name();
158+
const MString source_attr_name = source_attr_fn.name();
159+
const MString node_name = depend_node.name();
155160
MMSOLVER_MAYA_VRB(
156161
"MM Scene Graph: No complex attribute connection detected from "
157-
<< "\"" << source_node_fn.name().asChar() << "."
158-
<< source_attr_fn.name().asChar() << "\""
162+
<< "\"" << source_node_name.asChar() << "." << source_attr_name.asChar()
163+
<< "\""
159164
<< " to "
160-
<< "\"" << depend_node.name().asChar() << "." << name.asChar() << "\": "
165+
<< "\"" << node_name.asChar() << "." << name.asChar() << "\": "
161166
<< " attr_is_readable=" << is_readable
162167
<< " attr_is_writable=" << is_writable);
163168

@@ -568,26 +573,29 @@ MStatus check_transform_node(MDagPath &dag_path) {
568573
CHECK_MSTATUS_AND_RETURN_IT(status);
569574
if (!path_valid) {
570575
status = MS::kFailure;
576+
const MString node_name = dag_path.fullPathName();
571577
MMSOLVER_MAYA_WRN("MM Scene Graph: Invalid DAG path: "
572-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
578+
<< "\"" << node_name.asChar() << "\"");
573579
CHECK_MSTATUS_AND_RETURN_IT(status);
574580
}
575581

576582
auto is_instanced = dag_path.isInstanced(&status);
577583
CHECK_MSTATUS_AND_RETURN_IT(status);
578584
if (is_instanced) {
579585
status = MS::kFailure;
586+
const MString node_name = dag_path.fullPathName();
580587
MMSOLVER_MAYA_WRN("MM Scene Graph: No support for instanced nodes: "
581-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
588+
<< "\"" << node_name.asChar() << "\"");
582589
CHECK_MSTATUS_AND_RETURN_IT(status);
583590
}
584591

585592
MObject node_mobject = dag_path.node(&status);
586593
CHECK_MSTATUS_AND_RETURN_IT(status);
587594
if (node_mobject.isNull()) {
588595
status = MS::kFailure;
596+
const MString node_name = dag_path.fullPathName();
589597
MMSOLVER_MAYA_WRN("MM Scene Graph: Invalid node MObject: "
590-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
598+
<< "\"" << node_name.asChar() << "\"");
591599
CHECK_MSTATUS_AND_RETURN_IT(status);
592600
}
593601

@@ -597,9 +605,10 @@ MStatus check_transform_node(MDagPath &dag_path) {
597605
CHECK_MSTATUS_AND_RETURN_IT(status);
598606
if (!is_zero(scale_pivot)) {
599607
status = MS::kFailure;
608+
const MString node_name = dag_path.fullPathName();
600609
MMSOLVER_MAYA_WRN(
601610
"MM Scene Graph: No support for non-zero scale pivot: "
602-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
611+
<< "\"" << node_name.asChar() << "\"");
603612
CHECK_MSTATUS_AND_RETURN_IT(status);
604613
}
605614

@@ -608,9 +617,10 @@ MStatus check_transform_node(MDagPath &dag_path) {
608617
CHECK_MSTATUS_AND_RETURN_IT(status);
609618
if (!is_zero(scale_pivot_translation)) {
610619
status = MS::kFailure;
620+
const MString node_name = dag_path.fullPathName();
611621
MMSOLVER_MAYA_WRN(
612622
"MM Scene Graph: No support for non-zero scale pivot translation: "
613-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
623+
<< "\"" << node_name.asChar() << "\"");
614624
CHECK_MSTATUS_AND_RETURN_IT(status);
615625
}
616626

@@ -619,9 +629,10 @@ MStatus check_transform_node(MDagPath &dag_path) {
619629
CHECK_MSTATUS_AND_RETURN_IT(status);
620630
if (!is_zero(rotate_pivot)) {
621631
status = MS::kFailure;
632+
const MString node_name = dag_path.fullPathName();
622633
MMSOLVER_MAYA_WRN(
623634
"MM Scene Graph: No support for non-zero rotate pivot: "
624-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
635+
<< "\"" << node_name.asChar() << "\"");
625636
CHECK_MSTATUS_AND_RETURN_IT(status);
626637
}
627638

@@ -630,10 +641,11 @@ MStatus check_transform_node(MDagPath &dag_path) {
630641
CHECK_MSTATUS_AND_RETURN_IT(status);
631642
if (!is_zero(rotate_pivot_translation)) {
632643
status = MS::kFailure;
644+
const MString node_name = dag_path.fullPathName();
633645
MMSOLVER_MAYA_WRN(
634646
"MM Scene Graph: No support for non-zero rotate pivot "
635647
"translation\": "
636-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
648+
<< "\"" << node_name.asChar() << "\"");
637649
CHECK_MSTATUS_AND_RETURN_IT(status);
638650
}
639651

@@ -642,89 +654,98 @@ MStatus check_transform_node(MDagPath &dag_path) {
642654
auto tx_has_conn = attribute_has_complex_connection(dg_node, tx_attr_name);
643655
if (tx_has_conn) {
644656
status = MS::kFailure;
657+
const MString node_name = dag_path.fullPathName();
645658
MMSOLVER_MAYA_WRN("MM Scene Graph: Unsupported attribute connection on "
646659
<< "\"" << tx_attr_name.asChar() << "\": "
647-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
660+
<< "\"" << node_name.asChar() << "\"");
648661
CHECK_MSTATUS_AND_RETURN_IT(status);
649662
}
650663

651664
auto ty_attr_name = MString("translateY");
652665
auto ty_has_conn = attribute_has_complex_connection(dg_node, ty_attr_name);
653666
if (ty_has_conn) {
654667
status = MS::kFailure;
668+
const MString node_name = dag_path.fullPathName();
655669
MMSOLVER_MAYA_WRN("MM Scene Graph: Unsupported attribute connection on "
656670
<< "\"" << ty_attr_name.asChar() << "\": "
657-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
671+
<< "\"" << node_name.asChar() << "\"");
658672
CHECK_MSTATUS_AND_RETURN_IT(status);
659673
}
660674

661675
auto tz_attr_name = MString("translateZ");
662676
auto tz_has_conn = attribute_has_complex_connection(dg_node, tz_attr_name);
663677
if (tz_has_conn) {
664678
status = MS::kFailure;
679+
const MString node_name = dag_path.fullPathName();
665680
MMSOLVER_MAYA_WRN("MM Scene Graph: Unsupported attribute connection on "
666681
<< "\"" << tz_attr_name.asChar() << "\": "
667-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
682+
<< "\"" << node_name.asChar() << "\"");
668683
CHECK_MSTATUS_AND_RETURN_IT(status);
669684
}
670685

671686
auto rx_attr_name = MString("rotateX");
672687
auto rx_has_conn = attribute_has_complex_connection(dg_node, rx_attr_name);
673688
if (rx_has_conn) {
674689
status = MS::kFailure;
690+
const MString node_name = dag_path.fullPathName();
675691
MMSOLVER_MAYA_WRN("MM Scene Graph: Unsupported attribute connection on "
676692
<< "\"" << rx_attr_name.asChar() << "\": "
677-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
693+
<< "\"" << node_name.asChar() << "\"");
678694
CHECK_MSTATUS_AND_RETURN_IT(status);
679695
}
680696

681697
auto ry_attr_name = MString("rotateY");
682698
auto ry_has_conn = attribute_has_complex_connection(dg_node, ry_attr_name);
683699
if (ry_has_conn) {
684700
status = MS::kFailure;
701+
const MString node_name = dag_path.fullPathName();
685702
MMSOLVER_MAYA_WRN("MM Scene Graph: Unsupported attribute connection on "
686703
<< "\"" << ry_attr_name.asChar() << "\": "
687-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
704+
<< "\"" << node_name.asChar() << "\"");
688705
CHECK_MSTATUS_AND_RETURN_IT(status);
689706
}
690707

691708
auto rz_attr_name = MString("rotateZ");
692709
auto rz_has_conn = attribute_has_complex_connection(dg_node, rz_attr_name);
693710
if (rz_has_conn) {
694711
status = MS::kFailure;
712+
const MString node_name = dag_path.fullPathName();
695713
MMSOLVER_MAYA_WRN("MM Scene Graph: Unsupported attribute connection on "
696714
<< "\"" << rz_attr_name.asChar() << "\": "
697-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
715+
<< "\"" << node_name.asChar() << "\"");
698716
CHECK_MSTATUS_AND_RETURN_IT(status);
699717
}
700718

701719
auto sx_attr_name = MString("scaleX");
702720
auto sx_has_conn = attribute_has_complex_connection(dg_node, sx_attr_name);
703721
if (sx_has_conn) {
704722
status = MS::kFailure;
723+
const MString node_name = dag_path.fullPathName();
705724
MMSOLVER_MAYA_WRN("MM Scene Graph: Unsupported attribute connection on "
706725
<< "\"" << sx_attr_name.asChar() << "\": "
707-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
726+
<< "\"" << node_name.asChar() << "\"");
708727
CHECK_MSTATUS_AND_RETURN_IT(status);
709728
}
710729

711730
auto sy_attr_name = MString("scaleY");
712731
auto sy_has_conn = attribute_has_complex_connection(dg_node, sy_attr_name);
713732
if (sy_has_conn) {
714733
status = MS::kFailure;
734+
const MString node_name = dag_path.fullPathName();
715735
MMSOLVER_MAYA_WRN("MM Scene Graph: Unsupported attribute connection on "
716736
<< "\"" << sy_attr_name.asChar() << "\": "
717-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
737+
<< "\"" << node_name.asChar() << "\"");
718738
CHECK_MSTATUS_AND_RETURN_IT(status);
719739
}
720740

721741
auto sz_attr_name = MString("scaleZ");
722742
auto sz_has_conn = attribute_has_complex_connection(dg_node, sz_attr_name);
723743
if (sz_has_conn) {
724744
status = MS::kFailure;
745+
const MString node_name = dag_path.fullPathName();
725746
MMSOLVER_MAYA_WRN("MM Scene Graph: Unsupported attribute connection on "
726747
<< "\"" << sz_attr_name.asChar() << "\": "
727-
<< "\"" << dag_path.fullPathName().asChar() << "\"");
748+
<< "\"" << node_name.asChar() << "\"");
728749
CHECK_MSTATUS_AND_RETURN_IT(status);
729750
}
730751

src/mmSolver/node/MMLensModel3deNode.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,10 @@ MStatus MMLensModel3deNode::compute(const MPlug &plug, MDataBlock &data) {
649649
}
650650

651651
else {
652+
const MString node_name = name();
652653
MMSOLVER_MAYA_ERR(
653654
"mmlens::LensModelType value is invalid: nodeName="
654-
<< name().asChar()
655+
<< node_name.asChar()
655656
<< " lensModelType=" << static_cast<int>(lensModelType));
656657
status = MS::kFailure;
657658

0 commit comments

Comments
 (0)