summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-06-02 02:43:58 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-06-02 02:43:58 -0700
commit823b81363a1145de4b8660153007b11faf4e2c56 (patch)
tree09d9c70e232c1b98ec8447cae6b6618bcd91497d /web
parent1db534799ef5b41114d80fce0c38f1c93235a8a3 (diff)
downloaddartboat-823b81363a1145de4b8660153007b11faf4e2c56.tar.gz
dartboat-823b81363a1145de4b8660153007b11faf4e2c56.tar.xz
web: fix minor mem leaks and bugs
Diffstat (limited to 'web')
-rw-r--r--web/web_control.c4
-rw-r--r--web/web_match.c2
-rw-r--r--web/web_misc.c8
-rw-r--r--web/web_misc.h1
-rw-r--r--web/web_opts.c4
-rw-r--r--web/web_prompt.c17
-rw-r--r--web/web_scoreboard.c28
7 files changed, 52 insertions, 12 deletions
diff --git a/web/web_control.c b/web/web_control.c
index 07d52b1..7fdec9a 100644
--- a/web/web_control.c
+++ b/web/web_control.c
@@ -52,8 +52,10 @@ void end_boat_visit(int rem, double avg, int match_id)
EMSCRIPTEN_KEEPALIVE
void draw_boat_throwing(int pts, char *str, double x, double y, int match_id)
{
- if (!state || match_id != state->id)
+ if (!state || match_id != state->id) {
+ free(str);
return;
+ }
char pts_str[10];
sprintf(pts_str, "%d", pts);
diff --git a/web/web_match.c b/web/web_match.c
index 93ebf76..6e6f79d 100644
--- a/web/web_match.c
+++ b/web/web_match.c
@@ -22,7 +22,7 @@ void match_new()
void free_state()
{
- free(state->m);
+ match_free(state->m);
free(state);
state = NULL;
}
diff --git a/web/web_misc.c b/web/web_misc.c
index a754f9e..dcb386e 100644
--- a/web/web_misc.c
+++ b/web/web_misc.c
@@ -27,3 +27,11 @@ char *bufstr_flush(bufstr *b)
b->fl = b->buf;
return b->fl;
}
+
+void bufstr_clear(bufstr *b)
+{
+ if (b->buf != b->fl)
+ free(b->buf);
+ free(b->fl);
+ b->fl = b->buf = NULL;
+}
diff --git a/web/web_misc.h b/web/web_misc.h
index 9872181..0d75012 100644
--- a/web/web_misc.h
+++ b/web/web_misc.h
@@ -10,5 +10,6 @@ typedef struct bufstr {
void bufstr_buf(bufstr *b, char *str);
bool bufstr_changed(bufstr *b);
char *bufstr_flush(bufstr *b);
+void bufstr_clear(bufstr *b);
#endif
diff --git a/web/web_opts.c b/web/web_opts.c
index b880fd3..18a8c2a 100644
--- a/web/web_opts.c
+++ b/web/web_opts.c
@@ -19,12 +19,12 @@ static int difficulty = 32;
static void set_delay(char *val)
{
- delay_ms = atoi(val);
+ delay_ms = val ? atoi(val) : 0;
}
static void set_difficulty(char *val)
{
- int diff = atoi(val);
+ int diff = val ? atoi(val) : 0;
difficulty = diff < 0 ? 0 : diff > 99 ? 99 : diff;
comp_set_difficulty(difficulty);
diff --git a/web/web_prompt.c b/web/web_prompt.c
index 37745e8..9fefa7a 100644
--- a/web/web_prompt.c
+++ b/web/web_prompt.c
@@ -486,9 +486,14 @@ static void prompt_handle_on_change()
if (pm != PM_VISIT)
return;
+ int pts = 0;
char *str = prompt_get();
- update_user_rem_from_pts(atoi(str));
- free(str);
+ if (str) {
+ pts = atoi(str);
+ free(str);
+ }
+
+ update_user_rem_from_pts(pts);
}
static void prompt_handle_append(char *data)
@@ -499,7 +504,7 @@ static void prompt_handle_append(char *data)
return;
char *str = prompt_get();
- size_t len_str = strlen(str);
+ size_t len_str = str ? strlen(str) : 0;
size_t len_data = strlen(data);
if (len_str < 3) {
str = realloc(str, len_str + len_data + 1);
@@ -516,6 +521,8 @@ static void prompt_handle_backspace()
return;
char *str = prompt_get();
+ if (!str)
+ return;
size_t len_str = strlen(str);
if (len_str > 0) {
str[len_str-1] = 0;
@@ -549,6 +556,8 @@ static void prompt_handle_submit()
return;
char *str = prompt_get();
+ if (!str)
+ return;
prompt_handle_clear();
if (*str) {
if (pm == PM_VISIT)
@@ -575,6 +584,8 @@ static void prompt_handle_rem()
return;
char *str = prompt_get();
+ if (!str)
+ return;
prompt_handle_clear();
if (*str)
user_visit_to_rem(atoi(str));
diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c
index ed25b33..a5001f7 100644
--- a/web/web_scoreboard.c
+++ b/web/web_scoreboard.c
@@ -16,11 +16,26 @@
static struct {
bufstr name, rem, sugg, avg;
} *bufs;
+static int n_bufs;
#define NUM_SLOTS 2
static int slots[NUM_SLOTS];
static int active_pn;
+static void free_bufs()
+{
+ for (int i = 0; i < n_bufs; ++i) {
+ bufstr_clear(&bufs[i].name);
+ bufstr_clear(&bufs[i].rem);
+ bufstr_clear(&bufs[i].sugg);
+ bufstr_clear(&bufs[i].avg);
+ }
+
+ free(bufs);
+ bufs = NULL;
+ n_bufs = 0;
+}
+
static void flush_slot(int slot)
{
int pn = slots[slot - 1];
@@ -212,8 +227,10 @@ void update_player_rem(int pn, int rem)
void scoreboard_show_info(int num_players)
{
- if (bufs) free(bufs);
- bufs = calloc(num_players, sizeof(*bufs));
+ if (bufs)
+ free_bufs();
+ n_bufs = num_players;
+ bufs = calloc(n_bufs, sizeof(*bufs));
for (int i = 1; i <= num_players && i <= NUM_SLOTS; ++i) {
set_slot_player(i, i);
@@ -231,8 +248,8 @@ void scoreboard_show_info(int num_players)
void scoreboard_hide_info()
{
- free(bufs);
- bufs = NULL;
+ if (bufs)
+ free_bufs();
memset(slots, 0, sizeof(slots));
dom_set_uniq_class(NULL, "visible", ".info-slot .inner");
@@ -379,7 +396,8 @@ void draw_visits()
if (state->m->n_players != 1 && state->m->legs[1]->n_visits > n_visits)
n_visits = state->m->legs[1]->n_visits;
- struct dom_elem **elemv = malloc(7 * (n_visits + 1) * sizeof(*elemv));
+ struct dom_elem **elemv = malloc(
+ (9 + (7 * n_visits)) * sizeof(*elemv));
int elemc = 0;
elemv[elemc++] = create_div("", "visit-p1-spacer");