Skip to content

ANSI-fying#10

Closed
MaddTheSane wants to merge 39 commits into
NetHack:NetHack-3.6.0from
MaddTheSane:ansiHEAD
Closed

ANSI-fying#10
MaddTheSane wants to merge 39 commits into
NetHack:NetHack-3.6.0from
MaddTheSane:ansiHEAD

Conversation

@MaddTheSane

Copy link
Copy Markdown
Contributor

This moves NetHack away from K&R to ANSI C calling conventions.

From mondata.c to oinit.c, including hack.c that got overlooked.
From vision.c to zap.c.

All of the main NetHack source code should be looked over now.
It now builds on OS X when -Werror=knr-promoted-parameter is set.
This updates the code from the current HEAD.
Some of them conflicted with earlier declarations, others had boolean being compared/set to a non-bool value.
Comment thread include/flag.h Outdated
boolean showexp; /* show experience points */
boolean showscore; /* show score */
boolean silent; /* whether the bell rings or not */
boolean sortloot; /* sort items alphabetically when looting */

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was more a change to make Clang happy, as there are a few places where sortloot is treated more like a char that a boolean type (I use a custom prefix header that typedefs boolean to C99's bool).

Will be reverting anyway.

@ghost

ghost commented Apr 13, 2016

Copy link
Copy Markdown

This will require some discussion from the DT on whether we want to implement this and thereby drop pre-ANSI compiler support (which has, up until this point, been somewhat of a point of pride for NetHack to support so many platforms). In the meanwhile, please remove all changes that aren't strictly related to the ANSIfication if you wish to help the PR along.

@tung

tung commented Apr 13, 2016

Copy link
Copy Markdown
Contributor

In case it hasn't been seen, there was also an /r/nethack submission for this PR with a lot of relevant comments. I posted in that thread, but I'll repeat what I said about this PR here too:

This is a good thing, because it's the reason NetHack has to declare arg types like this:

int FDECL(foo, (SHORT_P, CHAR_P, BOOLEAN_P));

int
foo(s, c, b)
short s;
char c;
boolean b;
{
    /* ... */
    return 0;
}

instead of this:

int foo(short, char, boolean);

int
foo(short s, char c, boolean b)
{
    /* ... */
    return 0;
}

It also thwarts the ability for git to show the containing functions of diff blocks, which makes quickly figuring out where changes were made a big pain. Good stuff!

I forgot about FDECL and related macros. I've removed most of them now.

Just as well, I think FDECL is the reason that cscope doesn't work on the NetHack source code, and cscope is essentially god mode for C programming, so on the distant chance this actually gets merged into main line, it will help a lot.

As suggested by @scshunt.
@ghost

ghost commented Apr 14, 2016

Copy link
Copy Markdown

Changes to make Clang happy are definitely encouraged, but they should be a separate PR.

@MaddTheSane

Copy link
Copy Markdown
Contributor Author

Fair enough.

nhcopier pushed a commit that referenced this pull request Nov 19, 2019
@MaddTheSane MaddTheSane mentioned this pull request Dec 7, 2019
@MaddTheSane MaddTheSane closed this Dec 7, 2019
nhcopier pushed a commit that referenced this pull request Dec 1, 2022
If you were on a level teleporter, the spoteffects() call after
the hero gets expelled could end up going to a new level and
freeing all the monst chains from the level you were originally
engulfed on.

    #0 0xba0507 in free
    #1 0x87feda in dealloc_monst src/mon.c:2369
    #2 0x880a02 in dmonsfree src/mon.c:2194
    #3 0x9a7aa2 in savelev_core src/save.c:507
    #4 0x9a7a21 in savelev src/save.c:466
    #5 0x71eb9d in goto_level src/do.c:1483
    #6 0x71833f in deferred_goto src/do.c:1903
    #7 0xa2533f in level_tele src/teleport.c:1117
    #8 0xa2567b in level_tele_trap src/teleport.c:1198
    #9 0xa5c007 in trapeffect_level_telep src/trap.c:1861
    #10 0xa5f856 in trapeffect_selector src/trap.c:2497
    #11 0xa47497 in dotrap src/trap.c:2586
    #12 0x7d669b in spoteffects src/hack.c:2859
    #13 0x89d495 in xkilled src/mon.c:3187

The latter parts of xkilled() after the spoteffects() call would
then attempt to dereference the free'd monst pointer.

Save a copy of the monst struct prior to spoteffects() if you were
expelled, then point at the reference copy afterwards.

Resolves #938
nhcopier pushed a commit that referenced this pull request Aug 28, 2023
On GitHub issue 1090, Rhialto wrote:
Hi! I am currently in the process of adapting the pkgsrc packaging of NetHack 3.6.7 to include the curses window option. A bit late perhaps...

While working on that, I ran into a case of a NULL pointer being used. Apparently when linking with ncurses, this doesn't cause problems, but NetBSD's own curses doesn't like it, and crashes the game.

Here is the stack trace. It happens when #quitting, I think just before the high scores are about to be shown (but I guess it should show in the stack trace if I'm totally correct with that):

(gdb) bt
 #0  redrawwin (win=0x0) at /usr/src/lib/libcurses/touchwin.c:149
 #1  0x0000000017e104b4 in curses_refresh_nethack_windows () at ../win/curses/curswins.c:176
 #2  0x0000000017e1054a in curses_destroy_win (win=win@entry=0x7ad219520540) at ../win/curses/curswins.c:155
 #3  0x0000000017e1512c in curses_display_nhmenu (wid=<optimized out>, how=<optimized out>, _selected=0x7f7fff65d850) at ../win/curses/cursdial.c:771
 #4  0x0000000017e0ff81 in curses_select_menu (wid=wid@entry=21, how=how@entry=0, selected=selected@entry=0x7f7fff65d850) at ../win/curses/cursmain.c:607
 #5  0x0000000017e10257 in curses_display_nhwindow (wid=21, block=1) at ../win/curses/cursmain.c:385
 #6  0x0000000017ca8075 in really_done (how=<optimized out>, how@entry=13) at end.c:1609
 #7  0x0000000017ca993f in done (how=how@entry=13) at end.c:1201
 #8  0x0000000017ca9c85 in done2 () at end.c:381
 #9  done2 () at end.c:337
 #10 0x0000000017c489da in doextcmd () at cmd.c:363
 #11 0x0000000017c57e94 in rhack (cmd=<optimized out>, cmd@entry=0x0) at cmd.c:4909
 #12 0x0000000017c265da in moveloop (resuming=<optimized out>) at allmain.c:435
 #13 0x0000000017e1b862 in main (argc=<optimized out>, argv=<optimized out>) at ../sys/unix/unixmain.c:351
status_window is NULL here. Simply checking for NULL avoids the problem. I added checks for the other windows as well, while I was there.
nhcopier pushed a commit that referenced this pull request Dec 23, 2024
If freedynamicdata() gets called twice, for whatever reason, a "double free" can occur.

warning: 44     ./nptl/pthread_kill.c: No such file or directory
(gdb) bt
#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:78
#2  __GI___pthread_kill (threadid=<optimized out>, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3  0x00007ffff7c8b26e in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007ffff7c6e8ff in __GI_abort () at ./stdlib/abort.c:79
#5  0x00007ffff7c6f7b6 in __libc_message_impl (fmt=fmt@entry=0x7ffff7e148d7 "%s\n")
    at ../sysdeps/posix/libc_fatal.c:132
#6  0x00007ffff7ceefe5 in malloc_printerr (str=str@entry=0x7ffff7e17bf0 "free(): double free detected in tcache 2")
    at ./malloc/malloc.c:5772
#7  0x00007ffff7cf154f in _int_free (av=0x7ffff7e49ac0 <main_arena>, p=<optimized out>, have_lock=0)
    at ./malloc/malloc.c:4541
#8  0x00007ffff7cf3d9e in __GI___libc_free (mem=0x555555ad82a0) at ./malloc/malloc.c:3398
#9  0x00005555557c12e9 in free_rect () at rect.c:48
#10 0x00005555557d77a2 in freedynamicdata () at save.c:1240
#11 0x0000555555682754 in nh_terminate (status=0) at end.c:1671
#12 0x000055555589af15 in opt_terminate () at ../sys/unix/unixmain.c:768
#13 0x000055555589af7a in after_opt_showpaths (dir=0x0) at ../sys/unix/unixmain.c:796
#14 0x0000555555693dd9 in do_deferred_showpaths (code=0) at files.c:4491
#15 0x0000555555778405 in initoptions () at options.c:6948
#16 0x0000555555899cd9 in main (argc=2, argv=0x7fffffffdad8) at ../sys/unix/unixmain.c:151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants