Hi there all. I am writing a little Hangman console-app in c++ to try and learn the language.
It is mostly functional and can be played, but I have this Warning in IDE (vs2019 Community) saying:
"Warning C6386 Buffer overrun while writing to 'IsLetterFound': the writable size is 'GameWord.public: unsigned int __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::length(void)const ()*1' bytes, but '2' bytes might be written. Hangman C:\SomeFolder\RepoFolder\HANGMAN\HANGMAN\GAME.CPP 18 "
Now, this warning has been in my code for a while, but now after closing the game down I SOMETIMEs get a Windows 10 Error box pop-up saying something like "Overflow on stack-based system, this can be malicious/security risk"
I believe the problem is one of two things. Either, it is my array for checking if IsLetterFound (IMPORTANT NOTE: This array started life as a bool array, i then made it int and finally char to see if that fixed the problem. BUT I struggled in initializing this array as it was the first ever array i initialized with a value provided at runtime - this is my top candidate for the problem, but i have no cleu how to fix it!)
Failing that, could it possibly be my use of 'exit(EXIT_SUCCESS)' which i used to force the console to close when the user puts 'no' for PlayingAgain?
The actual Error Popup message only happens sometimes, and happens after closing VS2019, not my app.
I post my game.cpp and game.h in the entirety now. (main.cpp is so basic i left it out):
GAME.H:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
#include <string>
#include <random>
using String = std::string;
using RandGenerator = std::default_random_engine;
using RandDistribution = std::uniform_int_distribution<int>;
class Game
{
public:
Game();
String GetGameWord() const;
void StartNewGame();
String GetRandomGameWord() const;
bool CheckLetterFound(int);
bool SubmitGuess(char);
void DoNextTurn();
void AddHangmanPiece();
void PrintHangmanVisuals() const;
bool AskToPlayAgain();
bool CheckForGameCompletion();
private:
String GameWord;
String Words[4] = { "ALIEN", "BOOK", "PRICKLE", "ICE" };
char* IsLetterFound;
int CurrentHangmanPieces = 0;
static constexpr int HANGMAN_PIECES_MAX = 10;
};
|
GAME.CPP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include "Game.h"
#include <stdlib.h>
Game::Game()
{
StartNewGame();
}
void Game::StartNewGame() {
CurrentHangmanPieces = 0;
std::cout << "Let's Play Hangman!\n\n\n";
GameWord = GetRandomGameWord();
IsLetterFound = new char[GameWord.length()];
for (char i = 0; i < GameWord.length(); i++) {
IsLetterFound[i] = 'n'; // I HAVE THE WARNING, AND GREEN SQUIGGLE HERE!
}
DoNextTurn();
}
String Game::GetGameWord() const
{
return GameWord;
}
String Game::GetRandomGameWord() const
{
RandDistribution randDistribution(0, Words->length() - 1);
RandGenerator randGenerator;
return Words[randDistribution(randGenerator)];
}
bool Game::CheckLetterFound(int Index)
{
if (IsLetterFound[Index] == 'y')
return true;
return false;
}
bool Game::SubmitGuess(char Letter)
{
char Letter_Uppercase = toupper(Letter);
bool Found = false;
for (int i = 0; i < GameWord.length(); i++) {
if (GameWord[i] == Letter_Uppercase) {
Found = true;
IsLetterFound[i] = 'y';
}
}
return Found;
}
void Game::DoNextTurn() {
String DisplayString = "";
for (int i = 0; i < GetGameWord().length(); i++) {
if (CheckLetterFound(i)) {
DisplayString += GetGameWord()[i];
}
else {
DisplayString += "_";
}
DisplayString += " ";
}
char Letter;
std::cout << "***********************************************************" << "\n\n";
std::cout << DisplayString << "\n\n";
std::cout << "Enter a letter: ";
std::cin >> Letter;
bool Result = SubmitGuess(Letter);
if (!Result) {
AddHangmanPiece();
}
else {
if (CheckForGameCompletion()) {
std::cout << "You clevercloggs!\n";
if (AskToPlayAgain()) {
StartNewGame();
}
else {
exit(EXIT_SUCCESS);
}
}
}
PrintHangmanVisuals();
if (CurrentHangmanPieces < HANGMAN_PIECES_MAX) {
DoNextTurn();
}
else {
if (AskToPlayAgain()) {
StartNewGame();
}
else {
exit(EXIT_SUCCESS);
}
}
}
void Game::AddHangmanPiece() {
CurrentHangmanPieces++;
}
void HackyClearScreen() {
for (char i = 0; i < 10; i++) {
std::cout << "\n\n\n\n\n\n\n\n\n\n";
}
}
void Game::PrintHangmanVisuals() const {
// Left method out to save space
}
bool Game::AskToPlayAgain() {
char Response;
std::cout << "\nDo you want to play again? (Y/N) " << "\n";
std::cin >> Response;
char Response_ToLower = tolower(Response);
if (Response_ToLower == 'y') {
return true;
}
else if (Response_ToLower == 'n') {
return false;
}
else {
std::cout << "Invalid response\n";
AskToPlayAgain();
}
}
bool Game::CheckForGameCompletion()
{
for (int i = 0; i < GameWord.length(); i++) {
if (IsLetterFound[i] != 'y') {
return false;
}
}
return true;
}
|
Image to show Visual studio error:
https://pasteboard.co/IpCCZn7.png
If anyone can see my problem and help me I would be massively grateful. Also I realise my Random Number generator code does not work properly also :D