home · contact · privacy
Use not f_name variable but __func__, standardize function name writing.
[plomrogue] / src / client / windows.c
index faf1f8a642484a37235ae5b29eb3fe9698e75686..3881393ac01babe807f69e74576cf9c46f49d01f 100644 (file)
@@ -13,6 +13,7 @@
 #include <stdlib.h> /* free() */
 #include <string.h> /* memcpy(), strlen(), strnlen(), memset() */
 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
+#include "../common/try_malloc.h" /* try_malloc() */
 #include "draw_wins.h" /* draw_winconf_geometry(), draw_winconf_keybindings(),
                         * draw_win_inventory(), draw_win_info(), draw_win_log(),
                         * draw_win_available_keybindings(), draw_win_map(),
@@ -153,8 +154,10 @@ static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
     {
         dsc_space = fsize.y;
     }                                  /* vv-- 10 = max strlen for uint16_t */
-    char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
-    sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
+    uint16_t size = 1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1;
+    char * scrolldsc = try_malloc(size, __func__);
+    int test = sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
+    exit_trouble(test < 0, __func__, "sprintf");
 
     /* Decide on offset of the description text inside the scroll hint line. */
     uint16_t dsc_offset = 1;
@@ -188,6 +191,7 @@ static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
         }
         mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
     }
+    free(scrolldsc);
 }
 
 
@@ -230,7 +234,7 @@ static void draw_win_borderlines(struct Win * w)
             offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
         }                                    /* +2 is for padding/decoration */
         uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
-        char title[length_visible + 3];
+        char * title = try_malloc(length_visible + 3, "draw_win_borderlines");
         char decoration = ' ';
         if (w->id == world.winDB.active)
         {
@@ -240,6 +244,7 @@ static void draw_win_borderlines(struct Win * w)
         title[0] = title[length_visible + 1] = decoration;
         title[length_visible + 2] = '\0';
         mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
+        free(title);
     }
 }
 
@@ -387,16 +392,15 @@ extern struct Win * get_win_by_id(char id)
 
 extern void make_v_screen_and_init_win_sizes()
 {
-    char * f_name = "make_v_screen_and_init_win_sizes()";
     char * err_s = "creating an illegaly large virtual screen";
-    char * err_m = "triggering a memory allocation error via newpad()";
+    char * err_m = "triggering a memory allocation error via newpad";
     uint32_t maxy_test = getmaxy(world.winDB.t_screen);
     uint32_t maxx_test = getmaxx(world.winDB.t_screen);
-    exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, f_name, err_s);
+    exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, __func__, err_s);
     world.winDB.v_screen_size.y = maxy_test;
     world.winDB.v_screen_size.x = maxx_test;
     world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
-    exit_trouble(NULL == world.winDB.v_screen, f_name, err_m);
+    exit_trouble(NULL == world.winDB.v_screen, __func__, err_m);
     char id;
     while (0 != (id = get_next_win_id()))
     {
@@ -433,14 +437,16 @@ extern void reset_windows_on_winch()
 {
     endwin();  /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
     refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>.   */
-    char tmp_order[strlen(world.winDB.order) + 1];
-    sprintf(tmp_order, "%s", world.winDB.order);
+    char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, __func__);
+    int test = sprintf(tmp_order, "%s", world.winDB.order);
+    exit_trouble(test < 0, __func__, "sprintf");
     uint8_t i;
     char tmp_active = world.winDB.active;
     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
     delwin(world.winDB.v_screen);
     make_v_screen_and_init_win_sizes();
     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
+    free(tmp_order);
     world.winDB.active = tmp_active;
 }