Add FITSint() and FITSuint(),
which cast long long to int while panicking on overflow
This commit is contained in:
+5
-1
@@ -889,7 +889,7 @@ extern void reveal_paths(void);
|
|||||||
extern boolean read_tribute(const char *, const char *, int, char *, int,
|
extern boolean read_tribute(const char *, const char *, int, char *, int,
|
||||||
unsigned);
|
unsigned);
|
||||||
extern boolean Death_quote(char *, int);
|
extern boolean Death_quote(char *, int);
|
||||||
extern void livelog_add(long, const char *);
|
extern void livelog_add(long ll_type, const char *);
|
||||||
|
|
||||||
/* ### fountain.c ### */
|
/* ### fountain.c ### */
|
||||||
|
|
||||||
@@ -1031,6 +1031,10 @@ extern void shuffle_int_array(int *, int);
|
|||||||
nh_snprintf(__func__, __LINE__, str, size, __VA_ARGS__)
|
nh_snprintf(__func__, __LINE__, str, size, __VA_ARGS__)
|
||||||
extern void nh_snprintf(const char *func, int line, char *str, size_t size,
|
extern void nh_snprintf(const char *func, int line, char *str, size_t size,
|
||||||
const char *fmt, ...) PRINTF_F(5, 6);
|
const char *fmt, ...) PRINTF_F(5, 6);
|
||||||
|
#define FITSint(x) FITSint_(x, __func__, __LINE__)
|
||||||
|
extern int FITSint_(long long, const char *, int);
|
||||||
|
#define FITSuint(x) FITSuint_(x, __func__, __LINE__)
|
||||||
|
extern unsigned FITSuint_(unsigned long long, const char *, int);
|
||||||
|
|
||||||
/* ### insight.c ### */
|
/* ### insight.c ### */
|
||||||
|
|
||||||
|
|||||||
+6
-1
@@ -8,6 +8,10 @@
|
|||||||
/* since this file is also used in auxiliary programs, don't include all the
|
/* since this file is also used in auxiliary programs, don't include all the
|
||||||
function declarations for all of nethack */
|
function declarations for all of nethack */
|
||||||
#define EXTERN_H /* comment line for pre-compiled headers */
|
#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"
|
#include "config.h"
|
||||||
|
|
||||||
char *fmt_ptr(const genericptr);
|
char *fmt_ptr(const genericptr);
|
||||||
@@ -147,7 +151,8 @@ nhdupstr(const char *string, const char *file, int line)
|
|||||||
char *
|
char *
|
||||||
dupstr(const char *string)
|
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 */
|
/* similar for reasonable size strings, but return the length of the input as well */
|
||||||
|
|||||||
+1
-2
@@ -1039,8 +1039,7 @@ menualpha_cmp(const genericptr vptr1, const genericptr vptr2)
|
|||||||
int
|
int
|
||||||
parse_cond_option(boolean negated, char *opts)
|
parse_cond_option(boolean negated, char *opts)
|
||||||
{
|
{
|
||||||
int i;
|
int i, sl;
|
||||||
size_t sl;
|
|
||||||
const char *compareto, *uniqpart, prefix[] = "cond_";
|
const char *compareto, *uniqpart, prefix[] = "cond_";
|
||||||
|
|
||||||
if (!opts || strlen(opts) <= sizeof prefix - 1)
|
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 */
|
/* without extern.h via hack.h, these haven't been declared for us */
|
||||||
extern FILE *fopen_datafile(const char *, const char *, int);
|
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
|
#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)
|
if (lp->rev > DLB_MAX_VERS || lp->rev < DLB_MIN_VERS)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
lp->dir = (libdir *) alloc(lp->nentries * sizeof(libdir));
|
lp->dir = (libdir *) alloc(FITSuint(lp->nentries * sizeof(libdir)));
|
||||||
lp->sspace = (char *) alloc(lp->strsize);
|
lp->sspace = (char *) alloc(FITSuint(lp->strsize));
|
||||||
|
|
||||||
/* read in each directory entry */
|
/* read in each directory entry */
|
||||||
for (i = 0, sp = lp->sspace; i < lp->nentries; i++) {
|
for (i = 0, sp = lp->sspace; i < lp->nentries; i++) {
|
||||||
|
|||||||
@@ -1360,4 +1360,21 @@ nh_snprintf(
|
|||||||
|
|
||||||
RESTORE_WARNING_FORMAT_NONLITERAL
|
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*/
|
/*hacklib.c*/
|
||||||
|
|||||||
+2
-2
@@ -1399,7 +1399,7 @@ nhl_loadlua(lua_State *L, const char *fname)
|
|||||||
dlb_fseek(fh, 0L, SEEK_SET);
|
dlb_fseek(fh, 0L, SEEK_SET);
|
||||||
|
|
||||||
/* extra +1: room to add final '\n' if missing */
|
/* 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';
|
buf[0] = '\0';
|
||||||
bufin = bufout = buf;
|
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
|
* in use, and fseek(SEEK_END) only yields an upper bound on
|
||||||
* the actual amount of data in that situation.]
|
* 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;
|
break;
|
||||||
buflen -= cnt; /* set up for next iteration, if any */
|
buflen -= cnt; /* set up for next iteration, if any */
|
||||||
if (cnt == 0L) {
|
if (cnt == 0L) {
|
||||||
|
|||||||
+2
-2
@@ -6697,7 +6697,6 @@ msgtype_parse_add(char *str)
|
|||||||
|
|
||||||
for (i = 0; i < SIZE(msgtype_names); i++)
|
for (i = 0; i < SIZE(msgtype_names); i++)
|
||||||
if (streq(msgtype_names[i].name, msgtype, TRUE)) {
|
if (streq(msgtype_names[i].name, msgtype, TRUE)) {
|
||||||
//if (!strncmpi(msgtype_names[i].name, msgtype, strlen(msgtype))) {
|
|
||||||
typ = msgtype_names[i].msgtyp;
|
typ = msgtype_names[i].msgtyp;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -8468,7 +8467,8 @@ set_option_mod_status(const char *optnam, int status)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (k = 0; allopt[k].name; k++) {
|
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;
|
allopt[k].setwhere = status;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
static void xexit(int) NORETURN;
|
static void xexit(int) NORETURN;
|
||||||
char *eos(char *); /* also used by dlb.c */
|
char *eos(char *); /* also used by dlb.c */
|
||||||
FILE *fopen_datafile(const char *, const char *);
|
FILE *fopen_datafile(const char *, const char *);
|
||||||
|
unsigned FITSuint_(unsigned long long, const char *, int);
|
||||||
|
|
||||||
#ifdef DLB
|
#ifdef DLB
|
||||||
#ifdef DLBLIB
|
#ifdef DLBLIB
|
||||||
@@ -543,4 +544,14 @@ xexit(int retcd)
|
|||||||
/*NOTREACHED*/
|
/*NOTREACHED*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* In hacklib.c, but we don't have that and it calls panic() */
|
||||||
|
unsigned
|
||||||
|
FITSuint_(unsigned long long i, const char *file, int line){
|
||||||
|
unsigned ret = (unsigned)i;
|
||||||
|
if (ret != i) {
|
||||||
|
printf("Overflow at %s:%d\n", file, line);
|
||||||
|
xexit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
return (unsigned)i;
|
||||||
|
}
|
||||||
/*dlb_main.c*/
|
/*dlb_main.c*/
|
||||||
|
|||||||
@@ -171,6 +171,8 @@ static boolean use_enum = TRUE;
|
|||||||
extern unsigned _stklen = STKSIZ;
|
extern unsigned _stklen = STKSIZ;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
unsigned FITSuint_(unsigned long long, const char *, int);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Some of the routines in this source file were moved into .../src/mdlib
|
* Some of the routines in this source file were moved into .../src/mdlib
|
||||||
* to facilitate the use of a cross-compiler generation of some of the
|
* to facilitate the use of a cross-compiler generation of some of the
|
||||||
@@ -2349,4 +2351,14 @@ struct attribs attrmax, attrmin;
|
|||||||
#endif
|
#endif
|
||||||
#endif /* STRICT_REF_DEF */
|
#endif /* STRICT_REF_DEF */
|
||||||
|
|
||||||
|
/* In hacklib.c, but we don't have that and it calls panic() */
|
||||||
|
unsigned
|
||||||
|
FITSuint_(unsigned long long i, const char *file, int line){
|
||||||
|
unsigned ret = (unsigned)i;
|
||||||
|
if (ret != i) {
|
||||||
|
Fprintf(stdout, "Overflow at %s:%d\n", file, line);
|
||||||
|
makedefs_exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
return (unsigned)i;
|
||||||
|
}
|
||||||
/*makedefs.c*/
|
/*makedefs.c*/
|
||||||
|
|||||||
Reference in New Issue
Block a user