diff --git a/binlog.c b/binlog.c index 00dc12b..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 @@ -100,7 +104,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/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]) diff --git a/interface.c b/interface.c index 53a245c..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 @@ -675,7 +678,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 +687,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 +696,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 +953,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 +962,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/loop.c b/loop.c index fa06a2c..f23f0f3 100644 --- a/loop.c +++ b/loop.c @@ -16,13 +16,15 @@ Copyright Vitaly Valtman 2013 */ -#define READLINE_CALLBACKS + +#ifdef HAVE_CONFIG_H #include "config.h" -#define _GNU_SOURCE +#endif + +#define READLINE_CALLBACKS #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..de39372 100644 --- a/main.c +++ b/main.c @@ -16,8 +16,10 @@ Copyright Vitaly Valtman 2013 */ -#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H #include "config.h" +#endif #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 96c3a91..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 @@ -386,7 +391,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; } @@ -1462,6 +1467,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 +1475,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 +1490,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 +1511,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); 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 badf812..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 @@ -30,6 +34,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 +43,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, ...) { @@ -166,6 +167,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);