From 71b947af0e9d42a37b8ea579e4b012182d3a1a68 Mon Sep 17 00:00:00 2001 From: "nethack.allison" Date: Tue, 25 Nov 2003 00:31:19 +0000 Subject: [PATCH] more status_via_windowport (trunk only) It was possible for status_finish to get called twice, therefore free() could end up called twice. Add a macro symbol for the argument to status_initialize(). Adjust some field widths so that there is a little bit of breathing space. --- include/botl.h | 2 ++ src/allmain.c | 2 +- src/botl.c | 67 +++++++++++++++++++++++++++----------------------- src/end.c | 4 +-- src/options.c | 2 +- src/polyself.c | 2 +- 6 files changed, 42 insertions(+), 37 deletions(-) diff --git a/include/botl.h b/include/botl.h index fa152b912..fa8631e86 100644 --- a/include/botl.h +++ b/include/botl.h @@ -41,4 +41,6 @@ #define BL_MASK_STUNNED 0x00000020L #define BL_MASK_SLIMED 0x00000040L +#define REASSESS_ONLY TRUE + #endif /* BOTL_H */ diff --git a/src/allmain.c b/src/allmain.c index 9ea7220ef..d5f329a15 100644 --- a/src/allmain.c +++ b/src/allmain.c @@ -448,7 +448,7 @@ display_gamewindows() { WIN_MESSAGE = create_nhwindow(NHW_MESSAGE); #ifdef STATUS_VIA_WINDOWPORT - status_initialize(FALSE); + status_initialize(0); #else WIN_STATUS = create_nhwindow(NHW_STATUS); #endif diff --git a/src/botl.c b/src/botl.c index 7e66fac92..a9204f3dc 100644 --- a/src/botl.c +++ b/src/botl.c @@ -320,32 +320,32 @@ struct istat_s { struct istat_s blstats[2][MAXBLSTATS] = { { { 0L, P_STR, (genericptr_t)0, (char *)0, 80, 0}, /* 0 BL_TITLE */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 6, 0}, /* 1 BL_STR */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 6, 0}, /* 2 BL_DX */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 6, 0}, /* 3 BL_CO */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 6, 0}, /* 4 BL_IN */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 6, 0}, /* 5 BL_WI */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 6, 0}, /* 6 BL_CH */ - { 0L, P_STR, (genericptr_t)0, (char *)0, 8, 0}, /* 7 BL_ALIGN */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 1 BL_STR */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 2 BL_DX */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 3 BL_CO */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 4 BL_IN */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 5 BL_WI */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 6 BL_CH */ + { 0L, P_STR, (genericptr_t)0, (char *)0, 40, 0}, /* 7 BL_ALIGN */ { 0L, P_LNG, (genericptr_t)0, (char *)0, 20, 0}, /* 8 BL_SCORE */ { 0L, P_LNG, (genericptr_t)0, (char *)0, 20, 0}, /* 9 BL_CAP */ - { 0L, P_LNG, (genericptr_t)0, (char *)0, 8, 0}, /* 10 BL_GOLD */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 6, BL_ENEMAX}, /* 11 BL_ENE */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 6, 0}, /* 12 BL_ENEMAX */ - { 0L, P_LNG, (genericptr_t)0, (char *)0, 6, 0}, /* 13 BL_XP */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 6, 0}, /* 14 BL_AC */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 6, 0}, /* 15 BL_HD */ - { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 16 BL_TIME */ - { 0L, P_UINT,(genericptr_t)0, (char *)0, 20, 0}, /* 17 BL_HUNGER */ + { 0L, P_LNG, (genericptr_t)0, (char *)0, 10, 0}, /* 10 BL_GOLD */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 10, BL_ENEMAX}, /* 11 BL_ENE */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 12 BL_ENEMAX */ + { 0L, P_LNG, (genericptr_t)0, (char *)0, 10, 0}, /* 13 BL_XP */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 14 BL_AC */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 15 BL_HD */ + { 0L, P_INT, (genericptr_t)0, (char *)0, 20, 0}, /* 16 BL_TIME */ + { 0L, P_UINT,(genericptr_t)0, (char *)0, 40, 0}, /* 17 BL_HUNGER */ { 0L, P_INT, (genericptr_t)0, (char *)0, 10,BL_HPMAX}, /* 18 BL_HP */ { 0L, P_INT, (genericptr_t)0, (char *)0, 10, 0}, /* 19 BL_HPMAX */ { 0L, P_STR, (genericptr_t)0, (char *)0, 80, 0}, /* 20 BL_LEVELDESC */ { 0L, P_LNG, (genericptr_t)0, (char *)0, 20, 0}, /* 21 BL_EXP */ - { 0L, P_MASK,(genericptr_t)0, (char *)0, 0, 0}, /* 22 BL_CONDITION */ + { 0L, P_MASK,(genericptr_t)0, (char *)0, 0, 0}, /* 22 BL_CONDITION */ } }; -static boolean update_all = FALSE; +static boolean blinit = FALSE, update_all = FALSE; void status_initialize(reassessment) @@ -367,6 +367,7 @@ boolean reassessment; /* TRUE = just reassess fields w/o other initialization*/ if (!reassessment) { init_blstats(); (*windowprocs.win_status_init)(); + blinit = TRUE; } for (i = 0; i < MAXBLSTATS; ++i) { if ((i == BL_SCORE && !flags.showscore) || @@ -391,8 +392,8 @@ status_finish() /* free memory that we alloc'd now */ for (i = 0; i < MAXBLSTATS; ++i) { - free(blstats[0][i].ptr.a_void); - free(blstats[1][i].ptr.a_void); + free((genericptr_t)blstats[0][i].ptr.a_void); + free((genericptr_t)blstats[1][i].ptr.a_void); if (blstats[0][i].val) free((genericptr_t)blstats[0][i].val); if (blstats[1][i].val) free((genericptr_t)blstats[1][i].val); } @@ -408,21 +409,21 @@ init_blstats() case P_INT: blstats[0][i].ptr.a_iptr = (int *)alloc(sizeof(int)); blstats[1][i].ptr.a_iptr = (int *)alloc(sizeof(int)); - *blstats[0][i].ptr.a_iptr = 0; - *blstats[1][i].ptr.a_iptr = 0; + *(blstats[0][i].ptr.a_iptr) = 0; + *(blstats[1][i].ptr.a_iptr) = 0; break; case P_LNG: case P_MASK: blstats[0][i].ptr.a_lptr = (long *)alloc(sizeof(long)); blstats[1][i].ptr.a_lptr = (long *)alloc(sizeof(long)); - *blstats[0][i].ptr.a_lptr = 0L; - *blstats[1][i].ptr.a_lptr = 0L; + *(blstats[0][i].ptr.a_lptr) = 0L; + *(blstats[1][i].ptr.a_lptr) = 0L; break; case P_UINT: blstats[0][i].ptr.a_uptr = (unsigned *)alloc(sizeof(unsigned)); blstats[1][i].ptr.a_uptr = (unsigned *)alloc(sizeof(unsigned)); - *blstats[0][i].ptr.a_uptr = 0; - *blstats[1][i].ptr.a_uptr = 0; + *(blstats[0][i].ptr.a_uptr) = 0; + *(blstats[1][i].ptr.a_uptr) = 0; break; } if (blstats[0][i].valwidth) { @@ -449,6 +450,8 @@ bot() anything curr, prev; boolean valset[MAXBLSTATS]; + if (!blinit) panic("bot before init."); + idx_p = idx; idx = 1 - idx; /* 0 -> 1, 1 -> 0 */ @@ -506,7 +509,7 @@ bot() /* Alignment */ - Sprintf(blstats[idx][BL_ALIGN].val, + Strcpy(blstats[idx][BL_ALIGN].val, (u.ualign.type == A_CHAOTIC) ? "Chaotic" : (u.ualign.type == A_NEUTRAL) ? "Neutral" : "Lawful"); @@ -752,8 +755,10 @@ genl_status_finish() int i; /* free alloc'd memory here */ - for (i = 0; i < MAXBLSTATS; ++i) - free((genericptr_t)vals[i]); + for (i = 0; i < MAXBLSTATS; ++i) { + if (vals[i]) free((genericptr_t)vals[i]); + vals[i] = (char *)0; + } } void @@ -777,6 +782,7 @@ genericptr_t ptr; static int init = FALSE; long cond; register int i; + char *text = (char *)ptr; int fieldorder1[] = { BL_TITLE, BL_STR, BL_DX,BL_CO, BL_IN, BL_WI, BL_CH,BL_ALIGN, BL_SCORE, -1 @@ -803,9 +809,8 @@ genericptr_t ptr; if (cond & BL_MASK_SLIMED) Strcat(vals[idx], " Slime"); break; default: - (void) Sprintf(vals[idx], - fieldfmt[idx] ? fieldfmt[idx] : "%s", - (char *)ptr, MAXCO); + Sprintf(vals[idx], + fieldfmt[idx] ? fieldfmt[idx] : "%s", text); break; } } diff --git a/src/end.c b/src/end.c index 70858f626..5257db13e 100644 --- a/src/end.c +++ b/src/end.c @@ -768,9 +768,7 @@ die: wait_synch(); display_nhwindow(WIN_MESSAGE, TRUE); destroy_nhwindow(WIN_MAP); -#ifdef STATUS_VIA_WINDOWPORT - status_finish(); -#else +#ifndef STATUS_VIA_WINDOWPORT destroy_nhwindow(WIN_STATUS); #endif destroy_nhwindow(WIN_MESSAGE); diff --git a/src/options.c b/src/options.c index 354d1cf72..22775af91 100644 --- a/src/options.c +++ b/src/options.c @@ -2212,7 +2212,7 @@ goodfruit: #endif ) { #ifdef STATUS_VIA_WINDOWPORT - status_initialize(TRUE); /* TRUE = reassess only */ + status_initialize(REASSESS_ONLY); #endif context.botl = TRUE; } diff --git a/src/polyself.c b/src/polyself.c index a911994ac..ce16fa0f8 100644 --- a/src/polyself.c +++ b/src/polyself.c @@ -25,7 +25,7 @@ set_uasmon() { set_mon_data(&youmonst, &mons[u.umonnum], 0); #ifdef STATUS_VIA_WINDOWPORT - status_initialize(TRUE); /* TRUE = reassess fields only */ + status_initialize(REASSESS_ONLY); #endif }