From 937355038d2f4b4b3169a0937df0dab63c3dab88 Mon Sep 17 00:00:00 2001 From: nhmall Date: Wed, 23 Nov 2022 17:47:49 -0500 Subject: [PATCH] some coordxy and other conversion warnings When dist2() got changed to use coordxy parameters, a macro that uses it in its definition was overlooked and it had (int) casts in it. That caused a warning about possible data loss when the int then got converted to coordxy for the dist2() call. Give online2() coordxy parameters instead of int, like its bretheren. Avoid a couple of implicit conversion warnings where ints were being assigned to smaller uchar or ints being assigned to smaller short. A couple of signed vs unsigned warnings on some rumor processing. Avoid some signed vs unsigned warnings in mdlib/makedefs where a signed int param eventually got used in an external call that took size_t. Eliminate all of it by just having the outer NetHack routine also take a size_t. Lastly, insert some default C99 alternative time-related code in mdlib/makedefs since asctime() and ctime() are being flagged as deprecated in the upcoming C23 standard and will now start to trigger warnings for anyone using a C23-compliant compiler. --- include/extern.h | 2 +- include/hack.h | 4 ++-- src/hacklib.c | 2 +- src/mdlib.c | 12 ++++++------ sys/msdos/tile2bin.c | 5 +++++ util/makedefs.c | 20 +++++++++++++++----- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/include/extern.h b/include/extern.h index 7814f7572..2a3a92d28 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1028,7 +1028,7 @@ extern int rounddiv(long, int); extern int dist2(coordxy, coordxy, coordxy, coordxy); extern int isqrt(int); extern int distmin(coordxy, coordxy, coordxy, coordxy); -extern boolean online2(int, int, int, int); +extern boolean online2(coordxy, coordxy, coordxy, coordxy); extern boolean pmatch(const char *, const char *); extern boolean pmatchi(const char *, const char *); extern boolean pmatchz(const char *, const char *); diff --git a/include/hack.h b/include/hack.h index f7f1198bc..97b49696f 100644 --- a/include/hack.h +++ b/include/hack.h @@ -709,9 +709,9 @@ enum optset_restrictions { - min((int) greatest_erosion(obj), objects[(obj)->otyp].a_ac)) #define makeknown(x) discover_object((x), TRUE, TRUE) -#define distu(xx, yy) dist2((int) (xx), (int) (yy), (int) u.ux, (int) u.uy) +#define distu(xx, yy) dist2((coordxy) (xx), (coordxy) (yy), (coordxy) u.ux, (coordxy) u.uy) #define mdistu(mon) distu((mon)->mx, (mon)->my) -#define onlineu(xx, yy) online2((int)(xx), (int)(yy), (int) u.ux, (int) u.uy) +#define onlineu(xx, yy) online2((coordxy)(xx), (coordxy)(yy), (coordxy) u.ux, (coordxy) u.uy) #define rn1(x, y) (rn2(x) + (y)) diff --git a/src/hacklib.c b/src/hacklib.c index fbbb55f3e..38fd5a9c2 100644 --- a/src/hacklib.c +++ b/src/hacklib.c @@ -721,7 +721,7 @@ isqrt(int val) /* are two points lined up (on a straight line)? */ boolean -online2(int x0, int y0, int x1, int y1) +online2(coordxy x0, coordxy y0, coordxy x1, coordxy y1) { int dx = x0 - x1, dy = y0 - y1; /* If either delta is zero then they're on an orthogonal line, diff --git a/src/mdlib.c b/src/mdlib.c index 4e06e86d4..df3eb84d4 100644 --- a/src/mdlib.c +++ b/src/mdlib.c @@ -55,8 +55,8 @@ static boolean date_via_env = FALSE; extern unsigned long md_ignored_features(void); char *mdlib_version_string(char *, const char *); -char *version_id_string(char *, int, const char *); -char *bannerc_string(char *, int, const char *); +char *version_id_string(char *, size_t, const char *); +char *bannerc_string(char *, size_t, const char *); int case_insensitive_comp(const char *, const char *); static void make_version(void); @@ -300,7 +300,7 @@ RESTORE_WARNING_FORMAT_NONLITERAL #endif /* MAKEDEFS_C */ char * -version_id_string(char *outbuf, int bufsz, const char *build_date) +version_id_string(char *outbuf, size_t bufsz, const char *build_date) { char subbuf[64], versbuf[64]; char statusbuf[64]; @@ -333,7 +333,7 @@ version_id_string(char *outbuf, int bufsz, const char *build_date) /* still within #if MAKDEFS_C || FOR_RUNTIME */ char * -bannerc_string(char *outbuf, int bufsz, const char *build_date) +bannerc_string(char *outbuf, size_t bufsz, const char *build_date) { char subbuf[64], versbuf[64]; @@ -814,10 +814,10 @@ case_insensitive_comp(const char *s1, const char *s2) for (;; s1++, s2++) { u1 = (uchar) *s1; if (isupper(u1)) - u1 = tolower(u1); + u1 = (uchar) tolower(u1); u2 = (uchar) *s2; if (isupper(u2)) - u2 = tolower(u2); + u2 = (uchar) tolower(u2); if (u1 == '\0' || u1 != u2) break; } diff --git a/sys/msdos/tile2bin.c b/sys/msdos/tile2bin.c index d8b97da1a..90a7ebf43 100644 --- a/sys/msdos/tile2bin.c +++ b/sys/msdos/tile2bin.c @@ -190,7 +190,12 @@ main(int argc, char *argv[]) #endif strncpy(tibheader.ident, "NetHack 3.7 MSDOS Port binary tile file", 80); +#if !defined(NOSTRFTIME) + if (!strftime(tibheader.timestamp, + sizeof tibheader.timestamp, "%c", newtime)) +#else strncpy(tibheader.timestamp, asctime(newtime), 24); +#endif tibheader.timestamp[25] = '\0'; tibheader.tilecount = tilecount; tibheader.numcolors = num_colors; diff --git a/util/makedefs.c b/util/makedefs.c index e70f0e16e..0bff69a7f 100644 --- a/util/makedefs.c +++ b/util/makedefs.c @@ -1077,11 +1077,11 @@ do_rumors(void) true_rumor_size, true_rumor_offset, false_rumor_count, false_rumor_size, false_rumor_offset, eof_offset); /* record the current position; true rumors will start here */ - true_rumor_offset = ftell(tfp); + true_rumor_offset = (unsigned long) ftell(tfp); - false_rumor_offset = read_rumors_file(".tru", &true_rumor_count, - &true_rumor_size, true_rumor_offset, - MD_PAD_RUMORS); + false_rumor_offset = (unsigned long) read_rumors_file(".tru", &true_rumor_count, + &true_rumor_size, true_rumor_offset, + MD_PAD_RUMORS); if (!false_rumor_offset) goto rumors_failure; @@ -1241,11 +1241,21 @@ do_date(void) (unsigned long) clocktim); (void) fflush(stderr); } +#if !defined(NOSTRFTIME) + if (!strftime(cbuf, sizeof cbuf, "%c", gmtime(&clocktim))) + cbuf[0] = '\0'; +#else Strcpy(cbuf, asctime(gmtime(&clocktim))); +#endif /* NOSTRFTIME */ } #else /* ordinary build: use current date+time */ +#if !defined(NOSTRFTIME) + if (!strftime(cbuf, sizeof cbuf, "%c", localtime(&clocktim))) + cbuf[0] = '\0'; +#else Strcpy(cbuf, ctime(&clocktim)); +#endif /* NOSTRFTIME */ #endif /* REPRODUCIBLE_BUILD */ if ((c = strchr(cbuf, '\n')) != 0) @@ -1979,7 +1989,7 @@ do_objs(void) for (i = 0; !i || objects[i].oc_class != ILLOBJ_CLASS; i++) { SpinCursor(3); - objects[i].oc_name_idx = objects[i].oc_descr_idx = i; /* init */ + objects[i].oc_name_idx = objects[i].oc_descr_idx = (short) i; if (!(objnam = tmpdup(OBJ_NAME(objects[i])))) continue;