From 2724878bfc09d703f186b77d59c499d78976ea30 Mon Sep 17 00:00:00 2001 From: antma Date: Mon, 13 Jan 2014 16:09:33 +0400 Subject: [PATCH 1/6] fix small memory leaks in the function do_send_photo for the cases of invalid, empty or large file --- queries.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/queries.c b/queries.c index 96c3a91..50fd1de 100644 --- a/queries.c +++ b/queries.c @@ -1462,6 +1462,7 @@ void do_send_photo (int type, peer_id_t to_id, char *file_name) { int fd = open (file_name, O_RDONLY); if (fd < 0) { rprintf ("No such file '%s'\n", file_name); + tfree_str (file_name); return; } struct stat buf; @@ -1469,6 +1470,7 @@ void do_send_photo (int type, peer_id_t to_id, char *file_name) { long long size = buf.st_size; if (size <= 0) { rprintf ("File has zero length\n"); + tfree_str (file_name); close (fd); return; } @@ -1483,6 +1485,14 @@ void do_send_photo (int type, peer_id_t to_id, char *file_name) { f->part_size *= 2; } + if (f->part_size > (512 << 10)) { + close (fd); + rprintf ("Too big file. Maximal supported size is %d.\n", (512 << 10) * 1000); + tfree (f, sizeof (*f)); + tfree_str (file_name); + return; + } + f->id = lrand48 () * (1ll << 32) + lrand48 (); f->to_id = to_id; f->media_type = type; @@ -1496,11 +1506,6 @@ void do_send_photo (int type, peer_id_t to_id, char *file_name) { f->key = talloc (32); secure_random (f->key, 32); } - if (f->part_size > (512 << 10)) { - close (fd); - rprintf ("Too big file. Maximal supported size is %d", (512 << 10) * 1000); - return; - } if (f->media_type == CODE_input_media_uploaded_video && !f->encr) { f->media_type = CODE_input_media_uploaded_thumb_video; send_file_thumb (f); From 5de0d759421084b9205269901806c7d914892bd9 Mon Sep 17 00:00:00 2001 From: antma Date: Mon, 13 Jan 2014 16:26:48 +0400 Subject: [PATCH 2/6] add functions tstrndup (auto check return value of strndup calls) --- binlog.c | 2 +- interface.c | 10 +++++----- queries.c | 2 +- tools.c | 17 +++++++++++++++++ tools.h | 1 + 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/binlog.c b/binlog.c index 00dc12b..5e908db 100644 --- a/binlog.c +++ b/binlog.c @@ -100,7 +100,7 @@ void replay_log_event (void) { if (verbosity) { logprintf ( "id = %d, name = %.*s ip = %.*s port = %d\n", id, l1, name, l2, ip, port); } - alloc_dc (id, strndup (ip, l2), port); + alloc_dc (id, tstrndup (ip, l2), port); } rptr = in_ptr; break; diff --git a/interface.c b/interface.c index 53a245c..71e699c 100644 --- a/interface.c +++ b/interface.c @@ -675,7 +675,7 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - do_send_photo (CODE_input_media_uploaded_photo, id, strndup (s, t)); + do_send_photo (CODE_input_media_uploaded_photo, id, tstrndup (s, t)); } else if (IS_WORD("send_video")) { GET_PEER; int t; @@ -684,7 +684,7 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - do_send_photo (CODE_input_media_uploaded_video, id, strndup (s, t)); + do_send_photo (CODE_input_media_uploaded_video, id, tstrndup (s, t)); } else if (IS_WORD ("send_text")) { GET_PEER; int t; @@ -693,7 +693,7 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - do_send_text (id, strndup (s, t)); + do_send_text (id, tstrndup (s, t)); } else if (IS_WORD ("fwd")) { GET_PEER; int num = next_token_int (); @@ -950,7 +950,7 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - do_send_photo (CODE_input_media_uploaded_audio, id, strndup (s, t)); + do_send_photo (CODE_input_media_uploaded_audio, id, tstrndup (s, t)); } else if (IS_WORD("send_document")) { GET_PEER; int t; @@ -959,7 +959,7 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - do_send_photo (CODE_input_media_uploaded_document, id, strndup (s, t)); + do_send_photo (CODE_input_media_uploaded_document, id, tstrndup (s, t)); } else if (IS_WORD ("load_audio")) { long long num = next_token_int (); if (num == NOT_FOUND) { diff --git a/queries.c b/queries.c index 50fd1de..339e214 100644 --- a/queries.c +++ b/queries.c @@ -386,7 +386,7 @@ int send_code_on_answer (struct query *q UU) { if (phone_code_hash) { tfree_str (phone_code_hash); } - phone_code_hash = strndup (s, l); + phone_code_hash = tstrndup (s, l); want_dc_num = -1; return 0; } diff --git a/tools.c b/tools.c index badf812..f4a4c0a 100644 --- a/tools.c +++ b/tools.c @@ -166,6 +166,23 @@ char *tstrdup (const char *s) { #endif } +char *tstrndup (const char *s, size_t n) { +#ifdef DEBUG + size_t l = 0; + for (l = 0; l < n && s[l]; l++) { } + char *p = talloc (l + 1); + memcpy (p, s, l); + p[l] = 0; + return p; +#else + char *p = strndup (s, n); + if (p == NULL) { + out_of_memory (); + } + return p; +#endif +} + void ensure (int r) { if (!r) { logprintf ("Open SSL error\n"); diff --git a/tools.h b/tools.h index 8105c27..09f2b2b 100644 --- a/tools.h +++ b/tools.h @@ -24,6 +24,7 @@ void *talloc (size_t size); void *trealloc (void *ptr, size_t old_size, size_t size); void *talloc0 (size_t size); char *tstrdup (const char *s); +char *tstrndup (const char *s, size_t n); int tinflate (void *input, int ilen, void *output, int olen); void ensure (int r); void ensure_ptr (void *p); From dd40e9b36f6b2ea851890f80277187715a54fe22 Mon Sep 17 00:00:00 2001 From: antma Date: Mon, 13 Jan 2014 16:36:29 +0400 Subject: [PATCH 3/6] don't output backtrace in out of memory case --- tools.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tools.c b/tools.c index f4a4c0a..208036f 100644 --- a/tools.c +++ b/tools.c @@ -30,6 +30,8 @@ #include "tools.h" #ifdef DEBUG +#define RES_PRE 8 +#define RES_AFTER 8 #define MAX_BLOCKS 1000000 void *blocks[MAX_BLOCKS]; void *free_blocks[MAX_BLOCKS]; @@ -37,18 +39,13 @@ int used_blocks; int free_blocks_cnt; #endif -#ifdef DEBUG -#define RES_PRE 8 -#define RES_AFTER 8 -#endif - extern int verbosity; long long total_allocated_bytes; static void out_of_memory (void) { - logprintf ("Out of memory\n"); - assert (0 && "Out of memory"); + fprintf (stderr, "Out of memory\n"); + exit (1); } int tsnprintf (char *buf, int len, const char *format, ...) { From e27a27c4a093bb8dd6e32743b3f339ebb998ba5a Mon Sep 17 00:00:00 2001 From: antma Date: Mon, 13 Jan 2014 17:05:25 +0400 Subject: [PATCH 4/6] fix #include "config.h" for all source files --- binlog.c | 4 ++++ interface.c | 3 +++ loop.c | 7 +++++-- lua-tg.c | 2 ++ main.c | 6 +++++- mtproto-client.c | 5 +++++ mtproto-common.c | 5 +++++ net.c | 5 +++++ queries.c | 5 +++++ structures.c | 2 ++ tools.c | 4 ++++ 11 files changed, 45 insertions(+), 3 deletions(-) diff --git a/binlog.c b/binlog.c index 5e908db..36407f7 100644 --- a/binlog.c +++ b/binlog.c @@ -16,7 +16,11 @@ Copyright Vitaly Valtman 2013 */ + +#ifdef HAVE_CONFIG_H #include "config.h" +#endif + #ifdef USE_LUA # include "lua-tg.h" #endif diff --git a/interface.c b/interface.c index 71e699c..24ac58f 100644 --- a/interface.c +++ b/interface.c @@ -17,7 +17,10 @@ Copyright Vitaly Valtman 2013 */ +#ifdef HAVE_CONFIG_H #include "config.h" +#endif + #define _GNU_SOURCE #include diff --git a/loop.c b/loop.c index fa06a2c..c8bc2e7 100644 --- a/loop.c +++ b/loop.c @@ -16,13 +16,16 @@ Copyright Vitaly Valtman 2013 */ -#define READLINE_CALLBACKS + +#ifdef HAVE_CONFIG_H #include "config.h" +#endif + +#define READLINE_CALLBACKS #define _GNU_SOURCE #include #include - #include #include #ifdef READLINE_GNU diff --git a/lua-tg.c b/lua-tg.c index c3ff97e..62d9bb7 100644 --- a/lua-tg.c +++ b/lua-tg.c @@ -1,4 +1,6 @@ +#ifdef HAVE_CONFIG_H #include "config.h" +#endif #ifdef USE_LUA #include "lua-tg.h" diff --git a/main.c b/main.c index a832109..88410be 100644 --- a/main.c +++ b/main.c @@ -16,8 +16,12 @@ Copyright Vitaly Valtman 2013 */ -#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H #include "config.h" +#endif + +#define _GNU_SOURCE #include #include diff --git a/mtproto-client.c b/mtproto-client.c index 7f1d592..844af9c 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -17,6 +17,11 @@ Copyright Nikolay Durov, Andrey Lopatin 2012-2013 Copyright Vitaly Valtman 2013 */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #define _FILE_OFFSET_BITS 64 #include diff --git a/mtproto-common.c b/mtproto-common.c index 6d958de..12efcca 100644 --- a/mtproto-common.c +++ b/mtproto-common.c @@ -17,6 +17,11 @@ Copyright Nikolay Durov, Andrey Lopatin 2012-2013 Copyright Vitaly Valtman 2013 */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #define _FILE_OFFSET_BITS 64 #include diff --git a/net.c b/net.c index 6c672fc..bbc9a48 100644 --- a/net.c +++ b/net.c @@ -16,6 +16,11 @@ Copyright Vitaly Valtman 2013 */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #define _GNU_SOURCE #include #include diff --git a/queries.c b/queries.c index 339e214..de0232b 100644 --- a/queries.c +++ b/queries.c @@ -16,6 +16,11 @@ Copyright Vitaly Valtman 2013 */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #define _FILE_OFFSET_BITS 64 #include #include diff --git a/structures.c b/structures.c index 0bde616..81af89b 100644 --- a/structures.c +++ b/structures.c @@ -17,7 +17,9 @@ Copyright Vitaly Valtman 2013 */ +#ifdef HAVE_CONFIG_H #include "config.h" +#endif #include #include diff --git a/tools.c b/tools.c index 208036f..74ff5d2 100644 --- a/tools.c +++ b/tools.c @@ -17,6 +17,10 @@ Copyright Vitaly Valtman 2013 */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #define _GNU_SOURCE #include From c4fd38a894e69cfd34c15cfe5263f2b53c63db35 Mon Sep 17 00:00:00 2001 From: antma Date: Mon, 13 Jan 2014 17:12:39 +0400 Subject: [PATCH 5/6] remove useless #define _GNU_SOURCE from not using asprintf sources --- loop.c | 1 - main.c | 2 -- 2 files changed, 3 deletions(-) diff --git a/loop.c b/loop.c index c8bc2e7..f23f0f3 100644 --- a/loop.c +++ b/loop.c @@ -22,7 +22,6 @@ #endif #define READLINE_CALLBACKS -#define _GNU_SOURCE #include #include diff --git a/main.c b/main.c index 88410be..de39372 100644 --- a/main.c +++ b/main.c @@ -21,8 +21,6 @@ #include "config.h" #endif -#define _GNU_SOURCE - #include #include #include From 6b87e312770a7db5d8993ec19afbb7dd6abbbe07 Mon Sep 17 00:00:00 2001 From: antma Date: Mon, 13 Jan 2014 17:28:24 +0400 Subject: [PATCH 6/6] fix some autoscan warnings --- configure.ac | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index b4a9ca7..d56598d 100644 --- a/configure.ac +++ b/configure.ac @@ -99,15 +99,17 @@ AC_ARG_ENABLE(liblua,[--enable-liblua/--disable-liblua], ]) # Checks for header files. -AC_CHECK_HEADERS([fcntl.h malloc.h stdlib.h string.h sys/socket.h unistd.h]) +AC_CHECK_HEADERS([fcntl.h malloc.h netdb.h stdlib.h string.h unistd.h arpa/inet.h mach/mach.h netinet/in.h sys/file.h sys/socket.h termios.h]) # Checks for typedefs, structures, and compiler characteristics. AC_TYPE_SIZE_T +AC_TYPE_UID_T +AC_C_INLINE # Checks for library functions. AC_FUNC_MALLOC AC_FUNC_REALLOC -AC_CHECK_FUNCS([memset select strdup strndup]) +AC_CHECK_FUNCS([alarm endpwent memset memmove mkdir select socket strdup strndup uname]) AC_SUBST(EXTRA_LIBS) AC_CONFIG_FILES([Makefile])