Check return values of malloc, realloc, strdup and strndup

This commit is contained in:
Vysheng 2014-09-11 13:24:06 +04:00
parent 3f9d921782
commit 2e0a7fda26
6 changed files with 46 additions and 8 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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 ("");

2
loop.c
View File

@ -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);
}

View File

@ -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;

View File

@ -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;