From 2e0a7fda26d4e539020a815c4daf955cd3f2db59 Mon Sep 17 00:00:00 2001 From: Vysheng Date: Thu, 11 Sep 2014 13:24:06 +0400 Subject: [PATCH] Check return values of malloc, realloc, strdup and strndup --- auto-static.c | 6 ++++++ generate.c | 8 +++++++- interface.c | 32 +++++++++++++++++++++++++------- loop.c | 2 ++ lua-tg.c | 2 ++ structures.c | 4 ++++ 6 files changed, 46 insertions(+), 8 deletions(-) diff --git a/auto-static.c b/auto-static.c index 2e883ce..2e32465 100644 --- a/auto-static.c +++ b/auto-static.c @@ -57,6 +57,7 @@ static int (*autocomplete_fun)(const char *, int, int, char **); static void set_autocomplete_string (const char *s) { if (autocomplete_string) { free (autocomplete_string); } autocomplete_string = strdup (s); + assert (autocomplete_string); autocomplete_mode = 1; } @@ -116,12 +117,16 @@ static double get_double (void) { static struct paramed_type *paramed_type_dup (struct paramed_type *P) { if (ODDP (P)) { return P; } struct paramed_type *R = malloc (sizeof (*R)); + assert (R); R->type = malloc (sizeof (*R->type)); + assert (R->type); memcpy (R->type, P->type, sizeof (*P->type)); R->type->id = strdup (P->type->id); + assert (R->type->id); if (P->type->params_num) { R->params = malloc (sizeof (void *) * P->type->params_num); + assert (R->params); int i; for (i = 0; i < P->type->params_num; i++) { R->params[i] = paramed_type_dup (P->params[i]); @@ -287,6 +292,7 @@ int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, index = 0; if (!strncmp (text, autocomplete_string, len)) { *R = strdup (autocomplete_string); + assert (*R); return index; } else { return -1; diff --git a/generate.c b/generate.c index 3b0900d..692b44d 100644 --- a/generate.c +++ b/generate.c @@ -99,6 +99,7 @@ long long get_long (void) { static void *malloc0 (int size) { void *r = malloc (size); + assert (r); memset (r, 0, size); return r; } @@ -126,7 +127,9 @@ char *get_string (void) { buf_ptr += tlen / 4; assert (buf_ptr <= buf_end); - return strndup (res, len); + char *r = strndup (res, len); + assert (r); + return r; } @@ -144,6 +147,7 @@ int read_args_list (struct arg **args, int args_num, int *var_num); void *int_to_var_nat_const_init (long long x) { if (use_var_nat_full_form (x)) { struct tl_tree_nat_const *T = malloc (sizeof (*T)); + assert (T); T->self.flags = 0; T->self.methods = &tl_pnat_const_full_methods; T->value = x; @@ -1460,6 +1464,7 @@ struct tl_combinator *read_combinators (int v) { c->name = get_int (); c->id = get_string (); c->print_id = strdup (gen_print_id (c->id)); + assert (c->print_id); //char *s = c->id; //while (*s) { if (*s == '.') { *s = '_'; } ; s ++;} int x = get_int (); @@ -1485,6 +1490,7 @@ struct tl_type *read_types (void) { t->name = get_int (); t->id = get_string (); t->print_id = strdup (gen_print_id (t->id)); + assert (t->print_id); t->constructors_num = get_int (); assert (t->constructors_num >= 0 && t->constructors_num <= 1000); diff --git a/interface.c b/interface.c index bbcd0bc..6d3dfa2 100644 --- a/interface.c +++ b/interface.c @@ -461,6 +461,7 @@ int complete_string_list (char **list, int index, const char *text, int len, cha } if (list[index]) { *R = strdup (list[index]); + assert (*R); return index; } else { *R = 0; @@ -475,6 +476,7 @@ int complete_command_list (int index, const char *text, int len, char **R) { } if (commands[index].name) { *R = strdup (commands[index].name); + assert (*R); return index; } else { *R = 0; @@ -1163,7 +1165,9 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - tgl_do_send_photo (tgl_message_media_photo, id, strndup (s, t), 0, 0); + char *d = strndup (s, t); + assert (d); + tgl_do_send_photo (tgl_message_media_photo, id, d, 0, 0); } else if (IS_WORD ("chat_set_photo")) { GET_PEER_CHAT; int t; @@ -1172,7 +1176,9 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - tgl_do_set_chat_photo (id, strndup (s, t), 0, 0); + char *d = strndup (s, t); + assert (d); + tgl_do_set_chat_photo (id, d, 0, 0); } else if (IS_WORD ("set_profile_photo")) { int t; char *s = end_string_token (&t); @@ -1180,7 +1186,9 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - tgl_do_set_profile_photo (strndup (s, t), 0, 0); + char *d = strndup (s, t); + assert (d); + tgl_do_set_profile_photo (d, 0, 0); } else if (IS_WORD("send_video")) { GET_PEER; int t; @@ -1189,7 +1197,9 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - tgl_do_send_photo (tgl_message_media_video, id, strndup (s, t), 0, 0); + char *d = strndup (s, t); + assert (d); + tgl_do_send_photo (tgl_message_media_video, id, d, 0, 0); } else if (IS_WORD ("send_text")) { GET_PEER; int t; @@ -1198,7 +1208,9 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - tgl_do_send_text (id, strndup (s, t), 0, 0); + char *d = strndup (s, t); + assert (d); + tgl_do_send_text (id, d, 0, 0); } else if (IS_WORD ("fwd")) { GET_PEER; int num = next_token_int (); @@ -1533,7 +1545,9 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - tgl_do_send_photo (tgl_message_media_audio, id, strndup (s, t), 0, 0); + char *d = strndup (s, t); + assert (d); + tgl_do_send_photo (tgl_message_media_audio, id, d, 0, 0); } else if (IS_WORD("send_document")) { GET_PEER; int t; @@ -1542,7 +1556,9 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - tgl_do_send_photo (tgl_message_media_document, id, strndup (s, t), 0, 0); + char *d = strndup (s, t); + assert (d); + tgl_do_send_photo (tgl_message_media_document, id, d, 0, 0); } else if (IS_WORD ("load_audio")) { long long num = next_token_int (); if (num == NOT_FOUND) { @@ -1739,6 +1755,7 @@ void print_start (void) { saved_point = rl_point; #ifdef READLINE_GNU saved_line = malloc (rl_end + 1); + assert (saved_line); saved_line[rl_end] = 0; memcpy (saved_line, rl_line_buffer, rl_end); @@ -1747,6 +1764,7 @@ void print_start (void) { #else assert (rl_end >= 0); saved_line = malloc (rl_end + 1); + assert (saved_line); memcpy (saved_line, rl_line_buffer, rl_end + 1); rl_line_buffer[0] = 0; set_prompt (""); diff --git a/loop.c b/loop.c index be396c0..5faf622 100644 --- a/loop.c +++ b/loop.c @@ -96,6 +96,7 @@ static void stdin_read_callback_all (int arg, short what, struct event *self) { while (1) { if (line_buffer_pos == line_buffer_size) { line_buffer = realloc (line_buffer, line_buffer_size * 2 + 100); + assert (line_buffer); line_buffer_size = line_buffer_size * 2 + 100; assert (line_buffer); } @@ -267,6 +268,7 @@ void sign_in_callback (void *extra, int success, int registered, const char *mha } should_register = !registered; hash = strdup (mhash); + assert (hash); } diff --git a/lua-tg.c b/lua-tg.c index 70845fc..ec302ca 100644 --- a/lua-tg.c +++ b/lua-tg.c @@ -1181,6 +1181,7 @@ static int parse_lua_function (lua_State *L, struct lua_function *F) { int a2 = luaL_ref (L, LUA_REGISTRYINDEX); struct lua_query_extra *e = malloc (sizeof (*e)); + assert (e); e->func = a2; e->param = a1; @@ -1357,6 +1358,7 @@ static int postpone_from_lua (lua_State *L) { int *t = malloc (16); + assert (t); struct event *ev = evtimer_new (tgl_state.ev_base, lua_postpone_alarm, t); t[0] = a1; t[1] = a2; diff --git a/structures.c b/structures.c index 178d97c..7a555db 100644 --- a/structures.c +++ b/structures.c @@ -1754,6 +1754,7 @@ int tgl_complete_user_list (int index, const char *text, int len, char **R) { } if (index < peer_num) { *R = strdup (Peers[index]->print_name); + assert (*R); return index; } else { return -1; @@ -1767,6 +1768,7 @@ int tgl_complete_chat_list (int index, const char *text, int len, char **R) { } if (index < peer_num) { *R = strdup (Peers[index]->print_name); + assert (*R); return index; } else { return -1; @@ -1780,6 +1782,7 @@ int tgl_complete_encr_chat_list (int index, const char *text, int len, char **R) } if (index < peer_num) { *R = strdup (Peers[index]->print_name); + assert (*R); return index; } else { return -1; @@ -1793,6 +1796,7 @@ int tgl_complete_peer_list (int index, const char *text, int len, char **R) { } if (index < peer_num) { *R = strdup (Peers[index]->print_name); + assert (*R); return index; } else { return -1;