diff options
author | dolorous <dolorous@c06c8d41-db1a-0410-9941-cceddc491573> | 2008-06-25 19:47:37 +0000 |
---|---|---|
committer | dolorous <dolorous@c06c8d41-db1a-0410-9941-cceddc491573> | 2008-06-25 19:47:37 +0000 |
commit | e3b9a7a1e14dd81cace9e2241679c5a92eba6006 (patch) | |
tree | e3f48acadb2eb14be1cd282b11496622be261e4a /crawl-ref/source | |
parent | eef749ceb30bf5694dfe06646fde3f2493dc1fa0 (diff) | |
download | crawl-ref-e3b9a7a1e14dd81cace9e2241679c5a92eba6006.tar.gz crawl-ref-e3b9a7a1e14dd81cace9e2241679c5a92eba6006.zip |
For functions that return char*'s, don't return a c_str() of an
std::string, since as soon as the function returns, the std::string goes
out of scope, and the c_str() becomes a dangling pointer, which usually
points to the same area as before, but occasionally points to garbage.
Instead, make them return std::string's, and call c_str() on the return
value outside the functions. Among other things, this should fix
[1999515].
Note that I've only fixed direct c_str() returns for now. There might
be some indirect ones that I missed.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@6139 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source')
-rw-r--r-- | crawl-ref/source/delay.cc | 12 | ||||
-rw-r--r-- | crawl-ref/source/initfile.cc | 4 | ||||
-rw-r--r-- | crawl-ref/source/initfile.h | 6 | ||||
-rw-r--r-- | crawl-ref/source/macro.cc | 8 | ||||
-rw-r--r-- | crawl-ref/source/macro.h | 2 | ||||
-rw-r--r-- | crawl-ref/source/output.cc | 17 | ||||
-rw-r--r-- | crawl-ref/source/religion.cc | 10 | ||||
-rw-r--r-- | crawl-ref/source/stuff.cc | 6 | ||||
-rw-r--r-- | crawl-ref/source/xom.cc | 63 |
9 files changed, 64 insertions, 64 deletions
diff --git a/crawl-ref/source/delay.cc b/crawl-ref/source/delay.cc index d215c38e39..eb9393d4f4 100644 --- a/crawl-ref/source/delay.cc +++ b/crawl-ref/source/delay.cc @@ -211,13 +211,13 @@ static int _recite_to_monsters(int x, int y, int pow, int unused) return (1); } -static const char* _get_recite_speech(const std::string key, int weight) +static std::string _get_recite_speech(const std::string key, int weight) { seed_rng( weight + you.x_pos + you.y_pos); const std::string str = getSpeakString("zin_recite_speech_" + key); if (!str.empty()) - return (str.c_str()); + return (str); // in case nothing is found if (key == "start") @@ -429,7 +429,7 @@ void stop_delay( bool stop_stair_travel ) case DELAY_RECITE: mprf(MSGCH_PLAIN, "You stop %s.", - _get_recite_speech("other", you.num_turns + delay.duration)); + _get_recite_speech("other", you.num_turns + delay.duration).c_str()); _pop_delay(); break; @@ -683,7 +683,7 @@ void handle_delay( void ) break; case DELAY_RECITE: mprf(MSGCH_PLAIN, "You %s", - _get_recite_speech("start", you.num_turns + delay.duration)); + _get_recite_speech("start", you.num_turns + delay.duration).c_str()); if (apply_area_visible(_recite_to_monsters, delay.parm1)) viewwindow(true, false); @@ -890,7 +890,7 @@ void handle_delay( void ) break; case DELAY_RECITE: mprf(MSGCH_MULTITURN_ACTION, "You continue %s.", - _get_recite_speech("other", you.num_turns + delay.duration+1)); + _get_recite_speech("other", you.num_turns + delay.duration+1).c_str()); if (apply_area_visible(_recite_to_monsters, delay.parm1)) viewwindow(true, false); @@ -1034,7 +1034,7 @@ static void _finish_delay(const delay_queue_item &delay) case DELAY_RECITE: mprf(MSGCH_PLAIN, "You finish %s.", - _get_recite_speech("other", you.num_turns + delay.duration)); + _get_recite_speech("other", you.num_turns + delay.duration).c_str()); break; case DELAY_PASSWALL: diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc index c16b6427f3..9891bf14ad 100644 --- a/crawl-ref/source/initfile.cc +++ b/crawl-ref/source/initfile.cc @@ -118,12 +118,12 @@ const std::string cols[16] = "lightred", "lightmagenta", "yellow", "white" }; -const char* colour_to_str(unsigned char colour) +const std::string colour_to_str(unsigned char colour) { if ( colour >= 16 ) return "lightgrey"; else - return cols[colour].c_str(); + return cols[colour]; } // Returns -1 if unmatched else returns 0-15. diff --git a/crawl-ref/source/initfile.h b/crawl-ref/source/initfile.h index b04b3f3850..f949f94fc8 100644 --- a/crawl-ref/source/initfile.h +++ b/crawl-ref/source/initfile.h @@ -26,9 +26,9 @@ enum drop_mode_type }; god_type str_to_god(std::string god); -int str_to_colour( const std::string &str, int default_colour = -1, - bool accept_number = true ); -const char* colour_to_str( unsigned char colour ); +int str_to_colour(const std::string &str, int default_colour = -1, + bool accept_number = true); +const std::string colour_to_str(unsigned char colour); // last updated 12may2000 {dlb} /* *********************************************************************** diff --git a/crawl-ref/source/macro.cc b/crawl-ref/source/macro.cc index 2f2ad22ff3..5f2542bf39 100644 --- a/crawl-ref/source/macro.cc +++ b/crawl-ref/source/macro.cc @@ -101,16 +101,16 @@ static bool is_userfunction(const keyseq &seq) return (userfunc_index(seq) != -1); } -const char *get_userfunction(int key) +std::string get_userfunction(int key) { int index = userfunc_index(key); - return (index == -1? NULL : userfunctions[index].c_str()); + return (index == -1 ? NULL : userfunctions[index]); } -static const char *get_userfunction(const keyseq &seq) +static std::string get_userfunction(const keyseq &seq) { int index = userfunc_index(seq); - return (index == -1? NULL : userfunctions[index].c_str()); + return (index == -1 ? NULL : userfunctions[index]); } static bool userfunc_referenced(int index, const macromap &mm) diff --git a/crawl-ref/source/macro.h b/crawl-ref/source/macro.h index 1405cb7e19..35b2514a51 100644 --- a/crawl-ref/source/macro.h +++ b/crawl-ref/source/macro.h @@ -74,7 +74,7 @@ void macro_buf_add(const keyseq &actions, bool reverse = false ); bool is_userfunction(int key); bool is_synthetic_key(int key); -const char *get_userfunction(int key); +std::string get_userfunction(int key); void add_key_recorder(key_recorder* recorder); void remove_key_recorder(key_recorder* recorder); diff --git a/crawl-ref/source/output.cc b/crawl-ref/source/output.cc index 6074856306..7da5db1f69 100644 --- a/crawl-ref/source/output.cc +++ b/crawl-ref/source/output.cc @@ -1005,7 +1005,7 @@ static bool _mons_hostile(const monsters *mon) return (!mons_friendly(mon) && !mons_neutral(mon)); } -static const char* _get_monster_name(const monsters *mon, bool list_a = false) +static std::string _get_monster_name(const monsters *mon, bool list_a = false) { std::string desc = ""; @@ -1043,7 +1043,8 @@ static const char* _get_monster_name(const monsters *mon, bool list_a = false) desc += ")"; } } - return desc.c_str(); + + return (desc); } // Returns true if the first monster is more aggressive (in terms of @@ -1113,11 +1114,11 @@ static void _get_visible_monsters(std::vector<std::string>& describe) if (i > 0 && _compare_monsters_attitude(mons[i-1], mons[i])) { if (count == 1) - describe.push_back(_get_monster_name(mons[i-1], true)); + describe.push_back(_get_monster_name(mons[i-1], true).c_str()); else { describe.push_back(number_in_words(count) + " " - + pluralise(_get_monster_name(mons[i-1]))); + + pluralise(_get_monster_name(mons[i-1]).c_str())); } count = 0; } @@ -1127,12 +1128,12 @@ static void _get_visible_monsters(std::vector<std::string>& describe) if (mons.size() == 1 || _compare_monsters_attitude(mons[size-2], mons[size-1])) { - describe.push_back(_get_monster_name(mons[size-1], true)); + describe.push_back(_get_monster_name(mons[size-1], true).c_str()); } else { describe.push_back(number_in_words(count) + " " - + pluralise(_get_monster_name(mons[size-1]))); + + pluralise(_get_monster_name(mons[size-1]).c_str())); } } @@ -1621,7 +1622,7 @@ static void _print_overview_screen_equip(column_composer& cols, { const int item_idx = you.equip[e_order[i]]; const item_def& item = you.inv[item_idx]; - const char* colname = colour_to_str(item.colour); + const char* colname = colour_to_str(item.colour).c_str(); const char equip_char = index_to_letter(item_idx); char buf2[50]; @@ -1802,7 +1803,7 @@ static std::vector<formatted_string> _get_overview_stats() else { snprintf(god_colour_tag, sizeof god_colour_tag, "<%s>", - colour_to_str(god_colour(you.religion))); + colour_to_str(god_colour(you.religion)).c_str()); // piety rankings int prank = piety_rank() - 1; if (prank < 0 || you.religion == GOD_XOM) diff --git a/crawl-ref/source/religion.cc b/crawl-ref/source/religion.cc index 5d05bd6a9f..3f8fb9e2c1 100644 --- a/crawl-ref/source/religion.cc +++ b/crawl-ref/source/religion.cc @@ -4532,12 +4532,12 @@ static bool _beogh_followers_abandon_you() } // currently only used when orcish idols have been destroyed -static const char* _get_beogh_speech(const std::string key) +static std::string _get_beogh_speech(const std::string key) { std::string result = getSpeakString("Beogh " + key); if (!result.empty()) - return (result.c_str()); + return (result); return ("Beogh is angry!"); } @@ -4555,11 +4555,11 @@ void beogh_idol_revenge() const char *revenge; if (you.religion == GOD_BEOGH) - revenge = _get_beogh_speech("idol follower"); + revenge = _get_beogh_speech("idol follower").c_str(); else if (you.species == SP_HILL_ORC) - revenge = _get_beogh_speech("idol hill orc"); + revenge = _get_beogh_speech("idol hill orc").c_str(); else - revenge = _get_beogh_speech("idol other"); + revenge = _get_beogh_speech("idol other").c_str(); god_smites_you(GOD_BEOGH, KILLED_BY_BEOGH_SMITING, revenge); diff --git a/crawl-ref/source/stuff.cc b/crawl-ref/source/stuff.cc index 92ce879e89..352fda180c 100644 --- a/crawl-ref/source/stuff.cc +++ b/crawl-ref/source/stuff.cc @@ -1057,7 +1057,7 @@ static std::string _list_alternative_yes(char yes1, char yes2, return help; } -static const char* _list_allowed_keys(char yes1, char yes2, +static std::string _list_allowed_keys(char yes1, char yes2, bool lowered = false, bool allow_all = false) { @@ -1069,7 +1069,7 @@ static const char* _list_allowed_keys(char yes1, char yes2, result += (lowered ? "/n/q" : "/N/Q"); result += "]"; - return (result.c_str()); + return (result); } // Like yesno(), but returns 0 for no, 1 for yes, and -1 for quit. @@ -1083,7 +1083,7 @@ int yesnoquit( const char* str, bool safe, int safeanswer, bool allow_all, std::string prompt = make_stringf("%s%s ", str ? str : "Buggy prompt?", _list_allowed_keys(alt_yes, alt_yes2, - safe, allow_all)); + safe, allow_all).c_str()); while (true) { mpr(prompt.c_str(), MSGCH_PROMPT); diff --git a/crawl-ref/source/xom.cc b/crawl-ref/source/xom.cc index 4d76aca3e7..34fda175be 100644 --- a/crawl-ref/source/xom.cc +++ b/crawl-ref/source/xom.cc @@ -104,7 +104,7 @@ const char *describe_xom_favour() : "A beloved plaything of Xom."; } -static const char *_get_xom_speech(const std::string key) +static std::string _get_xom_speech(const std::string key) { std::string result = getSpeakString("Xom " + key); @@ -114,8 +114,7 @@ static const char *_get_xom_speech(const std::string key) if (result.empty()) return ("Xom makes something happen."); -// mprf(MSGCH_DIAGNOSTICS, "Xom speech result: %s", result.c_str()); - return (result.c_str()); + return (result); } bool xom_is_nice() @@ -191,7 +190,7 @@ static void _xom_makes_you_cast_random_spell(int sever) const spell_type spell = _xom_spells[random2(spellenum)]; - god_speaks(GOD_XOM, _get_xom_speech("spell effect")); + god_speaks(GOD_XOM, _get_xom_speech("spell effect").c_str()); #if DEBUG_DIAGNOSTICS || DEBUG_RELIGION || DEBUG_XOM mprf(MSGCH_DIAGNOSTICS, @@ -264,7 +263,7 @@ static bool _xom_annoyance_gift(int power) { // If you are wielding a cursed item then Xom will give you // an item of that same type. Ha ha! - god_speaks(GOD_XOM, _get_xom_speech("cursed gift")); + god_speaks(GOD_XOM, _get_xom_speech("cursed gift").c_str()); if (coinflip()) // For added humour, give the same sub-type. _xom_make_item(weapon->base_type, weapon->sub_type, power * 3); @@ -278,7 +277,7 @@ static bool _xom_annoyance_gift(int power) { // If you are wearing cursed gloves, then Xom will give you // a ring. Ha ha! - god_speaks(GOD_XOM, _get_xom_speech("cursed gift")); + god_speaks(GOD_XOM, _get_xom_speech("cursed gift").c_str()); _xom_make_item(OBJ_JEWELLERY, get_random_ring_type(), power * 3); return (true); }; @@ -288,7 +287,7 @@ static bool _xom_annoyance_gift(int power) { // If you are wearing a cursed amulet, then Xom will give // you an amulet. Ha ha! - god_speaks(GOD_XOM, _get_xom_speech("cursed gift")); + god_speaks(GOD_XOM, _get_xom_speech("cursed gift").c_str()); _xom_make_item(OBJ_JEWELLERY, get_random_amulet_type(), power * 3); return (true); }; @@ -300,7 +299,7 @@ static bool _xom_annoyance_gift(int power) { // If you are wearing a cursed ring, then Xom will give you // a ring. Ha ha! - god_speaks(GOD_XOM, _get_xom_speech("ring gift")); + god_speaks(GOD_XOM, _get_xom_speech("ring gift").c_str()); _xom_make_item(OBJ_JEWELLERY, get_random_ring_type(), power * 3); return (true); } @@ -309,7 +308,7 @@ static bool _xom_annoyance_gift(int power) { // Xom will give you a wielded item of a type different from // what you are currently wielding. - god_speaks(GOD_XOM, _get_xom_speech("weapon gift")); + god_speaks(GOD_XOM, _get_xom_speech("weapon gift").c_str()); const object_class_type objtype = _get_unrelated_wield_class(weapon->base_type); @@ -335,7 +334,7 @@ static bool _xom_gives_item(int power) { // If you are wearing a cursed cloak, then Xom will give you a // cloak or body armour. Ha ha! - god_speaks(GOD_XOM, _get_xom_speech("armour gift")); + god_speaks(GOD_XOM, _get_xom_speech("armour gift").c_str()); _xom_make_item(OBJ_ARMOUR, random2(10) ? get_random_body_armour_type(you.your_level * 2) @@ -344,7 +343,7 @@ static bool _xom_gives_item(int power) return (true); } - god_speaks(GOD_XOM, _get_xom_speech("general gift")); + god_speaks(GOD_XOM, _get_xom_speech("general gift").c_str()); // There are two kinds of Xom gifts: acquirement and random object. // The result from acquirement is very good (usually as good or @@ -448,7 +447,7 @@ static bool _xom_is_good(int sever) if (pot != POT_BERSERK_RAGE || you.can_go_berserk(false)) { - god_speaks(GOD_XOM, _get_xom_speech("potion effect")); + god_speaks(GOD_XOM, _get_xom_speech("potion effect").c_str()); if (pot == POT_BERSERK_RAGE) you.berserk_penalty = NO_BERSERK_PENALTY; @@ -506,11 +505,11 @@ static bool _xom_is_good(int sever) if (success) { if (numdifferent == numdemons) - god_speaks(GOD_XOM, _get_xom_speech("multiple holy summons")); + god_speaks(GOD_XOM, _get_xom_speech("multiple holy summons").c_str()); else if (numdifferent > 0) - god_speaks(GOD_XOM, _get_xom_speech("multiple mixed summons")); + god_speaks(GOD_XOM, _get_xom_speech("multiple mixed summons").c_str()); else - god_speaks(GOD_XOM, _get_xom_speech("multiple summons")); + god_speaks(GOD_XOM, _get_xom_speech("multiple summons").c_str()); // If we have only non-demons, there's a chance that they // may be hostile. @@ -550,7 +549,7 @@ static bool _xom_is_good(int sever) // This can fail with radius 1, or in open areas. if (vitrify_area(random2avg(sever / 2, 3) + 1)) { - god_speaks(GOD_XOM, _get_xom_speech("vitrification")); + god_speaks(GOD_XOM, _get_xom_speech("vitrification").c_str()); done = true; } @@ -587,9 +586,9 @@ static bool _xom_is_good(int sever) if (summons != -1) { if (different) - god_speaks(GOD_XOM, _get_xom_speech("single holy summon")); + god_speaks(GOD_XOM, _get_xom_speech("single holy summon").c_str()); else - god_speaks(GOD_XOM, _get_xom_speech("single summon")); + god_speaks(GOD_XOM, _get_xom_speech("single summon").c_str()); player_angers_monster(&menv[summons]); @@ -605,7 +604,7 @@ static bool _xom_is_good(int sever) if (mon) { - god_speaks(GOD_XOM, _get_xom_speech("good monster polymorph")); + god_speaks(GOD_XOM, _get_xom_speech("good monster polymorph").c_str()); monster_polymorph(mon, RANDOM_MONSTER, mons_wont_attack(mon) ? PPT_MORE : PPT_LESS); @@ -633,7 +632,7 @@ static bool _xom_is_good(int sever) if (you.can_safely_mutate() && player_mutation_level(MUT_MUTATION_RESISTANCE) < 3) { - god_speaks(GOD_XOM, _get_xom_speech("good mutations")); + god_speaks(GOD_XOM, _get_xom_speech("good mutations").c_str()); mpr("Your body is suffused with distortional energy."); @@ -678,9 +677,9 @@ static bool _xom_is_good(int sever) if (summons != -1) { if (different) - god_speaks(GOD_XOM, _get_xom_speech("single major holy summon")); + god_speaks(GOD_XOM, _get_xom_speech("single major holy summon").c_str()); else - god_speaks(GOD_XOM, _get_xom_speech("single major demon summon")); + god_speaks(GOD_XOM, _get_xom_speech("single major demon summon").c_str()); player_angers_monster(&menv[summons]); @@ -732,7 +731,7 @@ static bool _xom_is_bad(int sever) { if (random2(sever) <= 2) { - god_speaks(GOD_XOM, _get_xom_speech("zero miscast effect")); + god_speaks(GOD_XOM, _get_xom_speech("zero miscast effect").c_str()); miscast_effect(SPTYP_RANDOM, 0, 0, 0, "the mischief of Xom"); @@ -740,7 +739,7 @@ static bool _xom_is_bad(int sever) } else if (random2(sever) <= 3) { - god_speaks(GOD_XOM, _get_xom_speech("minor miscast effect")); + god_speaks(GOD_XOM, _get_xom_speech("minor miscast effect").c_str()); miscast_effect(SPTYP_RANDOM, 0, 0, random2(2), "the capriciousness of Xom"); @@ -749,7 +748,7 @@ static bool _xom_is_bad(int sever) } else if (random2(sever) <= 4) { - god_speaks(GOD_XOM, _get_xom_speech("lose stats")); + god_speaks(GOD_XOM, _get_xom_speech("lose stats").c_str()); lose_stat(STAT_RANDOM, 1 + random2(3), true, "the capriciousness of Xom" ); @@ -758,7 +757,7 @@ static bool _xom_is_bad(int sever) } else if (random2(sever) <= 5) { - god_speaks(GOD_XOM, _get_xom_speech("medium miscast effect")); + god_speaks(GOD_XOM, _get_xom_speech("medium miscast effect").c_str()); miscast_effect(SPTYP_RANDOM, 0, 0, random2(3), "the capriciousness of Xom"); @@ -770,7 +769,7 @@ static bool _xom_is_bad(int sever) if (you.can_safely_mutate() && player_mutation_level(MUT_MUTATION_RESISTANCE) < 3) { - god_speaks(GOD_XOM, _get_xom_speech("random mutations")); + god_speaks(GOD_XOM, _get_xom_speech("random mutations").c_str()); mpr("Your body is suffused with distortional energy."); @@ -797,7 +796,7 @@ static bool _xom_is_bad(int sever) if (mon) { - god_speaks(GOD_XOM, _get_xom_speech("bad monster polymorph")); + god_speaks(GOD_XOM, _get_xom_speech("bad monster polymorph").c_str()); monster_polymorph(mon, RANDOM_MONSTER, mons_wont_attack(mon) ? PPT_LESS : PPT_MORE); @@ -816,7 +815,7 @@ static bool _xom_is_bad(int sever) } else if (random2(sever) <= 8) { - const char *speech = _get_xom_speech("draining or torment"); + const char *speech = _get_xom_speech("draining or torment").c_str(); if (one_chance_in(4)) { @@ -847,7 +846,7 @@ static bool _xom_is_bad(int sever) } else if (random2(sever) <= 9) { - const char *speech = _get_xom_speech("hostile monster"); + const char *speech = _get_xom_speech("hostile monster").c_str(); // Nasty, but fun. if (one_chance_in(4)) @@ -886,7 +885,7 @@ static bool _xom_is_bad(int sever) } else if (random2(sever) <= 10) { - god_speaks(GOD_XOM, _get_xom_speech("major miscast effect")); + god_speaks(GOD_XOM, _get_xom_speech("major miscast effect").c_str()); miscast_effect(SPTYP_RANDOM, 0, 0, random2(4), "the severe capriciousness of Xom"); @@ -895,7 +894,7 @@ static bool _xom_is_bad(int sever) } else if (one_chance_in(sever) && (you.level_type != LEVEL_ABYSS)) { - god_speaks(GOD_XOM, _get_xom_speech("banishment")); + god_speaks(GOD_XOM, _get_xom_speech("banishment").c_str()); banished(DNGN_ENTER_ABYSS, "Xom"); |