]> granicus.if.org Git - nethack/commitdiff
curses: change from malloc() to nethack's alloc()
authorPatR <rankin@nethack.org>
Fri, 8 Feb 2019 00:48:37 +0000 (16:48 -0800)
committerPatR <rankin@nethack.org>
Fri, 8 Feb 2019 00:48:37 +0000 (16:48 -0800)
There was no provision for malloc() potentially returning Null and it
wasn't integrated with nethack's MONITOR_HEAP.  'heaputil' shows that
the curses interface is leaking like a sieve.  If some things are
actually being allocated separately and then freed from within curses,
those need to be thoroughly documented and maybe switched back to
malloc().

doc/fixes36.2
win/curses/cursdial.c
win/curses/cursmesg.c
win/curses/cursmisc.c
win/curses/curswins.c

index dacb4f4a9f6a7816e0ae347d24aec40b4d092232..3073017f879368b1bc6b4a9701e1164cb57d351a 100644 (file)
@@ -1,4 +1,4 @@
-$NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.250 $ $NHDT-Date: 1549584261 2019/02/08 00:04:21 $
+$NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.251 $ $NHDT-Date: 1549586901 2019/02/08 00:48:21 $
 
 This fixes36.2 file is here to capture information about updates in the 3.6.x
 lineage following the release of 3.6.1 in April 2018. Please note, however,
@@ -439,6 +439,9 @@ X11: its use of genl_status_update exposed a negative index use that could
 X11: rollback disabling of keystroke input for PICK_NONE menus (for scrolling)
 curses: catch up with tty to not put dolook/whatis autodescribe feedback into
        ^P message recall (multi-digit count feedback was already handled)
+curses: if the interface code ran out of memory, it would crash rather than
+       attempt a controlled panic (which is fairly likely crash anyway if
+       done when there's no memory available)
 
 
 Platform- and/or Interface-Specific Fixes or Features
index fd41e64ce29437961401a675fe20576315b5c57f..b4c5e66f188357967ac4a31265a0c428bdf2d756 100644 (file)
@@ -478,7 +478,7 @@ curses_create_nhmenu(winid wid)
         return;
     }
 
-    new_menu = malloc(sizeof (nhmenu));
+    new_menu = (nhmenu *) alloc((signed) sizeof (nhmenu));
     new_menu->wid = wid;
     new_menu->prompt = NULL;
     new_menu->entries = NULL;
@@ -524,7 +524,7 @@ curses_add_nhmenu_item(winid wid, int glyph, const ANY_P * identifier,
 
     new_str = curses_copy_of(str);
     curses_rtrim((char *) new_str);
-    new_item = malloc(sizeof (nhmenu_item));
+    new_item = (nhmenu_item *) alloc((unsigned) sizeof (nhmenu_item));
     new_item->wid = wid;
     new_item->glyph = glyph;
     new_item->identifier = *identifier;
@@ -630,7 +630,8 @@ curses_display_nhmenu(winid wid, int how, MENU_ITEM_P ** _selected)
     curses_destroy_win(win);
 
     if (num_chosen > 0) {
-        selected = (MENU_ITEM_P *) malloc(num_chosen * sizeof (MENU_ITEM_P));
+        selected = (MENU_ITEM_P *) alloc((unsigned)
+                                         (num_chosen * sizeof (MENU_ITEM_P)));
         count = 0;
 
         menu_item_ptr = current_menu->entries;
index 3f90ed8bca43ab632c29d5af8072349dbe4f39af..3876449a004ba7233388fd9819ed1af94fa81e59 100644 (file)
@@ -287,7 +287,7 @@ curses_prev_mesg()
 
     wid = curses_get_wid(NHW_MENU);
     curses_create_nhmenu(wid);
-    identifier = malloc(sizeof (anything));
+    identifier = (anything *) alloc((unsigned) sizeof (anything));
     identifier->a_void = NULL;
 
     for (count = 0; count < num_messages; count++) {
@@ -380,13 +380,13 @@ curses_message_win_getline(const char *prompt, char *answer, int buffer)
     maxy = height - 1 + border_space;
     maxx = width - 1 + border_space;
 
-    tmpbuf = (char *)malloc(strlen(prompt) + buffer + 2);
+    tmpbuf = (char *) alloc((unsigned) ((int) strlen(prompt) + buffer + 2));
     maxlines = buffer / width * 2;
     strcpy(tmpbuf, prompt);
     strcat(tmpbuf, " ");
     nlines = curses_num_lines(tmpbuf,width);
     maxlines += nlines * 2;
-    linestarts = (char **)malloc(sizeof(char*) * maxlines);
+    linestarts = (char **) alloc((unsigned) (sizeof (char *) * maxlines));
     p_answer = tmpbuf + strlen(tmpbuf);
     linestarts[0] = tmpbuf;
 
@@ -573,7 +573,8 @@ static void
 mesg_add_line(char *mline)
 {
     nhprev_mesg *tmp_mesg = NULL;
-    nhprev_mesg *current_mesg = malloc(sizeof (nhprev_mesg));
+    nhprev_mesg *current_mesg = (nhprev_mesg *) alloc((unsigned)
+                                                      (sizeof (nhprev_mesg)));
 
     current_mesg->str = curses_copy_of(mline);
     current_mesg->turn = moves;
index c20138453973039db72d85b22347e3f57bce5d2f..57a637bfe05fecec2f6b1200c5a3a1be46113cb0 100644 (file)
@@ -220,12 +220,12 @@ curses_copy_of(const char *s)
 {
     if (!s)
         s = "";
-    return strcpy((char *) alloc((unsigned) (strlen(s) + 1)), s);
+    return dupstr(s);
 }
 
 
 /* Determine the number of lines needed for a string for a dialog window
-of the given width */
+   of the given width */
 
 int
 curses_num_lines(const char *str, int width)
@@ -588,7 +588,7 @@ curses_view_file(const char *filename, boolean must_exist)
 
     wid = curses_get_wid(NHW_MENU);
     curses_create_nhmenu(wid);
-    identifier = malloc(sizeof (anything));
+    identifier = (anything *) alloc(sizeof (anything));
     identifier->a_void = NULL;
 
     while (dlb_fgets(buf, BUFSZ, fp) != NULL) {
index c74a39871bc964d3fb6d3ceaf2b02ff9ba007bd5..d347b3133ebc9cd47b56656e04022de76f9e0be8 100644 (file)
@@ -79,7 +79,8 @@ curses_create_window(int width, int height, orient orientation)
     height += 2;
 
     if ((width > term_cols) || (height > term_rows)) {
-        impossible("curses_create_window: Terminal too small for dialog window");
+        impossible(
+                "curses_create_window: Terminal too small for dialog window");
         width = term_cols;
         height = term_rows;
     }
@@ -193,7 +194,8 @@ WINDOW *
 curses_get_nhwin(winid wid)
 {
     if (!is_main_window(wid)) {
-        impossible("curses_get_nhwin: wid %d out of range. Not a main window.", wid);
+        impossible("curses_get_nhwin: wid %d out of range. Not a main window.",
+                   wid);
         return NULL;
     }
 
@@ -212,7 +214,8 @@ curses_add_nhwin(winid wid, int height, int width, int y, int x,
     int real_height = height;
 
     if (!is_main_window(wid)) {
-        impossible("curses_add_nhwin: wid %d out of range. Not a main window.", wid);
+        impossible("curses_add_nhwin: wid %d out of range. Not a main window.",
+                   wid);
         return;
     }
 
@@ -266,7 +269,7 @@ curses_add_wid(winid wid)
     nethack_wid *new_wid;
     nethack_wid *widptr = nhwids;
 
-    new_wid = malloc(sizeof (nethack_wid));
+    new_wid = (nethack_wid *) alloc((unsigned) sizeof (nethack_wid));
     new_wid->nhwid = wid;
 
     new_wid->next_wid = NULL;
@@ -305,7 +308,8 @@ curses_del_nhwin(winid wid)
     }
 
     if (!is_main_window(wid)) {
-        impossible("curses_del_nhwin: wid %d out of range. Not a main window.", wid);
+        impossible("curses_del_nhwin: wid %d out of range. Not a main window.",
+                   wid);
         return;
     }
 
@@ -396,7 +400,9 @@ void
 curses_get_window_xy(winid wid, int *x, int *y)
 {
     if (!is_main_window(wid)) {
-        impossible("curses_get_window_xy: wid %d out of range. Not a main window.", wid);
+        impossible(
+              "curses_get_window_xy: wid %d out of range. Not a main window.",
+                   wid);
         *x = 0;
         *y = 0;
         return;
@@ -451,8 +457,9 @@ int
 curses_get_window_orientation(winid wid)
 {
     if (!is_main_window(wid)) {
-        impossible
-            ("curses_get_window_orientation: wid %d out of range. Not a main window.", wid);
+        impossible(
+     "curses_get_window_orientation: wid %d out of range. Not a main window.",
+                   wid);
         return CENTER;
     }
 
@@ -461,7 +468,7 @@ curses_get_window_orientation(winid wid)
 
 
 /* Output a line of text to specified NetHack window with given coordinates
-and text attributes */
+   and text attributes */
 
 void
 curses_puts(winid wid, int attr, const char *text)
@@ -490,10 +497,12 @@ curses_puts(winid wid, int attr, const char *text)
 
     if (curses_is_menu(wid) || curses_is_text(wid)) {
         if (!curses_menu_exists(wid)) {
-            impossible("curses_puts: Attempted write to nonexistant window %d!", wid);
+            impossible(
+                     "curses_puts: Attempted write to nonexistant window %d!",
+                       wid);
             return;
         }
-        identifier = malloc(sizeof (anything));
+        identifier = (anything *) alloc((unsigned) sizeof (anything));
         identifier->a_void = NULL;
         curses_add_nhmenu_item(wid, NO_GLYPH, identifier, 0, 0, attr, text,
                                FALSE);