From: Christian Heller Date: Wed, 9 Jul 2014 19:45:51 +0000 (+0200) Subject: Test return values of _all_ *printf() calls. X-Git-Tag: tce~725 X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/ledger?a=commitdiff_plain;h=778534bf6946fe0fef17e353c55678d248d8d09d;p=plomrogue Test return values of _all_ *printf() calls. --- diff --git a/TODO b/TODO index a5a87c3..8ba4592 100644 --- a/TODO +++ b/TODO @@ -2,8 +2,6 @@ Next planned steps in plomrogue development: IN GENERAL: -- check for return values of *printf() - - expand use of hardcoded_strings module(s) BOTH SERVER/CLIENT: diff --git a/src/client/control.c b/src/client/control.c index f97f5bd..02e4fed 100644 --- a/src/client/control.c +++ b/src/client/control.c @@ -5,7 +5,7 @@ #include /* free() */ #include /* sprintf() */ #include /* strlen() */ -#include "../common/rexit.h" /* exit_err() */ +#include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "interface_conf.h" /* reload_interface_conf(), save_interface_conf() */ #include "io.h" /* send() */ @@ -169,7 +169,8 @@ static uint8_t try_server_commands(struct Command * command) uint8_t command_size = strlen(command->server_msg); uint8_t arg_size = 3; char * msg = try_malloc(command_size + 1 + arg_size + 1, f_name); - sprintf(msg, "%s %d", command->server_msg, arg); + int test = sprintf(msg, "%s %d", command->server_msg, arg); + exit_trouble(test < 0, f_name, "sprintf()"); send(msg); free(msg); return 1; diff --git a/src/client/draw_wins.c b/src/client/draw_wins.c index b6a9c3e..f6ac7e2 100644 --- a/src/client/draw_wins.c +++ b/src/client/draw_wins.c @@ -8,7 +8,7 @@ #include /* sprintf() */ #include /* free() */ #include /* memset(), strchr(), strdup/(), strlen() */ -#include "../common/rexit.h" /* exit_err() */ +#include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "keybindings.h" /* struct KeyBindingDB, get_keyname_to_keycode() */ #include "windows.h" /* yx_uint16, Win, get_win_by_id() */ @@ -185,7 +185,8 @@ static void add_line_compact(struct Win * win, char * line, attr_t attri, char * separator = last ? "" : " / "; uint32_t len_line_new = len_line + strlen(separator); char * line_new = try_malloc(len_line_new, f_name); - sprintf(line_new, "%s%s", line, separator); + int test = sprintf(line_new, "%s%s", line, separator); + exit_trouble(test < 0, f_name, "sprintf()"); uint16_t x = 0; uint16_t y; uint32_t z; @@ -263,7 +264,8 @@ static char * get_kb_line(struct KeyBinding * kb) char * keyname = get_keyname_to_keycode(kb->keycode); uint16_t size = strlen(keyname) + 3 + strlen(kb->command->dsc_long) + 1; char * kb_line = try_malloc(size, f_name); - sprintf(kb_line, "%s - %s", keyname, kb->command->dsc_long); + int test = sprintf(kb_line, "%s - %s", keyname, kb->command->dsc_long); + exit_trouble(test < 0, f_name, "sprintf()"); free(keyname); return kb_line; } @@ -362,8 +364,9 @@ extern void draw_win_info(struct Win * win) char * dsc_hitpoints = "\nHitpoints: "; uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3; char * text = try_malloc(maxl + 1, f_name); - sprintf(text, "%s%d%s%d", - dsc_turn, world.turn, dsc_hitpoints, world.player_lifepoints); + int test = sprintf(text, "%s%d%s%d", dsc_turn, world.turn, dsc_hitpoints, + world.player_lifepoints); + exit_trouble(test < 0, f_name, "sprintf()"); add_text_with_linebreaks(win, text); free(text); } @@ -463,13 +466,15 @@ extern void draw_winconf_geometry(struct Win * win) char * title = "Window's geometry:\n\n"; char * h_title = "Height to save: "; char h_value[6 + 1]; /* 6: int16_t value max strlen */ - sprintf(h_value, "%d", win->target_height); + int test = sprintf(h_value, "%d", win->target_height); + exit_trouble(test < 0, f_name, "sprintf()"); char * h_plus = " (width in cells)\n\n"; char * h_minus = " (negative diff: cells to screen width)\n\n"; char * h_type = (1 == win->target_height_type) ? h_minus : h_plus; char * w_title = "Width to save: "; char w_value[6 + 1]; - sprintf(w_value, "%d", win->target_width); + test = sprintf(w_value, "%d", win->target_width); + exit_trouble(test < 0, f_name, "sprintf()"); char * w_plus = "(height in cells)\n\n"; char * w_minus = " (negative diff: cells to screen height)\n\n"; char * w_type = (1 == win->target_width_type) ? w_minus : w_plus; @@ -481,8 +486,9 @@ extern void draw_winconf_geometry(struct Win * win) + strlen(w_title) + strlen(w_value) + strlen(w_type) + strlen(breaks_title) + strlen(breaks_type); 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); + test = 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); + exit_trouble(test < 0, f_name, "sprintf()"); add_text_with_linebreaks(win, text); free(text); } diff --git a/src/client/io.c b/src/client/io.c index 31387f2..c047f69 100644 --- a/src/client/io.c +++ b/src/client/io.c @@ -106,7 +106,8 @@ static void read_inventory(char * read_buf, uint32_t linemax, FILE * file) int new_size = strlen(read_buf); char * new_inventory = try_malloc(old_size + new_size + 1, f_name); memcpy(new_inventory, world.player_inventory, old_size); - sprintf(new_inventory + old_size, "%s", read_buf); + int test = sprintf(new_inventory + old_size, "%s", read_buf); + exit_trouble(test < 0, f_name, "sprintf()"); free(world.player_inventory); world.player_inventory = new_inventory; } @@ -150,7 +151,8 @@ static void read_log(char * read_buf, uint32_t linemax, FILE * file) int new_size = strlen(read_buf); char * new_log = try_malloc(old_size + new_size + 1, f_name); memcpy(new_log, world.log, old_size); - sprintf(new_log + old_size, "%s", read_buf); + int test = sprintf(new_log + old_size, "%s", read_buf); + exit_trouble(test < 0, f_name, "sprintf()"); free(world.log); world.log = new_log; } diff --git a/src/client/keybindings.c b/src/client/keybindings.c index 4231da4..b0b7c7a 100644 --- a/src/client/keybindings.c +++ b/src/client/keybindings.c @@ -5,6 +5,7 @@ #include /* NULL */ #include /* uint8_t, uint16_t, uint32_t */ #include /* FILE, sprintf() */ +#include "../common/rexit.h" /* exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "windows.h" /* draw_all_wins() */ #include "world.h" /* global world */ @@ -49,9 +50,11 @@ static struct KeyBindingDB * char_selected_kb_db(char c) static uint8_t try_keycode(uint16_t keycode_given, char * keyname, uint16_t keycode_match, char * keyname_match) { + char * f_name = "try_keycode()"; if (keycode_given == keycode_match) { - sprintf(keyname, "%s", keyname_match); + int test = sprintf(keyname, "%s", keyname_match); + exit_trouble(test < 0, f_name, "sprintf()"); return 1; } return 0; @@ -81,12 +84,12 @@ extern char * get_keyname_to_keycode(uint16_t keycode) char * keyname = try_malloc(10, f_name); /* max keyname length + 1 */ if (32 < keycode && keycode < 127) { - sprintf(keyname, "%c", keycode); + exit_trouble(sprintf(keyname, "%c", keycode) < 0, f_name, "sprintf()"); } else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) { uint16_t f = keycode - KEY_F0; - sprintf(keyname, "F%d", f); + exit_trouble(sprintf(keyname, "F%d", f) < 0, f_name, "sprintf()");; } else if ( try_keycode(keycode, keyname, 9, "TAB") || try_keycode(keycode, keyname, 10, "RETURN") @@ -108,7 +111,7 @@ extern char * get_keyname_to_keycode(uint16_t keycode) } else { - sprintf(keyname, "(unknown)"); + exit_trouble(sprintf(keyname, "(unknown)") < 0, f_name, "sprintf()"); } return keyname; } diff --git a/src/client/main.c b/src/client/main.c index f2051a4..344df69 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -72,6 +72,6 @@ int main(int argc, char * argv[]) /* Leave properly. */ cleanup(); - printf("%s\n", quit_msg); + exit_trouble(printf("%s\n", quit_msg) < 0, f_name, "printf()"); exit(EXIT_SUCCESS); } diff --git a/src/client/wincontrol.c b/src/client/wincontrol.c index e1a4fbd..c42516f 100644 --- a/src/client/wincontrol.c +++ b/src/client/wincontrol.c @@ -7,7 +7,7 @@ #include /* sprintf() */ #include /* free() */ #include /* memcpy(), memset(), strchr(), strlen() */ -#include "../common/rexit.h" /* exit_err() */ +#include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "windows.h" /* Win,yx_uint16, get_win_by_id(),get_win_pos_in_order() */ #include "world.h" /* global world */ @@ -200,7 +200,8 @@ static void suspend_win(struct Win * w) char next_char = world.winDB.order[i + 1]; world.winDB.order[i] = '\0'; char * second_part = &world.winDB.order[i + 1]; - sprintf(new_order, "%s%s", world.winDB.order, second_part); + int test = sprintf(new_order, "%s%s", world.winDB.order, second_part); + exit_trouble(test < 0, f_name, "sprintf()"); free(world.winDB.order); world.winDB.order = new_order; world.winDB.active = world.winDB.order[i]; diff --git a/src/client/windows.c b/src/client/windows.c index 7327fc2..0fee3e3 100644 --- a/src/client/windows.c +++ b/src/client/windows.c @@ -147,6 +147,8 @@ static char get_next_win_id() static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist, char * unit, struct yx_uint16 start) { + char * f_name = "sprintf()"; + /* Decide on alignment (vertical/horizontal?), thereby hint text space. */ char * more = "more"; uint16_t dsc_space = fsize.x; @@ -155,8 +157,9 @@ static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist, dsc_space = fsize.y; } /* vv-- 10 = max strlen for uint16_t */ 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); + char * scrolldsc = try_malloc(size, f_name); + int test = sprintf(scrolldsc, " %d %s %s ", dist, more, unit); + exit_trouble(test < 0, f_name, "sprintf()"); /* Decide on offset of the description text inside the scroll hint line. */ uint16_t dsc_offset = 1; @@ -210,8 +213,6 @@ static void winscroll_hint(struct Win * w, char dir, uint16_t dist) 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++) @@ -235,7 +236,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 = try_malloc(length_visible + 3, f_name); + char * title = try_malloc(length_visible + 3, "draw_win_borderlines()"); char decoration = ' '; if (w->id == world.winDB.active) { @@ -441,7 +442,8 @@ extern void reset_windows_on_winch() endwin(); /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */ refresh(); /* . */ char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, f_name); - sprintf(tmp_order, "%s", world.winDB.order); + int test = sprintf(tmp_order, "%s", world.winDB.order); + exit_trouble(test < 0, f_name, "sprintf()"); uint8_t i; char tmp_active = world.winDB.active; for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++); diff --git a/src/common/readwrite.c b/src/common/readwrite.c index abc8ca8..c27e861 100644 --- a/src/common/readwrite.c +++ b/src/common/readwrite.c @@ -24,7 +24,8 @@ extern FILE * try_fopen(char * path, char * mode, char * f) uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4) + strlen(f) + strlen(path) + strlen(mode) + 1; char * msg = try_malloc(size, f_name); - sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, mode, msg3, path, msg4); + int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,mode,msg3,path,msg4); + exit_trouble(test < 0, f_name, "sprintf()"); FILE * file_p = fopen(path, mode); exit_err(NULL == file_p, msg); free(msg); @@ -87,7 +88,8 @@ extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2, uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4) + strlen(f) + strlen(p2) + 1; char * msg = try_malloc(size, f_name); - sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4); + int test = sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4); + exit_trouble(test < 0, f_name, "sprintf()"); exit_err(unlink(p2), msg); free(msg); } @@ -96,7 +98,8 @@ extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2, uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1) + strlen(msg3) + strlen(p2) + strlen(msg4) + 1; char * msg = try_malloc(size, f_name); - sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4); + int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,p1,msg3,p2,msg4); + exit_trouble(test < 0, f_name, "sprintf()"); exit_err(rename(p1, p2), msg); free(msg); } diff --git a/src/common/rexit.c b/src/common/rexit.c index d8598cd..7e16f12 100644 --- a/src/common/rexit.c +++ b/src/common/rexit.c @@ -51,7 +51,8 @@ extern void exit_trouble(int err, char * parent, char * child) uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child) + strlen(p3) + 1; char * msg = try_malloc(size, f_name); - sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3); + int test = sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3); + exit_err(test < 0, "Trouble in exit_trouble() with sprintf()"); exit_err(err, msg); free(msg); } diff --git a/src/server/init.c b/src/server/init.c index a91bd2a..1060474 100644 --- a/src/server/init.c +++ b/src/server/init.c @@ -15,7 +15,7 @@ #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(), * try_fgets(), try_fwrite() */ -#include "../common/rexit.h" /* exit_err() */ +#include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "cleanup.h" /* set_cleanup_flag() */ #include "field_of_view.h" /* build_fov_map() */ @@ -100,7 +100,8 @@ extern void setup_server_io() exit_trouble(test && EEXIST != errno, f_name, "mkdir()"); world.file_out = try_fopen(s[PATH_OUT], "w", f_name); world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name); - sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0)); + test = sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0)); + exit_trouble(test < 0, f_name, "sprintf()"); try_fwrite(world.server_test, strlen(world.server_test), 1, world.file_out, f_name); fflush(world.file_out); @@ -186,7 +187,8 @@ extern void run_game() { char * command = s[CMD_MAKE_WORLD]; char * msg = try_malloc(strlen(command) + 1 + 11 + 1, f_name); - sprintf(msg, "%s %d", command, (int) time(NULL)); + int test = sprintf(msg, "%s %d", command, (int) time(NULL)); + exit_trouble(test < 0, f_name, "sprintf()"); obey_msg(msg, 1); free(msg); } diff --git a/src/server/io.c b/src/server/io.c index bc9d877..258ec45 100644 --- a/src/server/io.c +++ b/src/server/io.c @@ -182,7 +182,8 @@ static void update_worldstate_file() char * f_name = "update_worldstate_file()"; 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]); + int test = sprintf(path_tmp, "%s%s", s[PATH_WORLDSTATE],s[PATH_SUFFIX_TMP]); + exit_trouble(test < 0, f_name, "sprintf()"); FILE * file = try_fopen(path_tmp, "w", f_name); struct Thing * player = get_player(); write_value_as_line(world.turn, file); @@ -210,7 +211,7 @@ static void write_value_as_line(uint32_t value, FILE * file) { char * f_name = "write_value_as_line()"; char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */ - sprintf(write_buf, "%u\n", value); + exit_trouble(sprintf(write_buf, "%u\n", value) < 0, f_name, "sprintf()"); try_fwrite(write_buf, strlen(write_buf), 1, file, f_name); } diff --git a/src/server/run.c b/src/server/run.c index 9ab9f30..2b56a9d 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -4,7 +4,7 @@ #include "run.h" #include /* NULL */ #include /* uint8_t, uint16_t, uint32_t */ -#include /* FILE, sprintf(), fflush() */ +#include /* FILE, printf(), sprintf(), fflush() */ #include /* free(), atoi() */ #include /* strlen(), strcmp() strncmp(), strdup() */ #include /* access() */ @@ -311,7 +311,8 @@ static void record_msg(char * msg) char * f_name = "record_msg()"; 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]); + int test = sprintf(path_tmp, "%s%s", s[PATH_RECORD], s[PATH_SUFFIX_TMP]); + exit_trouble(test < 0, f_name, "sprintf()"); FILE * file_tmp = try_fopen(path_tmp, "w", f_name); if (!access(s[PATH_RECORD], F_OK)) { diff --git a/src/server/thing_actions.c b/src/server/thing_actions.c index bb8f585..c1e337b 100644 --- a/src/server/thing_actions.c +++ b/src/server/thing_actions.c @@ -6,7 +6,7 @@ #include /* sprintf() */ #include /* free() */ #include /* strlen(), strcmp(), memcpy(), strncmp() */ -#include "../common/rexit.h" /* exit_err() */ +#include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "../common/yx_uint8.h" /* struct yx_uint8 */ #include "field_of_view.h" /* build_fov_map() */ @@ -104,7 +104,8 @@ static void update_log(char * text) uint16_t len_whole = len_old + len_new + 1; char * new_text = try_malloc(len_whole, f_name); memcpy(new_text, world.log + offset, len_old); - sprintf(new_text + len_old, "%s", text); + int test = sprintf(new_text + len_old, "%s", text); + exit_trouble(test < 0, f_name, "sprintf()"); free(world.log); world.log = new_text; } @@ -131,7 +132,8 @@ static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted) } uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2; char * msg = try_malloc(len, f_name); - sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3); + int test = sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3); + exit_trouble(test < 0, f_name, "sprintf()"); update_log(msg); free(msg); hitted->lifepoints--; @@ -186,7 +188,8 @@ static void playerbonus_move(char d, uint8_t passable) dsc_move = "You fail to move "; } char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, f_name); - sprintf(msg, "\n%s%s.", dsc_move, dsc_dir); + int test = sprintf(msg, "\n%s%s.", dsc_move, dsc_dir); + exit_trouble(test < 0, f_name, "sprintf()"); update_log(msg); free(msg); } diff --git a/src/server/things.c b/src/server/things.c index 81d5931..eabded2 100644 --- a/src/server/things.c +++ b/src/server/things.c @@ -5,7 +5,7 @@ #include /* uint8_t, uint16_t, UINT8_MAX, UINT16_MAX */ #include /* free() */ #include /* memset(), strlen() */ -#include "../common/rexit.h" /* exit_err() */ +#include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "../common/yx_uint8.h" /* yx_uint8 */ #include "map.h" /* is_passable() */ @@ -192,7 +192,7 @@ extern struct ThingType * get_thing_type(uint8_t id) char * err_intro = "Requested thing type of unused ID "; uint16_t size = strlen(err_intro) + 3 + 1 + 1; char * err = try_malloc(size, f_name); - sprintf(err, "%s%d.", err_intro, id); + exit_trouble(sprintf(err, "%s%d.", err_intro, id) < 0, f_name, "sprintf()"); exit_err(NULL == tt, err); free(err); return tt;