Skip to content

Commit 26b33ed

Browse files
committed
Replace unsafe sprintf in examples
1 parent 3de77ee commit 26b33ed

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

cpp/8puzzle.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,9 @@ bool PuzzleState::IsSameState( PuzzleState &rhs )
337337

338338
void PuzzleState::PrintNodeInfo()
339339
{
340-
char str[100];
341-
sprintf( str, "%c %c %c\n%c %c %c\n%c %c %c\n",
340+
const int strSize = 100;
341+
char str[strSize];
342+
snprintf( str, strSize, "%c %c %c\n%c %c %c\n%c %c %c\n",
342343
tiles[0] + '0',
343344
tiles[1] + '0',
344345
tiles[2] + '0',

cpp/tests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ bool MapSearchNode::IsSameState( MapSearchNode &rhs )
9797

9898
void MapSearchNode::PrintNodeInfo()
9999
{
100-
char str[100];
101-
sprintf( str, "Node position : (%d,%d)\n", x,y );
100+
const int strSize = 100;
101+
char str[strSize];
102+
snprintf( str, strSize, "Node position : (%d,%d)\n", x,y );
102103

103104
cout << str;
104105
}

0 commit comments

Comments
 (0)