Skip to content

Commit 6c09b5e

Browse files
committed
Fix infinite loop on invalid input type enter
Also converts all spaces to tabs
1 parent 6c040c0 commit 6c09b5e

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

MTADiag/Common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#pragma once
1616

17+
#define NOMINMAX
18+
1719
#include <iostream>
1820
#include <windows.h>
1921
#include <string>

MTADiag/Diag.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ void Diag::Begin ( void )
5959
std::cout << "Checking for missing GTA files, please wait..." << std::endl;
6060

6161
for ( unsigned int i = 0; i < ( sizeof ( fileList ) / sizeof ( fileList[0] ) ); i++ )
62-
{
63-
std::string szFilename = fileList[i].szFilename;
62+
{
63+
std::string szFilename = fileList[i].szFilename;
6464

6565
if ( !( CheckForFile ( GTAPath + szFilename ) ) )
6666
{
6767
std::cout << "Missing GTA file: " << fileList[i].szFilename << std::endl; // output any messed up file
6868
//bQuit = true; // we need to quit since the user's GTA install is probably screwed up
6969
}
7070
std::cout << "\rChecking " << ( i + 1 ) << " out of " << ( sizeof ( fileList ) / sizeof ( fileList[0] ) ) << "...";
71-
}
71+
}
7272
std::cout << std::endl;
7373
if ( bQuit )
7474
{
@@ -83,7 +83,7 @@ void Diag::Begin ( void )
8383
{
8484
std::string MTAVersionTrim = MTAVersion; // copy the MTAVersion string
8585
MTAVersionTrim.resize ( 15 ); // trim the MTAVersion string stored in registry to 15 characters
86-
// the version appropriation HTML has a 15 char string lacking two trailing zeros
86+
// the version appropriation HTML has a 15 char string lacking two trailing zeros
8787
if ( FindInFile ( files[1].c_str(), ( MTAVersionTrim ) ) ) // look for the current MTA version string in it
8888
std::cout << "MTA is up-to-date." << std::endl << std::endl; // we've found it, hooray, we don't need to update MTA
8989
else
@@ -127,9 +127,9 @@ void Diag::Begin ( void )
127127
std::cout << "Checking for modified or nonstandard GTA files, please wait..." << std::endl;
128128

129129
for ( unsigned int i = 0; i < ( sizeof ( fileList ) / sizeof ( fileList[0] ) ); i++ )
130-
{
130+
{
131131
std::string szMd5 = fileList[i].szMd5;
132-
std::string szFilename = fileList[i].szFilename;
132+
std::string szFilename = fileList[i].szFilename;
133133

134134
if ( !( CompareFileMD5 ( szMd5, ( GTAPath + szFilename ) ) ) )
135135
{
@@ -141,7 +141,7 @@ void Diag::Begin ( void )
141141
Log::WriteStringToLog ( "" );
142142
}
143143
std::cout << "\rChecking " << ( i + 1 ) << " out of " << ( sizeof ( fileList ) / sizeof ( fileList[0] ) ) << "...";
144-
}
144+
}
145145
std::cout << std::endl;
146146
#endif
147147
// collect more information and output to log file
@@ -159,11 +159,11 @@ void Diag::Begin ( void )
159159
// write some of MTA's logs to our log
160160
Log::WriteFileToLog ( MTAPath + "\\MTA\\core.log", "core.log" );
161161

162-
// 1.4
162+
// 1.4
163163
Log::WriteFileToLog ( MTAPath + "\\MTA\\logfile.txt", "logfile.txt" );
164164
Log::WriteFileToLog ( MTAPath + "\\MTA\\logfile_old.txt", "logfile_old.txt" );
165165
Log::WriteFileToLog ( MTAPath + "\\MTA\\CEGUI.log", "CEGUI.log" );
166-
// 1.5
166+
// 1.5
167167
Log::WriteFileToLog ( MTAPath + "\\MTA\\logs\\logfile.txt", "logfile.txt" );
168168
Log::WriteFileToLog ( MTAPath + "\\MTA\\logs\\logfile.txt.1", "logfile.txt.1" );
169169
Log::WriteFileToLog ( MTAPath + "\\MTA\\logs\\CEGUI.log", "CEGUI.log" );
@@ -335,14 +335,23 @@ void Diag::UserPickVersion ( void )
335335
std::cout << "[" << i << "] 1." << i << std::endl;
336336
}
337337
// have the user pick between the versions
338+
bool success;
338339
do {
340+
success = true;
339341
std::cout << "Enter version choice: ";
340342
std::cin >> MTAVerChoice;
341343

342-
if ( MTAVersionsInstalled[MTAVerChoice].empty() || MTAVerChoice > CUR_MTA_VERSIONS )
344+
if ( std::cin.fail() ) {
345+
std::cerr << "Invalid choice entered." << std::endl;
346+
std::cin.clear();
347+
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
348+
success = false;
349+
} else if ( MTAVersionsInstalled[MTAVerChoice].empty() || MTAVerChoice > CUR_MTA_VERSIONS ) {
343350
std::cout << "Invalid choice entered." << std::endl;
351+
success = false;
352+
}
344353

345-
} while ( MTAVersionsInstalled[MTAVerChoice].empty() || MTAVerChoice > CUR_MTA_VERSIONS );
354+
} while ( !success );
346355
}
347356

348357
std::string Diag::GetMTAPath ( void )

0 commit comments

Comments
 (0)