Skip to content

Commit 845c3fd

Browse files
committed
Fix null pointer dereference in STEPattributeList operator[]
Improves error handling in STEPattributeList operator[] - return an error attribute and set error state rather than trying to rely on crashing via a NULL deference. That's undefined behavior, so it was not guaranteed - this requires the calling code to check the return, but is *much* cleaner. Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com>
1 parent 6cbc18b commit 845c3fd

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/clstepcore/STEPattributeList.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ STEPattribute & STEPattributeList::operator []( int n ) {
4141
return *( a->attr );
4242
}
4343

44-
// else
44+
// else - error case: return a static error object to avoid undefined behavior
45+
// The error object allows calling code to detect the error condition
46+
static STEPattribute errorAttr;
4547
cerr << "\nERROR in STEP Core library: " << __FILE__ << ":"
4648
<< __LINE__ << "\n" << _POC_ << "\n\n";
47-
return *( STEPattribute * ) 0;
49+
errorAttr.Error().AppendToDetailMsg( "STEPattributeList::operator[] - index out of bounds" );
50+
errorAttr.Error().severity( SEVERITY_BUG );
51+
return errorAttr;
4852
}
4953

5054
int STEPattributeList::list_length() {

0 commit comments

Comments
 (0)