reformat lots of the CRASHREPORT code

Replace tabs, split 'if (condtion) do_something' across two lines,
insert lots of spaces in things like 'if(condition){'.  I changed
a lot of C++ style comments to traditional C style, but left quite
a few of those as-is.

This also rewrites the code that pull request #1216 purports to fix.
I still can't make sense of the original and the patched edition.

Supersedes #1216
Closes #1216
This commit is contained in:
PatR
2024-03-09 14:25:38 -05:00
committed by nhkeni
parent ed5ef46109
commit 42d5d6a453
+80 -52
View File
@@ -1,4 +1,4 @@
/* NetHack 3.7 end.c $NHDT-Date: 1702023265 2023/12/08 08:14:25 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.285 $ */ /* NetHack 3.7 end.c $NHDT-Date: 1709597568 2024/03/05 00:12:48 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.305 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2012. */ /*-Copyright (c) Robert Patrick Rankin, 2012. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -256,7 +256,8 @@ NH_abort(char *why USED_FOR_CRASHREPORT)
if (!binfile || !*binfile) { \ if (!binfile || !*binfile) { \
/* If this triggers, investigate CFBundleGetMainBundle */ \ /* If this triggers, investigate CFBundleGetMainBundle */ \
/* or CFBundleCopyExecutableURL. */ \ /* or CFBundleCopyExecutableURL. */ \
raw_print("BETA warning: crashreport_init called without useful info"); \ raw_print( \
"BETA warning: crashreport_init called without useful info"); \
goto skip; \ goto skip; \
} }
# else # else
@@ -281,7 +282,7 @@ NH_abort(char *why USED_FOR_CRASHREPORT)
# define HASH_OFLAGS O_RDONLY # define HASH_OFLAGS O_RDONLY
# define HASH_BINFILE_DECL char binfile[PATH_MAX+1]; # define HASH_BINFILE_DECL char binfile[PATH_MAX+1];
# define HASH_BINFILE() \ # define HASH_BINFILE() \
int len = readlink("/proc/self/exe", binfile, sizeof(binfile)-1); \ int len = readlink("/proc/self/exe", binfile, sizeof binfile - 1); \
if (len > 0) { \ if (len > 0) { \
binfile[len] = '\0'; \ binfile[len] = '\0'; \
} else { \ } else { \
@@ -305,19 +306,24 @@ NH_abort(char *why USED_FOR_CRASHREPORT)
goto skip; \ goto skip; \
} }
# endif // WIN32 # endif // WIN32
// Binary ID - Use only as a hint to contact.html for recognizing our own
// binaries. This is easily spoofed! /* Binary ID - Use only as a hint to contact.html for recognizing our own
binaries. This is easily spoofed! */
static char bid[40]; static char bid[40];
/* ARGSUSED */ /* ARGSUSED */
void void
crashreport_init(int argc UNUSED, char *argv[] UNUSED){ crashreport_init(int argc UNUSED, char *argv[] UNUSED)
static int once=0; if(once++) return; // NetHackW.exe calls us twice {
static int once = 0;
if (once++) /* NetHackW.exe calls us twice */
return;
HASH_BINFILE_DECL; HASH_BINFILE_DECL;
HASH_PRAGMA_START HASH_PRAGMA_START
HASH_CONTEXTPTR(ctxp); HASH_CONTEXTPTR(ctxp);
if(HASH_INIT(ctxp)) goto skip; if (HASH_INIT(ctxp))
HASH_BINFILE(); // Does "goto skip" on error. goto skip;
HASH_BINFILE(); /* Does "goto skip" on error. */
int fd = open(binfile, HASH_OFLAGS, 0); int fd = open(binfile, HASH_OFLAGS, 0);
if (fd == -1) { if (fd == -1) {
@@ -330,28 +336,39 @@ crashreport_init(int argc UNUSED, char *argv[] UNUSED){
int segsize; int segsize;
unsigned char segment[4096]; unsigned char segment[4096];
while (0 < (segsize = read(fd, segment,sizeof(segment)))) { while (0 < (segsize = read(fd, segment, sizeof segment))) {
if(HASH_UPDATE(ctxp, segment, segsize)) goto skip; if (HASH_UPDATE(ctxp, segment, segsize))
goto skip;
} }
if (segsize < 0) { if (segsize < 0) {
close(fd); close(fd);
goto skip; goto skip;
} }
if(HASH_FINISH(ctxp)) goto skip; if (HASH_FINISH(ctxp))
goto skip;
close(fd); close(fd);
static const char hex[] = "0123456789abcdef";
char *p = bid; char *p = bid;
unsigned char *in; unsigned char *in;
HASH_RESULT(ctxp, &in); HASH_RESULT(ctxp, &in);
/* Just in case, make sure not to overflow the bid buffer. */ uint8 cnt = (uint8) HASH_RESULT_SIZE(ctxp);
char cnt=min(HASH_RESULT_SIZE(ctxp), (sizeof(bid)-1)); /* Just in case, make sure not to overflow the bid buffer.
while (cnt--) { Divide size by 2 because each octet in the hash uses two slots
p += snprintf(p, HASH_RESULT_SIZE(ctxp) - (p - bid), "%02x", *(in++)); in bid[] when formatted as a pair of hexadecimal digits. */
if (cnt >= (uint8) sizeof bid / 2)
cnt = (uint8) sizeof bid / 2 - 1;
while (cnt) {
/* sprintf(p, "%02x", *in++), p += 2; */
*p++ = hex[(*in >> 4) & 0x0f];
*p++ = hex[*in++ & 0x0f];
--cnt;
} }
*p = '\0'; *p = '\0';
return; return;
skip: skip:
strncpy((char *)bid,"unknown",sizeof(bid)-1); Strcpy(bid, "unknown");
HASH_CLEANUP(ctxp); HASH_CLEANUP(ctxp);
HASH_PRAGMA_END HASH_PRAGMA_END
} }
@@ -368,7 +385,8 @@ skip:
#undef HASH_BINFILE #undef HASH_BINFILE
void void
crashreport_bidshow(void){ crashreport_bidshow(void)
{
#if defined(WIN32) && !defined(WIN32CON) #if defined(WIN32) && !defined(WIN32CON)
if (0 == win32_cr_helper('D', ctxp, bid, 0)) if (0 == win32_cr_helper('D', ctxp, bid, 0))
#endif #endif
@@ -400,7 +418,8 @@ crashreport_bidshow(void){
#define SWR_ADD(str) \ #define SWR_ADD(str) \
utmp = strlen(str); \ utmp = strlen(str); \
mark = uend; \ mark = uend; \
if(utmp >= urem) goto full; \ if (utmp >= urem) \
goto full; \
strncpy(uend, str, utmp); \ strncpy(uend, str, utmp); \
uend += utmp; urem -= utmp; \ uend += utmp; urem -= utmp; \
*uend = '\0'; *uend = '\0';
@@ -409,19 +428,19 @@ crashreport_bidshow(void){
// want to roll back to the last SWR_ADD, update mark before // want to roll back to the last SWR_ADD, update mark before
// calling this macro. // calling this macro.
#define SWR_ADD_URIcoded(str) \ #define SWR_ADD_URIcoded(str) \
if(swr_add_uricoded(str, &uend, &urem, mark))goto full; if (swr_add_uricoded(str, &uend, &urem, mark)) \
goto full;
// On overflow, truncate to markp (but only if markp != NULL). /* On overflow, truncate to markp (but only if markp != NULL). */
boolean boolean
swr_add_uricoded(const char *in, char **out, int *remaining, char *markp){ swr_add_uricoded(
const char *in,
char **out,
int *remaining,
char *markp)
{
while (*in) { while (*in) {
if( if (isalnum(*in) || strchr("_-.~", *in)) {
isalnum(*in) ||
*in == '_' ||
*in == '-' ||
*in == '.' ||
*in == '~' // ||
){
**out = *in; **out = *in;
(*out)++; (*out)++;
(*remaining)--; (*remaining)--;
@@ -431,7 +450,8 @@ swr_add_uricoded(const char *in, char **out, int *remaining, char *markp){
(*remaining)--; (*remaining)--;
} else { } else {
if (*remaining <= 3) { if (*remaining <= 3) {
if(markp) *out = markp, *remaining = 0; if (markp)
*out = markp, *remaining = 0;
**out = '\0'; **out = '\0';
return TRUE; return TRUE;
} }
@@ -441,13 +461,14 @@ swr_add_uricoded(const char *in, char **out, int *remaining, char *markp){
} }
in++; in++;
if (!*remaining) { if (!*remaining) {
if(markp) *out = markp, *remaining = 0; if (markp)
*out = markp, *remaining = 0;
**out = '\0'; **out = '\0';
return TRUE; return TRUE;
} }
**out = '\0'; **out = '\0';
} }
return FALSE; // normal return return FALSE; /* normal return */
} }
static char url[MAX_URL]; // XXX too bad this isn't allocated as needed static char url[MAX_URL]; // XXX too bad this isn't allocated as needed
@@ -457,30 +478,32 @@ static int utmp; // used inside macros
static char *mark; // holds previous terminator (generally) static char *mark; // holds previous terminator (generally)
boolean boolean
submit_web_report(int cos, const char *msg, const char *why){ submit_web_report(int cos, const char *msg, const char *why)
{
urem = (gc.crash_urlmax < 0 || gc.crash_urlmax > MAX_URL) urem = (gc.crash_urlmax < 0 || gc.crash_urlmax > MAX_URL)
? MAX_URL : min(MAX_URL,gc.crash_urlmax); ? MAX_URL : min(MAX_URL,gc.crash_urlmax);
char temp[200]; char temp[200];
char temp2[200]; char temp2[200];
int countpp=0; // pre and post traceback lines int countpp = 0; /* pre and post traceback lines */
// URL loaded for creating reports to the NetHack DevTeam // URL loaded for creating reports to the NetHack DevTeam
// CRASHREPORTURL=https://nethack.org/links/cr-37BETA.html // CRASHREPORTURL=https://nethack.org/links/cr-37BETA.html
if(!sysopt.crashreporturl) return FALSE; if (!sysopt.crashreporturl)
return FALSE;
SWR_ADD(sysopt.crashreporturl); SWR_ADD(sysopt.crashreporturl);
/* cos - operation, v - version */ /* cos - operation, v - version */
snprintf(temp, sizeof(temp), "?cos=%d&v=1",cos); Snprintf(temp, sizeof temp, "?cos=%d&v=1", cos);
SWR_ADD(temp); SWR_ADD(temp);
/* msg==NULL for #bugreport */ /* msg==NULL for #bugreport */
if (msg) { if (msg) {
SWR_ADD("&subject="); SWR_ADD("&subject=");
snprintf(temp, sizeof(temp), "%s report for NetHack %s", Snprintf(temp, sizeof temp, "%s report for NetHack %s",
msg, version_string(temp2, sizeof(temp2))); msg, version_string(temp2, sizeof temp2 ));
SWR_ADD_URIcoded(temp); SWR_ADD_URIcoded(temp);
} }
SWR_ADD("&gitver="); SWR_ADD("&gitver=");
SWR_ADD_URIcoded(getversionstring(temp2, sizeof(temp2))); SWR_ADD_URIcoded(getversionstring(temp2, sizeof temp2));
if (gc.crash_name) { if (gc.crash_name) {
SWR_ADD("&name="); SWR_ADD("&name=");
@@ -522,15 +545,15 @@ submit_web_report(int cos, const char *msg, const char *why){
count = backtrace(bt, SIZE(bt)); count = backtrace(bt, SIZE(bt));
info = backtrace_symbols(bt, count); info = backtrace_symbols(bt, count);
for (x = 0; x < count; x++) { for (x = 0; x < count; x++) {
copynchars(temp, info[x], (int) sizeof temp - 1 - 1); // \n\0 copynchars(temp, info[x], (int) sizeof temp - 1 - 1); /* \n\0 */
/* try to remove up to 16 blank spaces by removing 8 twice */ /* try to remove up to 16 blank spaces by removing 8 twice */
(void) strsubst(temp, " ", ""); (void) strsubst(temp, " ", "");
(void) strsubst(temp, " ", ""); (void) strsubst(temp, " ", "");
strncat(temp, "\n", sizeof temp - 1); (void) strncat(temp, "\n", sizeof temp - 1);
# if 0 // __linux__ # if 0 // __linux__
// not needed for MacOS // not needed for MacOS
// XXX is it actually needed for linux? TBD // XXX is it actually needed for linux? TBD
snprintf(temp2, sizeof(temp2), "[%02lu]\n", (unsigned long) x); Snprintf(temp2, sizeof temp2, "[%02lu]\n", (unsigned long) x);
uend--; // remove the \n we added above uend--; // remove the \n we added above
SWR_ADD_URIcoded(temp2); SWR_ADD_URIcoded(temp2);
# endif // linux # endif // linux
@@ -549,7 +572,8 @@ submit_web_report(int cos, const char *msg, const char *why){
countpp++; countpp++;
for (k = 0; k < 5; k++) { for (k = 0; k < 5; k++) {
const char *line = get_saved_pline(k); const char *line = get_saved_pline(k);
if(!line) break; if (!line)
break;
SWR_ADD_URIcoded(line); SWR_ADD_URIcoded(line);
SWR_ADD_URIcoded("\n"); SWR_ADD_URIcoded("\n");
countpp++; countpp++;
@@ -561,10 +585,11 @@ submit_web_report(int cos, const char *msg, const char *why){
// detailrows: Guess since we can't know the // detailrows: Guess since we can't know the
// width of the window. // width of the window.
SWR_ADD("&detailrows="); SWR_ADD("&detailrows=");
(void)snprintf(temp,sizeof(temp),"%d",min(count+countpp,30)); Snprintf(temp, sizeof temp, "%d", min(count + countpp, 30));
SWR_ADD_URIcoded(temp); SWR_ADD_URIcoded(temp);
full: ; full:
;
//printf("URL=%ld '%s'\n",strlen(url),url); //printf("URL=%ld '%s'\n",strlen(url),url);
#ifdef WIN32 #ifdef WIN32
int *rv = win32_cr_shellexecute(url); int *rv = win32_cr_shellexecute(url);
@@ -611,10 +636,11 @@ printf("ShellExecute returned: %p\n",rv); // >32 is ok
} }
int int
dobugreport(void){ dobugreport(void)
{
if (!submit_web_report(2, NULL, "#bugreport command")) { if (!submit_web_report(2, NULL, "#bugreport command")) {
pline("Unable to send bug report. Please visit %s instead.", pline("Unable to send bug report. Please visit %s instead.",
sysopt.crashreporturl (sysopt.crashreporturl && *sysopt.crashreporturl)
? sysopt.crashreporturl ? sysopt.crashreporturl
: "https://www.nethack.org" : "https://www.nethack.org"
); );
@@ -1197,17 +1223,19 @@ dump_plines(void)
#endif /* DUMPLOG */ #endif /* DUMPLOG */
#ifdef CRASHREPORT #ifdef CRASHREPORT
// lineno==0 gives the most recent message (e.g. "Do you want to call panic..." /* lineno==0 gives the most recent message (e.g.
// if called from #panic) "Do you want to call panic..." if called from #panic) */
static const char * static const char *
get_saved_pline(int lineno){ get_saved_pline(int lineno)
{
int p; int p;
int limit = DUMPLOG_MSG_COUNT; int limit = DUMPLOG_MSG_COUNT;
if(lineno >= DUMPLOG_MSG_COUNT) return NULL; if (lineno >= DUMPLOG_MSG_COUNT)
return NULL;
p = (gs.saved_pline_index - 1) % DUMPLOG_MSG_COUNT; p = (gs.saved_pline_index - 1) % DUMPLOG_MSG_COUNT;
while (limit--) { while (limit--) {
if(gs.saved_plines[p]){ // valid line if (gs.saved_plines[p]) { /* valid line */
if (lineno--) { if (lineno--) {
p = (p - 1 + DUMPLOG_MSG_COUNT) % DUMPLOG_MSG_COUNT; p = (p - 1 + DUMPLOG_MSG_COUNT) % DUMPLOG_MSG_COUNT;
} else { } else {