Skip to content

Commit 58732df

Browse files
committed
Use snprintf instead of sprintf. There are probably other string handling modernizations to do here...
1 parent a74c755 commit 58732df

29 files changed

+238
-204
lines changed

src/cldai/sdaiBinary.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,17 @@ Severity SDAI_Binary::ReadBinary( istream & in, ErrorDescriptor * err, int Assig
121121
if( !validDelimiters ) {
122122
err->GreaterSeverity( SEVERITY_WARNING );
123123
if( needDelims )
124-
sprintf( messageBuf,
124+
snprintf( messageBuf, sizeof(messageBuf),
125125
"Binary value missing double quote delimiters.\n" );
126126
else
127-
sprintf( messageBuf,
127+
snprintf( messageBuf, sizeof(messageBuf),
128128
"Mismatched double quote delimiters for binary.\n" );
129129
err->AppendToDetailMsg( messageBuf );
130130
err->AppendToUserMsg( messageBuf );
131131
}
132132
} else {
133133
err->GreaterSeverity( SEVERITY_WARNING );
134-
sprintf( messageBuf, "Invalid binary value.\n" );
134+
snprintf( messageBuf, sizeof(messageBuf), "Invalid binary value.\n" );
135135
err->AppendToDetailMsg( messageBuf );
136136
err->AppendToUserMsg( messageBuf );
137137
}

src/cldai/sdaiEnum.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ Severity SDAI_LOGICAL::ReadEnum( istream & in, ErrorDescriptor * err, int Assign
211211
if( !validDelimiters ) {
212212
err->GreaterSeverity( SEVERITY_WARNING );
213213
if( needDelims )
214-
sprintf( messageBuf,
214+
snprintf( messageBuf, sizeof(messageBuf),
215215
"Enumerated value has invalid period delimiters.\n" );
216216
else
217-
sprintf( messageBuf,
217+
snprintf( messageBuf, sizeof(messageBuf),
218218
"Mismatched period delimiters for enumeration.\n" );
219219
err->AppendToDetailMsg( messageBuf );
220220
err->AppendToUserMsg( messageBuf );
@@ -241,7 +241,7 @@ Severity SDAI_LOGICAL::ReadEnum( istream & in, ErrorDescriptor * err, int Assign
241241
} else {
242242
in.putback( c );
243243
err->GreaterSeverity( SEVERITY_WARNING );
244-
sprintf( messageBuf, "Invalid enumeration value.\n" );
244+
snprintf( messageBuf, sizeof(messageBuf), "Invalid enumeration value.\n" );
245245
err->AppendToDetailMsg( messageBuf );
246246
err->AppendToUserMsg( messageBuf );
247247
}
@@ -489,10 +489,10 @@ Severity SDAI_Enum::ReadEnum( istream & in, ErrorDescriptor * err, int AssignVal
489489
if( !validDelimiters ) {
490490
err->GreaterSeverity( SEVERITY_WARNING );
491491
if( needDelims )
492-
sprintf( messageBuf,
492+
snprintf( messageBuf, sizeof(messageBuf),
493493
"Enumerated value has invalid period delimiters.\n" );
494494
else
495-
sprintf( messageBuf,
495+
snprintf( messageBuf, sizeof(messageBuf),
496496
"Mismatched period delimiters for enumeration.\n" );
497497
err->AppendToDetailMsg( messageBuf );
498498
err->AppendToUserMsg( messageBuf );
@@ -519,7 +519,7 @@ Severity SDAI_Enum::ReadEnum( istream & in, ErrorDescriptor * err, int AssignVal
519519
} else {
520520
in.putback( c );
521521
err->GreaterSeverity( SEVERITY_WARNING );
522-
sprintf( messageBuf, "Invalid enumeration value.\n" );
522+
snprintf( messageBuf, sizeof(messageBuf), "Invalid enumeration value.\n" );
523523
err->AppendToDetailMsg( messageBuf );
524524
err->AppendToUserMsg( messageBuf );
525525
}

src/cleditor/STEPfile.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ int STEPfile::ReadData1( istream & in ) {
528528
} // end while loop
529529

530530
if( _entsNotCreated ) {
531-
sprintf( buf,
531+
snprintf( buf, sizeof(buf),
532532
"STEPfile Reading File: Unable to create %d instances.\n\tIn first pass through DATA section. Check for invalid entity types.\n",
533533
_entsNotCreated );
534534
_error.AppendToUserMsg( buf );
@@ -647,7 +647,7 @@ int STEPfile::ReadData2( istream & in, bool useTechCor ) {
647647
} // end while loop
648648

649649
if( _entsInvalid ) {
650-
sprintf( buf,
650+
snprintf( buf, sizeof(buf),
651651
"%s \n\tTotal instances: %d \n\tInvalid instances: %d \n\tIncomplete instances (includes invalid instances): %d \n\t%s: %d.\n",
652652
"Second pass complete - instance summary:", total_instances,
653653
_entsInvalid, _entsIncomplete, "Warnings",
@@ -1321,7 +1321,7 @@ SDAI_Application_instance * STEPfile::ReadInstance( istream & in, ostream & out,
13211321
}
13221322
} else {
13231323
if( node->CurrState() == completeSE ) {
1324-
sprintf( errbuf, "WARNING in WORKING FILE: changing instance #%d state from completeSE to incompleteSE.\n", fileid );
1324+
snprintf( errbuf, sizeof(errbuf), "WARNING in WORKING FILE: changing instance #%d state from completeSE to incompleteSE.\n", fileid );
13251325
_error.AppendToUserMsg( errbuf );
13261326
if( _fileType != WORKING_SESSION ) {
13271327
node->ChangeState( incompleteSE );
@@ -1643,7 +1643,7 @@ Severity STEPfile::AppendFile( istream * in, bool useTechCor ) {
16431643
}
16441644
SetFileType( WORKING_SESSION );
16451645
} else {
1646-
sprintf( errbuf,
1646+
snprintf( errbuf, sizeof(errbuf),
16471647
"Faulty input at beginning of file. \"ISO-10303-21;\" or"
16481648
" \"STEP_WORKING_SESSION;\" expected. File not read: %s\n",
16491649
( ( FileName().compare( "-" ) == 0 ) ? "standard input" : FileName().c_str() ) );
@@ -1658,14 +1658,14 @@ Severity STEPfile::AppendFile( istream * in, bool useTechCor ) {
16581658
rval = ReadHeader( *in );
16591659
cout << "\nHEADER read:";
16601660
if( rval < SEVERITY_WARNING ) {
1661-
sprintf( errbuf,
1661+
snprintf( errbuf, sizeof(errbuf),
16621662
"Error: non-recoverable error in reading header section. "
16631663
"There were %d errors encountered. Rest of file is ignored.\n",
16641664
_errorCount );
16651665
_error.AppendToUserMsg( errbuf );
16661666
return rval;
16671667
} else if( rval != SEVERITY_NULL ) {
1668-
sprintf( errbuf, " %d ERRORS\t %d WARNINGS\n\n",
1668+
snprintf( errbuf, sizeof(errbuf), " %d ERRORS\t %d WARNINGS\n\n",
16691669
_errorCount, _warningCount );
16701670
cout << errbuf;
16711671
} else {
@@ -1683,7 +1683,7 @@ Severity STEPfile::AppendFile( istream * in, bool useTechCor ) {
16831683

16841684
cout << "\nFIRST PASS complete: " << total_insts
16851685
<< " instances created.\n";
1686-
sprintf( errbuf,
1686+
snprintf( errbuf, sizeof(errbuf),
16871687
" %d ERRORS\t %d WARNINGS\n\n",
16881688
_errorCount, _warningCount );
16891689
cout << errbuf;
@@ -1727,7 +1727,7 @@ Severity STEPfile::AppendFile( istream * in, bool useTechCor ) {
17271727
//check for "ENDSEC;"
17281728
ReadTokenSeparator( *in2 );
17291729
if( total_insts != valid_insts ) {
1730-
sprintf( errbuf, "%d invalid instances in file: %s\n",
1730+
snprintf( errbuf, sizeof(errbuf), "%d invalid instances in file: %s\n",
17311731
total_insts - valid_insts, ( ( FileName().compare( "-" ) == 0 ) ? "standard input" : FileName().c_str() ) );
17321732
_error.AppendToUserMsg( errbuf );
17331733
CloseInputFile( in2 );
@@ -1736,7 +1736,7 @@ Severity STEPfile::AppendFile( istream * in, bool useTechCor ) {
17361736

17371737
cout << "\nSECOND PASS complete: " << valid_insts
17381738
<< " instances valid.\n";
1739-
sprintf( errbuf,
1739+
snprintf( errbuf, sizeof(errbuf),
17401740
" %d ERRORS\t %d WARNINGS\n\n",
17411741
_errorCount, _warningCount );
17421742
_error.AppendToUserMsg( errbuf );

src/cleditor/STEPfile.inline.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ istream * STEPfile::OpenInputFile( const std::string filename ) {
179179
} else {
180180
if( SetFileName( filename ).empty() && ( filename.compare( "-" ) != 0 ) ) {
181181
char msg[BUFSIZ+1];
182-
sprintf( msg, "Unable to find file for input: \'%s\'. File not read.\n", filename.c_str() );
182+
snprintf( msg, sizeof(msg), "Unable to find file for input: \'%s\'. File not read.\n", filename.c_str() );
183183
_error.AppendToUserMsg( msg );
184184
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
185185
return( 0 );
@@ -196,7 +196,7 @@ istream * STEPfile::OpenInputFile( const std::string filename ) {
196196

197197
if( !in || !( in -> good() ) ) {
198198
char msg[BUFSIZ+1];
199-
sprintf( msg, "Unable to open file for input: \'%s\'. File not read.\n", filename.c_str() );
199+
snprintf( msg, sizeof(msg), "Unable to open file for input: \'%s\'. File not read.\n", filename.c_str() );
200200
_error.AppendToUserMsg( msg );
201201
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
202202
return ( 0 );
@@ -231,7 +231,7 @@ ofstream * STEPfile::OpenOutputFile( std::string filename ) {
231231
} else {
232232
if( SetFileName( filename ).empty() ) {
233233
char msg[BUFSIZ+1];
234-
sprintf( msg, "can't find file: %s\nFile not written.\n", filename.c_str() );
234+
snprintf( msg, sizeof(msg), "can't find file: %s\nFile not written.\n", filename.c_str() );
235235
_error.AppendToUserMsg( msg );
236236
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
237237
}

src/clstepcore/STEPaggrEntity.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Severity EntityAggregate::ReadValue( istream & in, ErrorDescriptor * err,
9191
CheckRemainingInput( in, &errdesc, buf, ",)" );
9292

9393
if( errdesc.severity() < SEVERITY_INCOMPLETE ) {
94-
sprintf( errmsg, " index: %d\n", value_cnt );
94+
snprintf( errmsg, sizeof(errmsg), " index: %d\n", value_cnt );
9595
errdesc.PrependToDetailMsg( errmsg );
9696
err->AppendFromErrorArg( &errdesc );
9797
}
@@ -221,7 +221,7 @@ const char * EntityNode::asStr( std::string & s ) {
221221
return "";
222222
} else { // otherwise return entity id
223223
char tmp [64];
224-
sprintf( tmp, "#%d", node->STEPfile_id );
224+
snprintf( tmp, sizeof(tmp), "#%d", node->STEPfile_id );
225225
s = tmp;
226226
}
227227
return const_cast<char *>( s.c_str() );

src/clstepcore/STEPaggrInt.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const char * IntNode::asStr( std::string & s ) {
9696
const char * IntNode::STEPwrite( std::string & s, const char * ) {
9797
char tmp[BUFSIZ+1];
9898
if( value != S_INT_NULL ) {
99-
sprintf( tmp, "%ld", value );
99+
snprintf( tmp, sizeof(tmp), "%ld", value );
100100
s = tmp;
101101
} else {
102102
s.clear();

src/clstepcore/STEPaggrSelect.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Severity SelectAggregate::ReadValue( istream & in, ErrorDescriptor * err,
8787
CheckRemainingInput( in, &errdesc, buf, ",)" );
8888

8989
if( errdesc.severity() < SEVERITY_INCOMPLETE ) {
90-
sprintf( errmsg, " index: %d\n", value_cnt );
90+
snprintf( errmsg, sizeof(errmsg), " index: %d\n", value_cnt );
9191
errdesc.PrependToDetailMsg( errmsg );
9292
err->AppendFromErrorArg( &errdesc );
9393
}

src/clstepcore/STEPaggregate.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Severity STEPaggregate::ReadValue( istream & in, ErrorDescriptor * err,
174174
CheckRemainingInput( in, &errdesc, buf, ",)" );
175175

176176
if( errdesc.severity() < SEVERITY_INCOMPLETE ) {
177-
sprintf( errmsg, " index: %d\n", value_cnt );
177+
snprintf( errmsg, sizeof(errmsg), " index: %d\n", value_cnt );
178178
errdesc.PrependToDetailMsg( errmsg );
179179
err->AppendFromErrorArg( &errdesc );
180180
}

src/clstepcore/STEPattribute.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,15 +1266,15 @@ void STEPattribute::AddErrorInfo() {
12661266
errStr[0] = '\0';
12671267
if( SEVERITY_INPUT_ERROR < _error.severity() &&
12681268
_error.severity() < SEVERITY_NULL ) {
1269-
sprintf( errStr, " Warning: ATTRIBUTE '%s : %s : %d' - ",
1269+
snprintf( errStr, sizeof(errStr), " Warning: ATTRIBUTE '%s : %s : %d' - ",
12701270
Name(), TypeName().c_str(), Type() );
12711271
_error.PrependToDetailMsg( errStr );
12721272
} else if( _error.severity() == SEVERITY_INPUT_ERROR ) {
1273-
sprintf( errStr, " Error: ATTRIBUTE '%s : %s : %d' - ",
1273+
snprintf( errStr, sizeof(errStr), " Error: ATTRIBUTE '%s : %s : %d' - ",
12741274
Name(), TypeName().c_str(), Type() );
12751275
_error.PrependToDetailMsg( errStr );
12761276
} else if( _error.severity() <= SEVERITY_BUG ) {
1277-
sprintf( errStr, " BUG: ATTRIBUTE '%s : %s : %d' - ",
1277+
snprintf( errStr, sizeof(errStr), " BUG: ATTRIBUTE '%s : %s : %d' - ",
12781278
Name(), TypeName().c_str(), Type() );
12791279
_error.PrependToDetailMsg( errStr );
12801280
}
@@ -1302,12 +1302,12 @@ char STEPattribute::SkipBadAttr( istream & in, char * StopChars ) {
13021302
}
13031303
if( in.eof() ) {
13041304
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
1305-
sprintf( errStr, " Error: attribute '%s : %s : %d' - %s.\n",
1305+
snprintf( errStr, sizeof(errStr), " Error: attribute '%s : %s : %d' - %s.\n",
13061306
Name(), TypeName().c_str(), Type(),
13071307
"Unexpected EOF when skipping bad attr value" );
13081308
_error.AppendToDetailMsg( errStr );
13091309
} else {
1310-
sprintf( errStr, " Error: attribute '%s : %s : %d' - %s.\n",
1310+
snprintf( errStr, sizeof(errStr), " Error: attribute '%s : %s : %d' - %s.\n",
13111311
Name(), TypeName().c_str(), Type(), "Invalid value" );
13121312
_error.AppendToDetailMsg( errStr );
13131313
}

src/clstepcore/STEPcomplex.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ void STEPcomplex::CopyAs( SDAI_Application_instance * se ) {
661661
char errStr[BUFSIZ+1];
662662
cerr << "STEPcomplex::CopyAs() called with non-complex entity: "
663663
<< __FILE__ << __LINE__ << "\n" << _POC_ "\n";
664-
sprintf( errStr,
664+
snprintf( errStr, sizeof(errStr),
665665
"STEPcomplex::CopyAs(): %s - entity #%d.\n",
666666
"Programming ERROR - called with non-complex entity",
667667
STEPfile_id );
@@ -702,7 +702,7 @@ SDAI_Application_instance * STEPcomplex::Replicate() {
702702
char errStr[BUFSIZ+1];
703703
cerr << "STEPcomplex::Replicate() name buffer too small: "
704704
<< __FILE__ << __LINE__ << "\n" << _POC_ "\n";
705-
sprintf( errStr,
705+
snprintf( errStr, sizeof(errStr),
706706
"STEPcomplex::Replicate(): %s - entity #%d.\n",
707707
"Programming ERROR - name buffer too small",
708708
STEPfile_id );

0 commit comments

Comments
 (0)