Skip to content

Commit

Permalink
Replace unsafe sprintf in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhj committed Apr 1, 2023
1 parent 3de77ee commit 26b33ed
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cpp/8puzzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,9 @@ bool PuzzleState::IsSameState( PuzzleState &rhs )

void PuzzleState::PrintNodeInfo()
{
char str[100];
sprintf( str, "%c %c %c\n%c %c %c\n%c %c %c\n",
const int strSize = 100;
char str[strSize];
snprintf( str, strSize, "%c %c %c\n%c %c %c\n%c %c %c\n",
tiles[0] + '0',
tiles[1] + '0',
tiles[2] + '0',
Expand Down
5 changes: 3 additions & 2 deletions cpp/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ bool MapSearchNode::IsSameState( MapSearchNode &rhs )

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

cout << str;
}
Expand Down

0 comments on commit 26b33ed

Please sign in to comment.