Skip to content

Commit

Permalink
Fix #6898: Replace atoi() with strtoul()
Browse files Browse the repository at this point in the history
Normalize type and parsing of generation_seed across all files
Add assert_compile() to ensure correct type
  • Loading branch information
MiguelHorta authored and orudge committed Oct 11, 2018
1 parent fbfa4eb commit e00908f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/genworld.h
Expand Up @@ -22,7 +22,7 @@ enum LandscapeGenerator {
LG_TERRAGENESIS = 1, ///< TerraGenesis Perlin landscape generator
};

static const uint GENERATE_NEW_SEED = UINT_MAX; ///< Create a new random seed
static const uint32 GENERATE_NEW_SEED = UINT32_MAX; ///< Create a new random seed

/** Modes for GenerateWorld */
enum GenWorldMode {
Expand Down Expand Up @@ -97,7 +97,7 @@ void SetGeneratingWorldProgress(GenWorldProgress cls, uint total);
void IncreaseGeneratingWorldProgress(GenWorldProgress cls);
void PrepareGenerateWorldProgress();
void ShowGenerateWorldProgress();
void StartNewGameWithoutGUI(uint seed);
void StartNewGameWithoutGUI(uint32 seed);
void ShowCreateScenario();
void StartScenarioEditor();

Expand Down
2 changes: 1 addition & 1 deletion src/genworld_gui.cpp
Expand Up @@ -870,7 +870,7 @@ void StartScenarioEditor()
* Start a normal game without the GUI.
* @param seed The seed of the new game.
*/
void StartNewGameWithoutGUI(uint seed)
void StartNewGameWithoutGUI(uint32 seed)
{
/* GenerateWorld takes care of the possible GENERATE_NEW_SEED value in 'seed' */
_settings_newgame.game_creation.generation_seed = seed;
Expand Down
6 changes: 4 additions & 2 deletions src/openttd.cpp
Expand Up @@ -385,7 +385,7 @@ void OpenBrowser(const char *url)
/** Callback structure of statements to be executed after the NewGRF scan. */
struct AfterNewGRFScan : NewGRFScanCallback {
Year startyear; ///< The start year.
uint generation_seed; ///< Seed for the new game.
uint32 generation_seed; ///< Seed for the new game.
char *dedicated_host; ///< Hostname for the dedicated server.
uint16 dedicated_port; ///< Port for the dedicated server.
char *network_conn; ///< Information about the server to connect to, or NULL.
Expand All @@ -394,6 +394,8 @@ struct AfterNewGRFScan : NewGRFScanCallback {
bool *save_config_ptr; ///< The pointer to the save config setting.
bool save_config; ///< The save config setting.

assert_compile(sizeof(generation_seed) == sizeof(_settings_game.game_creation.generation_seed));

This comment has been minimized.

Copy link
@nielsmh

nielsmh Oct 26, 2018

Contributor

This line fails to compile on Visual C++ 2015 Update 3. AfterNewGRFScan::generation_seed is considered undeclared at this point (for some reason), but placing the static assert inside a function body makes it valid.

This comment has been minimized.

Copy link
@LordAro

LordAro Oct 26, 2018

Member

Interesting, I can imagine it failing if it were using one of the old definitions of assert_compile, but it should be using the proper static_assert function...

OpenTTD/src/stdafx.h

Lines 356 to 357 in eff09c4

#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
#define assert_compile(expr) static_assert(expr, #expr )

This comment has been minimized.

Copy link
@nielsmh

nielsmh Oct 26, 2018

Contributor
3>..\src\openttd.cpp(397): error C2327: 'AfterNewGRFScan::generation_seed': is not a type name, static, or enumerator
3>..\src\openttd.cpp(397): error C2065: 'generation_seed': undeclared identifier
3>..\src\openttd.cpp(397): error C2338: sizeof(generation_seed) == sizeof(_settings_game.game_creation.generation_seed)

/**
* Create a new callback.
* @param save_config_ptr Pointer to the save_config local variable which
Expand Down Expand Up @@ -666,7 +668,7 @@ int openttd_main(int argc, char *argv[])

goto exit_noshutdown;
}
case 'G': scanner->generation_seed = atoi(mgo.opt); break;
case 'G': scanner->generation_seed = strtoul(mgo.opt, NULL, 10); break;
case 'c': free(_config_file); _config_file = stredup(mgo.opt); break;
case 'x': scanner->save_config = false; break;
case 'h':
Expand Down

0 comments on commit e00908f

Please sign in to comment.