From 3f18c6f8dee3ad40c122daa4f57e8575f4d408eb Mon Sep 17 00:00:00 2001 From: jfreegman Date: Mon, 13 Dec 2021 12:33:52 -0500 Subject: [PATCH] cleanup: fix some uninitialized memory warnings and clarify some logic --- src/game_base.c | 20 ++++++++++---------- src/game_chess.c | 10 ++++++---- src/line_info.c | 11 ++++++----- src/name_lookup.c | 5 +++-- src/windows.c | 4 ++-- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/game_base.c b/src/game_base.c index 253abcb..dff68b2 100644 --- a/src/game_base.c +++ b/src/game_base.c @@ -517,10 +517,10 @@ static void game_draw_help_bar(const GameData *game, WINDOW *win) { int max_x; int max_y; - UNUSED_VAR(max_x); - getmaxyx(win, max_y, max_x); + UNUSED_VAR(max_x); + wmove(win, max_y - 1, 1); if (!game->is_multiplayer) { @@ -937,10 +937,10 @@ int game_y_bottom_bound(const GameData *game) { int max_x; int max_y; - UNUSED_VAR(max_x); - getmaxyx(game->window, max_y, max_x); + UNUSED_VAR(max_x); + return ((max_y + game->game_max_y) / 2) - 1; } @@ -948,10 +948,10 @@ int game_y_top_bound(const GameData *game) { int max_x; int max_y; - UNUSED_VAR(max_x); - getmaxyx(game->window, max_y, max_x); + UNUSED_VAR(max_x); + return ((max_y - game->game_max_y) / 2) + 1; } @@ -959,10 +959,10 @@ int game_x_right_bound(const GameData *game) { int max_x; int max_y; - UNUSED_VAR(max_y); - getmaxyx(game->window, max_y, max_x); + UNUSED_VAR(max_y); + return ((max_x + game->game_max_x) / 2) - 1; } @@ -970,10 +970,10 @@ int game_x_left_bound(const GameData *game) { int max_x; int max_y; - UNUSED_VAR(max_y); - getmaxyx(game->window, max_y, max_x); + UNUSED_VAR(max_y); + return ((max_x - game->game_max_x) / 2) + 1; } diff --git a/src/game_chess.c b/src/game_chess.c index d530bfa..d3de78f 100644 --- a/src/game_chess.c +++ b/src/game_chess.c @@ -804,11 +804,13 @@ static bool chess_mock_move_valid(ChessState *state, const Player *player, Tile { Board *board = &state->board; + const bool en_passant = player->en_passant_move_number == -1; bool in_check = false; Tile *ep_tile = NULL; Piece ep_piece; + memset(&ep_piece, 0, sizeof(ep_piece)); - if (player->en_passant_move_number == -1) { // remove piece that was captured via en passant + if (en_passant) { // remove piece that was captured via en passant ChessCoords ep_coords; ep_coords.N = player->colour == White ? to->chess_coords.N - 1 : to->chess_coords.N + 1; ep_coords.L = to->chess_coords.L; @@ -819,7 +821,7 @@ static bool chess_mock_move_valid(ChessState *state, const Player *player, Tile } chess_copy_piece(&ep_piece, &ep_tile->piece); - ep_tile->piece.type = NoPiece; + ep_tile->piece.type = NoPiece; // temporarily remove piece } Piece from_piece; @@ -835,8 +837,8 @@ static bool chess_mock_move_valid(ChessState *state, const Player *player, Tile from->piece.type = from_piece.type; chess_copy_piece(&to->piece, &to_piece); - if (player->en_passant_move_number == -1) { - ep_tile->piece.type = ep_piece.type; + if (en_passant) { + ep_tile->piece.type = ep_piece.type; // reset piece to original state } return !in_check; diff --git a/src/line_info.c b/src/line_info.c index ae49cd5..c880c85 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -148,9 +148,10 @@ static int print_n_chars(WINDOW *win, const char *s, size_t n, int max_y) int x; int y; - UNUSED_VAR(x); getyx(win, y, x); + UNUSED_VAR(x); + // make sure cursor will wrap correctly after newline to prevent display bugs if (y + 1 >= max_y) { return -1; @@ -324,10 +325,10 @@ static void line_info_init_line(ToxWindow *self, struct line_info *line) { int y2; int x2; - UNUSED_VAR(y2); - getmaxyx(self->window, y2, x2); + UNUSED_VAR(y2); + const int max_y = y2 - CHATBOX_HEIGHT - WINDOW_BAR_HEIGHT; const int max_x = self->show_peerlist ? x2 - 1 - SIDEBAR_WIDTH : x2; @@ -522,10 +523,10 @@ void line_info_print(ToxWindow *self) while (line && numlines++ <= max_y && print_ret == 0) { int y; int x; - UNUSED_VAR(y); - getyx(win, y, x); + UNUSED_VAR(y); + if (x > 0) { // Prevents us from printing off the screen break; } diff --git a/src/name_lookup.c b/src/name_lookup.c index ad6bef4..082edc2 100644 --- a/src/name_lookup.c +++ b/src/name_lookup.c @@ -108,14 +108,15 @@ static int load_nameserver_list(const char *path) char line[MAX_SERVER_LINE]; while (fgets(line, sizeof(line), fp) && Nameservers.lines < MAX_SERVERS) { - int linelen = strlen(line); + size_t linelen = strlen(line); if (linelen < SERVER_KEY_SIZE * 2 + 5) { continue; } if (line[linelen - 1] == '\n') { - line[--linelen] = '\0'; + --linelen; + line[linelen] = '\0'; } const char *name = strtok(line, " "); diff --git a/src/windows.c b/src/windows.c index fb65817..37d3a10 100644 --- a/src/windows.c +++ b/src/windows.c @@ -685,10 +685,10 @@ void draw_window_bar(ToxWindow *self) int cur_x; int cur_y; - UNUSED_VAR(cur_y); - getyx(win, cur_y, cur_x); + UNUSED_VAR(cur_y); + wattron(win, COLOR_PAIR(BAR_TEXT)); mvwhline(win, 0, cur_x, ' ', COLS - cur_x); wattroff(win, COLOR_PAIR(BAR_TEXT));