From 9a33f5bfb221170e823efd3c33ab5d331baf7c74 Mon Sep 17 00:00:00 2001 From: dholland Date: Sun, 7 Jun 2009 20:13:18 +0000 Subject: sprintf -> snprintf, plus some use of strlcpy/strlcat where appropriate XXX: there's still one sprintf left which will take some hacking to expunge. --- hack/hack.objnam.c | 178 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 107 insertions(+), 71 deletions(-) (limited to 'hack/hack.objnam.c') diff --git a/hack/hack.objnam.c b/hack/hack.objnam.c index f7857918..d2123e45 100644 --- a/hack/hack.objnam.c +++ b/hack/hack.objnam.c @@ -1,4 +1,4 @@ -/* $NetBSD: hack.objnam.c,v 1.8 2009/06/07 18:30:39 dholland Exp $ */ +/* $NetBSD: hack.objnam.c,v 1.9 2009/06/07 20:13:18 dholland Exp $ */ /* * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica, @@ -63,13 +63,13 @@ #include #ifndef lint -__RCSID("$NetBSD: hack.objnam.c,v 1.8 2009/06/07 18:30:39 dholland Exp $"); +__RCSID("$NetBSD: hack.objnam.c,v 1.9 2009/06/07 20:13:18 dholland Exp $"); #endif /* not lint */ #include #include "hack.h" #include "extern.h" -#define Sprintf (void) sprintf +#define Snprintf (void) snprintf #define Strcat (void) strcat #define Strcpy (void) strcpy #define PREFIX 15 @@ -91,7 +91,7 @@ char * sitoa(int a) { static char buf[13]; - Sprintf(buf, (a < 0) ? "%d" : "+%d", a); + Snprintf(buf, sizeof(buf), (a < 0) ? "%d" : "+%d", a); return (buf); } @@ -99,6 +99,7 @@ char * typename(int otyp) { static char buf[BUFSZ]; + size_t bufpos; struct objclass *ocl = &objects[otyp]; const char *an = ocl->oc_name; const char *dn = ocl->oc_descr; @@ -122,26 +123,42 @@ typename(int otyp) Strcpy(buf, an); if (otyp >= TURQUOISE && otyp <= JADE) Strcat(buf, " stone"); - if (un) - Sprintf(eos(buf), " called %s", un); - if (dn) - Sprintf(eos(buf), " (%s)", dn); + if (un) { + bufpos = strlen(buf); + Snprintf(buf+bufpos, sizeof(buf)-bufpos, + " called %s", un); + } + if (dn) { + bufpos = strlen(buf); + Snprintf(buf+bufpos, sizeof(buf)-bufpos, + " (%s)", dn); + } } else { - Strcpy(buf, dn ? dn : an); - if (ocl->oc_olet == GEM_SYM) - Strcat(buf, " gem"); - if (un) - Sprintf(eos(buf), " called %s", un); + strlcpy(buf, dn ? dn : an, sizeof(buf)); + if (ocl->oc_olet == GEM_SYM) { + strlcat(buf, " gem", sizeof(buf)); + } + if (un) { + bufpos = strlen(buf); + Snprintf(buf+bufpos, sizeof(buf)-bufpos, + " called %s", un); + } } return (buf); } /* here for ring/scroll/potion/wand */ - if (nn) - Sprintf(eos(buf), " of %s", an); - if (un) - Sprintf(eos(buf), " called %s", un); - if (dn) - Sprintf(eos(buf), " (%s)", dn); + if (nn) { + bufpos = strlen(buf); + Snprintf(buf+bufpos, sizeof(buf)-bufpos, " of %s", an); + } + if (un) { + bufpos = strlen(buf); + Snprintf(buf+bufpos, sizeof(buf)-bufpos, " called %s", un); + } + if (dn) { + bufpos = strlen(buf); + Snprintf(buf+bufpos, sizeof(buf)-bufpos, " (%s)", dn); + } return (buf); } @@ -149,12 +166,15 @@ char * xname(struct obj *obj) { static char bufr[BUFSZ]; + /* caution: doname() and aobjnam() below "know" these sizes */ char *buf = &(bufr[PREFIX]); /* leave room for "17 -3 " */ + size_t bufmax = sizeof(bufr) - PREFIX; int nn = objects[obj->otyp].oc_name_known; const char *an = objects[obj->otyp].oc_name; const char *dn = objects[obj->otyp].oc_descr; char *un = objects[obj->otyp].oc_uname; int pl = (obj->quan != 1); + if (!obj->dknown && !Blind) obj->dknown = 1;/* %% doesnt belong here */ switch (obj->olet) { @@ -165,10 +185,10 @@ xname(struct obj *obj) break; case TOOL_SYM: if (!nn) { - Strcpy(buf, dn); + strlcpy(buf, dn, bufmax); break; } - Strcpy(buf, an); + strlcpy(buf, an, bufmax); break; case FOOD_SYM: if (obj->otyp == DEAD_HOMUNCULUS && pl) { @@ -193,10 +213,10 @@ xname(struct obj *obj) case ARMOR_SYM: case CHAIN_SYM: case ROCK_SYM: - Strcpy(buf, an); + strlcpy(buf, an, bufmax); break; case BALL_SYM: - Sprintf(buf, "%sheavy iron ball", + Snprintf(buf, bufmax, "%sheavy iron ball", (obj->owt > objects[obj->otyp].oc_weight) ? "very " : ""); break; case POTION_SYM: @@ -210,14 +230,14 @@ xname(struct obj *obj) break; if (un) { Strcat(buf, " called "); - Strcat(buf, un); + strlcat(buf, un, bufmax); } else { Strcat(buf, " of "); - Strcat(buf, an); + strlcat(buf, an, bufmax); } } else { - Strcpy(buf, dn); - Strcat(buf, " potion"); + strlcpy(buf, dn, bufmax); + strlcat(buf, " potion", bufmax); } break; case SCROLL_SYM: @@ -230,34 +250,34 @@ xname(struct obj *obj) break; if (nn) { Strcat(buf, " of "); - Strcat(buf, an); + strlcat(buf, an, bufmax); } else if (un) { Strcat(buf, " called "); - Strcat(buf, un); + strlcat(buf, un, bufmax); } else { Strcat(buf, " labeled "); - Strcat(buf, dn); + strlcat(buf, dn, bufmax); } break; case WAND_SYM: if (!obj->dknown) - Sprintf(buf, "wand"); + Snprintf(buf, bufmax, "wand"); else if (nn) - Sprintf(buf, "wand of %s", an); + Snprintf(buf, bufmax, "wand of %s", an); else if (un) - Sprintf(buf, "wand called %s", un); + Snprintf(buf, bufmax, "wand called %s", un); else - Sprintf(buf, "%s wand", dn); + Snprintf(buf, bufmax, "%s wand", dn); break; case RING_SYM: if (!obj->dknown) - Sprintf(buf, "ring"); + Snprintf(buf, bufmax, "ring"); else if (nn) - Sprintf(buf, "ring of %s", an); + Snprintf(buf, bufmax, "ring of %s", an); else if (un) - Sprintf(buf, "ring called %s", un); + Snprintf(buf, bufmax, "ring called %s", un); else - Sprintf(buf, "%s ring", dn); + Snprintf(buf, bufmax, "%s ring", dn); break; case GEM_SYM: if (!obj->dknown) { @@ -265,15 +285,15 @@ xname(struct obj *obj) break; } if (!nn) { - Sprintf(buf, "%s gem", dn); + Snprintf(buf, bufmax, "%s gem", dn); break; } - Strcpy(buf, an); + strlcpy(buf, an, bufmax); if (obj->otyp >= TURQUOISE && obj->otyp <= JADE) - Strcat(buf, " stone"); + strlcat(buf, " stone", bufmax); break; default: - Sprintf(buf, "glorkum %c (0%o) %u %d", + Snprintf(buf, bufmax, "glorkum %c (0%o) %u %d", obj->olet, obj->olet, obj->otyp, obj->spe); } if (pl) { @@ -294,17 +314,21 @@ xname(struct obj *obj) } p = eos(buf) - 1; if (*p == 's' || *p == 'z' || *p == 'x' || - (*p == 'h' && p[-1] == 's')) - Strcat(buf, "es"); /* boxes */ - else if (*p == 'y' && !strchr(vowels, p[-1])) - Strcpy(p, "ies"); /* rubies, zruties */ - else - Strcat(buf, "s"); + (*p == 'h' && p[-1] == 's')) { + /* boxes */ + strlcat(buf, "es", bufmax); + } else if (*p == 'y' && !strchr(vowels, p[-1])) { + /* rubies, zruties */ + *p = '\0'; + strlcat(buf, "ies", bufmax); + } else { + strlcat(buf, "s", bufmax); + } } nopl: if (obj->onamelth) { - Strcat(buf, " named "); - Strcat(buf, ONAME(obj)); + strlcat(buf, " named ", bufmax); + strlcat(buf, ONAME(obj), bufmax); } return (buf); } @@ -314,8 +338,13 @@ doname(struct obj *obj) { char prefix[PREFIX]; char *bp = xname(obj); + size_t bppos, bpmax; + + /* XXX do this better somehow w/o knowing internals of xname() */ + bpmax = BUFSZ - PREFIX; + if (obj->quan != 1) - Sprintf(prefix, "%u ", obj->quan); + Snprintf(prefix, sizeof(prefix), "%u ", obj->quan); else Strcpy(prefix, "a "); switch (obj->olet) { @@ -325,33 +354,35 @@ doname(struct obj *obj) break; case ARMOR_SYM: if (obj->owornmask & W_ARMOR) - Strcat(bp, " (being worn)"); + strlcat(bp, " (being worn)", bpmax); /* fall into next case */ case WEAPON_SYM: if (obj->known) { - Strcat(prefix, sitoa(obj->spe)); - Strcat(prefix, " "); + strlcat(prefix, sitoa(obj->spe), sizeof(prefix)); + strlcat(prefix, " ", sizeof(prefix)); } break; case WAND_SYM: - if (obj->known) - Sprintf(eos(bp), " (%d)", obj->spe); + if (obj->known) { + bppos = strlen(bp); + Snprintf(bp+bppos, bpmax-bppos, " (%d)", obj->spe); + } break; case RING_SYM: if (obj->owornmask & W_RINGR) - Strcat(bp, " (on right hand)"); + strlcat(bp, " (on right hand)", bpmax); if (obj->owornmask & W_RINGL) - Strcat(bp, " (on left hand)"); + strlcat(bp, " (on left hand)", bpmax); if (obj->known && (objects[obj->otyp].bits & SPEC)) { - Strcat(prefix, sitoa(obj->spe)); - Strcat(prefix, " "); + strlcat(prefix, sitoa(obj->spe), sizeof(prefix)); + strlcat(prefix, " ", sizeof(prefix)); } break; } if (obj->owornmask & W_WEP) - Strcat(bp, " (weapon in hand)"); + strlcat(bp, " (weapon in hand)", bpmax); if (obj->unpaid) - Strcat(bp, " (unpaid)"); + strlcat(bp, " (unpaid)", bpmax); if (!strcmp(prefix, "a ") && strchr(vowels, *bp)) Strcpy(prefix, "an "); bp = strprepend(bp, prefix); @@ -360,12 +391,12 @@ doname(struct obj *obj) /* used only in hack.fight.c (thitu) */ void -setan(const char *str, char *buf) +setan(const char *str, char *buf, size_t bufmax) { if (strchr(vowels, *str)) - Sprintf(buf, "an %s", str); + Snprintf(buf, bufmax, "an %s", str); else - Sprintf(buf, "a %s", str); + Snprintf(buf, bufmax, "a %s", str); } char * @@ -373,20 +404,25 @@ aobjnam(struct obj *otmp, const char *verb) { char *bp = xname(otmp); char prefix[PREFIX]; + size_t bpmax; + + /* XXX do this better somehow w/o knowing internals of xname() */ + bpmax = BUFSZ - PREFIX; + if (otmp->quan != 1) { - Sprintf(prefix, "%u ", otmp->quan); + Snprintf(prefix, sizeof(prefix), "%u ", otmp->quan); bp = strprepend(bp, prefix); } if (verb) { /* verb is given in plural (i.e., without trailing s) */ - Strcat(bp, " "); + strlcat(bp, " ", bpmax); if (otmp->quan != 1) - Strcat(bp, verb); + strlcat(bp, verb, bpmax); else if (!strcmp(verb, "are")) - Strcat(bp, "is"); + strlcat(bp, "is", bpmax); else { - Strcat(bp, verb); - Strcat(bp, "s"); + strlcat(bp, verb, bpmax); + strlcat(bp, "s", bpmax); } } return (bp); -- cgit v1.2.3-56-ge451