- check for return values of *printf()
-- be more strict and humble when allocating memory from the stack
-
- expand use of hardcoded_strings module(s)
BOTH SERVER/CLIENT:
#include "control.h"
#include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
+#include <stdlib.h> /* free() */
#include <stdio.h> /* sprintf() */
#include <string.h> /* strlen() */
#include "../common/rexit.h" /* exit_err() */
+#include "../common/try_malloc.h" /* try_malloc() */
#include "interface_conf.h" /* reload_interface_conf(), save_interface_conf() */
#include "io.h" /* send() */
#include "keybindings.h" /* get_command_to_keycode(), get_keycode_to_command(),
* mod_selected_keyb(), move_keyb_selection()
*/
-#include "map.h" /* for map_scroll(), map_center(), toggle_autofocus() */
+#include "map.h" /* map_scroll(), map_center(), toggle_autofocus() */
#include "wincontrol.h" /* shift_active_win(), resize_active_win(),
* toggle_win_size_type(), toggle_window(),
* cycle_active_win(), scroll_v_screen(),
* toggle_linebreak_type(), toggle_winconfig()
*/
#include "windows.h" /* get_win_by_id() */
-#include "world.h" /* for global world */
+#include "world.h" /* world */
static uint8_t try_server_commands(struct Command * command)
{
+ char * f_name = "try_server_commands()";
if (command->server_msg)
{
uint8_t arg = (uint8_t) command->arg;
}
uint8_t command_size = strlen(command->server_msg);
uint8_t arg_size = 3;
- char msg[command_size + 1 + arg_size + 1];
+ char * msg = try_malloc(command_size + 1 + arg_size + 1, f_name);
sprintf(msg, "%s %d", command->server_msg, arg);
send(msg);
+ free(msg);
return 1;
}
return 0;
static void add_line_compact(struct Win * win, char * line, attr_t attri,
uint16_t * offset, uint8_t last)
{
+ char * f_name = "add_line_compact()";
uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0);
try_resize_winmap(win, y_start + 1, win->frame_size.x);
uint16_t len_line = strlen(line);
char * separator = last ? "" : " / ";
uint32_t len_line_new = len_line + strlen(separator);
- char line_new[len_line_new];
+ char * line_new = try_malloc(len_line_new, f_name);
sprintf(line_new, "%s%s", line, separator);
uint16_t x = 0;
uint16_t y;
try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
}
}
+ free(line_new);
*offset = x;
}
extern void draw_win_info(struct Win * win)
{
+ char * f_name = "draw_win_info()";
char * dsc_turn = "Turn: ";
char * dsc_hitpoints = "\nHitpoints: ";
uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
- char text[maxl + 1];
+ char * text = try_malloc(maxl + 1, f_name);
sprintf(text, "%s%d%s%d",
dsc_turn, world.turn, dsc_hitpoints, world.player_lifepoints);
add_text_with_linebreaks(win, text);
+ free(text);
}
extern void draw_winconf_geometry(struct Win * win)
{
+ char * f_name = "draw_winconf_geometry()";
char * title = "Window's geometry:\n\n";
char * h_title = "Height to save: ";
char h_value[6 + 1]; /* 6: int16_t value max strlen */
+ strlen(h_title) + strlen(h_value) + strlen(h_type)
+ strlen(w_title) + strlen(w_value) + strlen(w_type)
+ strlen(breaks_title) + strlen(breaks_type);
- char text[text_size + 1];
+ char * text = try_malloc(text_size + 1, f_name);
sprintf(text, "%s%s%s%s%s%s%s%s%s", title, h_title, h_value, h_type,
w_title, w_value, w_type, breaks_title, breaks_type);
add_text_with_linebreaks(win, text);
+ free(text);
}
extern void shift_active_win(char dir)
{
+ char * f_name = "shift_active_win()";
uint8_t len_order = strlen(world.winDB.order);
if (1 < len_order)
{
- char tmp[len_order + 1];
+ char * tmp = try_malloc(len_order + 1, f_name);
tmp[len_order] = '\0';
uint8_t pos = get_win_pos_in_order(world.winDB.active);
if ('f' == dir)
world.winDB.order[pos - 1] = world.winDB.active;
}
}
+ free(tmp);
update_wins(get_win_by_id(world.winDB.order[0]));
}
}
#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(),
{
dsc_space = fsize.y;
} /* vv-- 10 = max strlen for uint16_t */
- char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
+ uint16_t size = 1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1;
+ char * scrolldsc = try_malloc(size, "scroll_hint()");
sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
/* Decide on offset of the description text inside the scroll hint line. */
}
mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
}
+ free(scrolldsc);
}
static void draw_win_borderlines(struct Win * w)
{
+ char * f_name = "draw_win_borderlines()";
+
/* Draw vertical and horizontal border lines. */
uint16_t y, x;
for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
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, f_name);
char decoration = ' ';
if (w->id == world.winDB.active)
{
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);
}
}
extern void reset_windows_on_winch()
{
+ char * f_name = "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];
+ char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, f_name);
sprintf(tmp_order, "%s", world.winDB.order);
uint8_t i;
char tmp_active = world.winDB.active;
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;
}
#include <stdio.h> /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
* fputc(), fwrite(), fclose(), fopen(), clearerr()
*/
+#include <stdlib.h> /* free() */
#include <string.h> /* strlen() */
-#include <unistd.h> /* for access(), unlink() */
+#include <unistd.h> /* access(), unlink() */
#include "rexit.h" /* exit_err(), exit_trouble() */
+#include "try_malloc.h" /* try_malloc() */
extern FILE * try_fopen(char * path, char * mode, char * f)
{
+ char * f_name = "try_fopen()";
char * msg1 = "Trouble in ";
char * msg2 = " with fopen() (mode '";
char * msg3 = "') on path '";
char * msg4 = "'.";
uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
+ strlen(f) + strlen(path) + strlen(mode) + 1;
- char msg[size];
+ char * msg = try_malloc(size, f_name);
sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, mode, msg3, path, msg4);
FILE * file_p = fopen(path, mode);
exit_err(NULL == file_p, msg);
+ free(msg);
return file_p;
}
extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
char * f)
{
+ char * f_name = "try_fclose_unlink_rename()";
try_fclose(file, f);
char * msg1 = "Trouble in ";
char * msg4 = "'.";
char * msg2 = " with unlink() on path '";
uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
+ strlen(f) + strlen(p2) + 1;
- char msg[size];
+ char * msg = try_malloc(size, f_name);
sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
exit_err(unlink(p2), msg);
+ free(msg);
}
char * msg2 = " with rename() from '";
char * msg3 = "' to '";
uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
+ strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
- char msg[size];
+ char * msg = try_malloc(size, f_name);
sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
exit_err(rename(p1, p2), msg);
+ free(msg);
}
#include <errno.h> /* global errno */
#include <stdint.h> /* uint16_t */
#include <stdio.h> /* printf(), perror(), sprintf() */
-#include <stdlib.h> /* exit(), EXIT_FAILURE */
+#include <stdlib.h> /* exit(), free(), EXIT_FAILURE */
#include <string.h> /* strlen() */
+#include "try_malloc.h" /* try_malloc() */
extern void exit_trouble(int err, char * parent, char * child)
{
+ char * f_name = "exit_trouble()";
char * p1 = "Trouble in ";
char * p2 = " with ";
char * p3 = ".";
uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
+ strlen(p3) + 1;
- char msg[size];
+ char * msg = try_malloc(size, f_name);
sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
exit_err(err, msg);
+ free(msg);
}
#include "try_malloc.h"
#include <stdlib.h> /* for malloc */
+#include <stdio.h> /* sprintf() */
+#include <string.h> /* strlen() */
#include <sys/types.h> /* for size_t */
-#include "rexit.h" /* for exit_trouble() */
+#include "rexit.h" /* for exit_err() */
extern void * try_malloc(size_t size, char * f)
{
+ char * prefix = "Trouble with malloc() in ";
+ char * msg = malloc(strlen(prefix) + strlen(f) + 1 + 1);
+ exit_err(NULL == msg,
+ "Trouble in try_malloc() with malloc() for error message string.");
+ int test = sprintf(msg, "%s%s.", prefix, f);
+ exit_err(test < 0, "Trouble in try_malloc() with sprintf().");
void * p = malloc(size);
- exit_trouble(NULL == p, f, "malloc()");
+ exit_err(NULL == p, msg); /* Bypass exit_trouble() calling try_malloc(). */
+ free(msg);
return p;
}
exit_err(access(s[PATH_RECORD], F_OK), "No record found to replay.");
FILE * file = try_fopen(s[PATH_RECORD], "r", f_name);
uint32_t linemax = textfile_width(file);
- char line[linemax + 1];
+ char * line = try_malloc(linemax + 1, f_name);
while ( world.turn < world.replay
&& NULL != try_fgets(line, linemax + 1, file, f_name))
{
}
}
}
+ free(line);
try_fclose(file, f_name);
}
{
FILE * file = try_fopen(path_savefile, "r", f_name);
uint32_t linemax = textfile_width(file);
- char line[linemax + 1];
+ char * line = try_malloc(linemax + 1, f_name);
while (NULL != try_fgets(line, linemax + 1, file, f_name))
{
if (strlen(line) && strcmp("\n", line))
obey_msg(line, 0);
}
}
+ free(line);
try_fclose(file, f_name);
}
else
{
char * command = s[CMD_MAKE_WORLD];
- char msg[strlen(command) + 1 + 11 + 1];
+ char * msg = try_malloc(strlen(command) + 1 + 11 + 1, f_name);
sprintf(msg, "%s %d", command, (int) time(NULL));
obey_msg(msg, 1);
+ free(msg);
}
io_loop();
}
static void update_worldstate_file()
{
char * f_name = "update_worldstate_file()";
- char path_tmp[strlen(s[PATH_WORLDSTATE]) + strlen(s[PATH_SUFFIX_TMP]) + 1];
+ uint16_t size = strlen(s[PATH_WORLDSTATE]) + strlen(s[PATH_SUFFIX_TMP]) + 1;
+ char * path_tmp = try_malloc(size, f_name);
sprintf(path_tmp, "%s%s", s[PATH_WORLDSTATE], s[PATH_SUFFIX_TMP]);
FILE * file = try_fopen(path_tmp, "w", f_name);
struct Thing * player = get_player();
try_fwrite(world.log, strlen(world.log), 1, file, f_name);
}
try_fclose_unlink_rename(file, path_tmp, s[PATH_WORLDSTATE], f_name);
+ free(path_tmp);
set_cleanup_flag(CLEANUP_WORLDSTATE);
char * dot = ".\n";;
try_fwrite(dot, strlen(dot), 1, world.file_out, f_name);
* textfile_width(), try_fputc()
*/
#include "../common/rexit.h" /* exit_trouble(), exit_err() */
+#include "../common/try_malloc.h" /* try_malloc() */
#include "ai.h" /* ai() */
#include "cleanup.h" /* set_cleanup_flag(), unset_cleanup_flag() */
#include "field_of_view.h" /* build_fov_map() */
|| parse_thing_command(tok0, tok1, t)
|| parse_val(tok0, tok1, s[CMD_ARGUMENT], '8', (char *)&t->arg)
|| parse_val(tok0, tok1, s[CMD_PROGRESS],'8',(char *)&t->progress)
-
|| parse_val(tok0, tok1, s[CMD_LIFEPOINTS],'8',
(char *) &t->lifepoints)
|| parse_position(tok0, tok1, t)
static void record_msg(char * msg)
{
char * f_name = "record_msg()";
- char path_tmp[strlen(s[PATH_RECORD]) + strlen(s[PATH_SUFFIX_TMP]) + 1];
+ uint16_t size = strlen(s[PATH_RECORD]) + strlen(s[PATH_SUFFIX_TMP]) + 1;
+ char * path_tmp = try_malloc(size, f_name);
sprintf(path_tmp, "%s%s", s[PATH_RECORD], s[PATH_SUFFIX_TMP]);
FILE * file_tmp = try_fopen(path_tmp, "w", f_name);
if (!access(s[PATH_RECORD], F_OK))
{
FILE * file_read = try_fopen(s[PATH_RECORD], "r", f_name);
uint32_t linemax = textfile_width(file_read);
- char line[linemax + 1];
+ char * line = try_malloc(linemax + 1, f_name);
while (try_fgets(line, linemax + 1, file_read, f_name))
{
try_fwrite(line, strlen(line), 1, file_tmp, f_name);
}
+ free(line);
try_fclose(file_read, f_name);
}
try_fwrite(msg, strlen(msg), 1, file_tmp, f_name);
try_fputc('\n', file_tmp, f_name);
try_fclose_unlink_rename(file_tmp, path_tmp, s[PATH_RECORD], f_name);
+ free(path_tmp);
}
static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
{
+ char * f_name = "actor_hits_actor()";
struct ThingType * tt_hitter = get_thing_type(hitter->type);
struct ThingType * tt_hitted = get_thing_type(hitted->type);
struct Thing * player = get_player();
msg3 = tt_hitted->name;
}
uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
- char msg[len];
+ char * msg = try_malloc(len, f_name);
sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
update_log(msg);
+ free(msg);
hitted->lifepoints--;
if (0 == hitted->lifepoints)
{
static void playerbonus_move(char d, uint8_t passable)
{
+ char * f_name = "playerbonus_move()";
char * dsc_dir = "north-east";
if ( match_dir(d, &dsc_dir, 'd', "east")
|| match_dir(d, &dsc_dir, 'c', "south-east")
{
dsc_move = "You fail to move ";
}
- char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
+ char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, f_name);
sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
update_log(msg);
+ free(msg);
}
extern struct ThingType * get_thing_type(uint8_t id)
{
+ char * f_name = "get_thing_type()";
struct ThingType * tt = world.thing_types;
for (; NULL != tt && id != tt->id; tt = tt->next);
char * err_intro = "Requested thing type of unused ID ";
- char err[strlen(err_intro) + 3 + 1 + 1];
+ uint16_t size = strlen(err_intro) + 3 + 1 + 1;
+ char * err = try_malloc(size, f_name);
sprintf(err, "%s%d.", err_intro, id);
exit_err(NULL == tt, err);
+ free(err);
return tt;
}