Add FITSint() and FITSuint(),
which cast long long to int while panicking on overflow
This commit is contained in:
@@ -8,6 +8,10 @@
|
||||
/* since this file is also used in auxiliary programs, don't include all the
|
||||
function declarations for all of nethack */
|
||||
#define EXTERN_H /* comment line for pre-compiled headers */
|
||||
/* but we need this one */
|
||||
#define FITSuint(x) FITSuint_(x, __func__, __LINE__)
|
||||
extern unsigned FITSuint_(unsigned long long, const char *, int);
|
||||
|
||||
#include "config.h"
|
||||
|
||||
char *fmt_ptr(const genericptr);
|
||||
@@ -147,7 +151,8 @@ nhdupstr(const char *string, const char *file, int line)
|
||||
char *
|
||||
dupstr(const char *string)
|
||||
{
|
||||
return strcpy((char *) alloc(strlen(string) + 1), string);
|
||||
unsigned len = FITSuint(strlen(string));
|
||||
return strcpy((char *) alloc(len + 1), string);
|
||||
}
|
||||
|
||||
/* similar for reasonable size strings, but return the length of the input as well */
|
||||
|
||||
@@ -1039,8 +1039,7 @@ menualpha_cmp(const genericptr vptr1, const genericptr vptr2)
|
||||
int
|
||||
parse_cond_option(boolean negated, char *opts)
|
||||
{
|
||||
int i;
|
||||
size_t sl;
|
||||
int i, sl;
|
||||
const char *compareto, *uniqpart, prefix[] = "cond_";
|
||||
|
||||
if (!opts || strlen(opts) <= sizeof prefix - 1)
|
||||
|
||||
@@ -36,6 +36,10 @@ char dlbfilename[MAX_DLB_FILENAME];
|
||||
|
||||
/* without extern.h via hack.h, these haven't been declared for us */
|
||||
extern FILE *fopen_datafile(const char *, const char *, int);
|
||||
#define FITSuint(x) FITSuint_((x), __func__, __LINE__)
|
||||
/* implementation will be in either dlb_main.c or the core */
|
||||
extern unsigned FITSuint_(unsigned long long, const char *, int);
|
||||
|
||||
|
||||
#ifdef DLBLIB
|
||||
/*
|
||||
@@ -132,8 +136,8 @@ readlibdir(library *lp) /* library pointer to fill in */
|
||||
if (lp->rev > DLB_MAX_VERS || lp->rev < DLB_MIN_VERS)
|
||||
return FALSE;
|
||||
|
||||
lp->dir = (libdir *) alloc(lp->nentries * sizeof(libdir));
|
||||
lp->sspace = (char *) alloc(lp->strsize);
|
||||
lp->dir = (libdir *) alloc(FITSuint(lp->nentries * sizeof(libdir)));
|
||||
lp->sspace = (char *) alloc(FITSuint(lp->strsize));
|
||||
|
||||
/* read in each directory entry */
|
||||
for (i = 0, sp = lp->sspace; i < lp->nentries; i++) {
|
||||
|
||||
@@ -1360,4 +1360,21 @@ nh_snprintf(
|
||||
|
||||
RESTORE_WARNING_FORMAT_NONLITERAL
|
||||
|
||||
/* cast to int or panic on overflow; use via macro */
|
||||
int
|
||||
FITSint_(lua_Integer i, const char *file, int line){
|
||||
int ret = (int)i;
|
||||
if (ret != i)
|
||||
panic("Overflow at %s:%d", file, line);
|
||||
return (int)i;
|
||||
}
|
||||
|
||||
unsigned
|
||||
FITSuint_(unsigned long long i, const char *file, int line){
|
||||
unsigned ret = (unsigned)i;
|
||||
if (ret != i)
|
||||
panic("Overflow at %s:%d", file, line);
|
||||
return (unsigned)i;
|
||||
}
|
||||
|
||||
/*hacklib.c*/
|
||||
|
||||
@@ -1399,7 +1399,7 @@ nhl_loadlua(lua_State *L, const char *fname)
|
||||
dlb_fseek(fh, 0L, SEEK_SET);
|
||||
|
||||
/* extra +1: room to add final '\n' if missing */
|
||||
buf = bufout = (char *) alloc(buflen + 1 + 1);
|
||||
buf = bufout = (char *) alloc(FITSint(buflen + 1 + 1));
|
||||
buf[0] = '\0';
|
||||
bufin = bufout = buf;
|
||||
|
||||
@@ -1416,7 +1416,7 @@ nhl_loadlua(lua_State *L, const char *fname)
|
||||
* in use, and fseek(SEEK_END) only yields an upper bound on
|
||||
* the actual amount of data in that situation.]
|
||||
*/
|
||||
if ((cnt = dlb_fread(bufin, 1, min(buflen, LOADCHUNKSIZE), fh)) < 0L)
|
||||
if ((cnt = dlb_fread(bufin, 1, min((int)buflen, LOADCHUNKSIZE), fh)) < 0L)
|
||||
break;
|
||||
buflen -= cnt; /* set up for next iteration, if any */
|
||||
if (cnt == 0L) {
|
||||
|
||||
@@ -6697,7 +6697,6 @@ msgtype_parse_add(char *str)
|
||||
|
||||
for (i = 0; i < SIZE(msgtype_names); i++)
|
||||
if (streq(msgtype_names[i].name, msgtype, TRUE)) {
|
||||
//if (!strncmpi(msgtype_names[i].name, msgtype, strlen(msgtype))) {
|
||||
typ = msgtype_names[i].msgtyp;
|
||||
break;
|
||||
}
|
||||
@@ -8468,7 +8467,8 @@ set_option_mod_status(const char *optnam, int status)
|
||||
return;
|
||||
}
|
||||
for (k = 0; allopt[k].name; k++) {
|
||||
if (!strncmpi(allopt[k].name, optnam, strlen(optnam))) {
|
||||
if (streq(allopt[k].name, optnam, TRUE)) {
|
||||
//if (!strncmpi(allopt[k].name, optnam, strlen(optnam))) {
|
||||
allopt[k].setwhere = status;
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user