diff --git a/include/extern.h b/include/extern.h index f18a69b75..26e4cb183 100644 --- a/include/extern.h +++ b/include/extern.h @@ -353,6 +353,7 @@ E void FDECL(new_mname, (struct monst *,int)); E void FDECL(free_mname, (struct monst *)); E void FDECL(new_oname, (struct obj *,int)); E void FDECL(free_oname, (struct obj *)); +E const char *FDECL(safe_oname, (struct obj *)); E struct monst *FDECL(christen_monst, (struct monst *,const char *)); E int NDECL(do_mname); E struct obj *FDECL(oname, (struct obj *,const char *)); diff --git a/src/bones.c b/src/bones.c index 8220afb5a..fc51a5188 100644 --- a/src/bones.c +++ b/src/bones.c @@ -68,16 +68,14 @@ boolean restore; /* artifact bookeeping needs to be done during restore; other fixups are done while saving */ if (otmp->oartifact) { - char *tmponame = ""; - if (has_oname(otmp)) tmponame = ONAME(otmp); - if (exist_artifact(otmp->otyp, tmponame) || + if (exist_artifact(otmp->otyp, safe_oname(otmp)) || is_quest_artifact(otmp)) { /* prevent duplicate--revert to ordinary obj */ otmp->oartifact = 0; if (has_oname(otmp)) free_oname(otmp); } else { - artifact_exists(otmp, tmponame, TRUE); + artifact_exists(otmp, safe_oname(otmp), TRUE); } } } else { /* saving */ diff --git a/src/do_name.c b/src/do_name.c index 7e5802c43..fd1f2d98b 100644 --- a/src/do_name.c +++ b/src/do_name.c @@ -268,6 +268,21 @@ struct obj *obj; } } +/* safe_oname() always returns a valid pointer to + * a string, either the pointer to an object's name + * if it has one, or a pointer to an empty string + * if it doesn't. + */ +const char * +safe_oname(obj) +struct obj *obj; +{ + if (has_oname(obj)) + return ONAME(obj); + else + return ""; +} + /* historical note: this returns a monster pointer because it used to allocate a new bigger block of memory to hold the monster and its name */ struct monst * diff --git a/src/objnam.c b/src/objnam.c index 80cf59b54..44252b23f 100644 --- a/src/objnam.c +++ b/src/objnam.c @@ -2942,10 +2942,7 @@ typfnd: && !wizard #endif ) { - char *tnam = ""; - if (has_oname(otmp)) - tnam = ONAME(otmp); - artifact_exists(otmp, tnam, FALSE); + artifact_exists(otmp, safe_oname(otmp), FALSE); obfree(otmp, (struct obj *) 0); otmp = &zeroobj; pline("For a moment, you feel %s in your %s, but it disappears!",