Merge pull request #37 from antma/master

checks strndup result & enhance configure.ac
This commit is contained in:
vysheng 2014-01-13 05:57:22 -08:00
commit 19851f9ef2
13 changed files with 86 additions and 25 deletions

View File

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

View File

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

View File

@ -17,7 +17,10 @@
Copyright Vitaly Valtman 2013
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define _GNU_SOURCE
#include <assert.h>
@ -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) {

8
loop.c
View File

@ -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 <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef READLINE_GNU

View File

@ -1,4 +1,6 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef USE_LUA
#include "lua-tg.h"

4
main.c
View File

@ -16,8 +16,10 @@
Copyright Vitaly Valtman 2013
*/
#define _GNU_SOURCE
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <unistd.h>

View File

@ -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 <assert.h>

View File

@ -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 <assert.h>

5
net.c
View File

@ -16,6 +16,11 @@
Copyright Vitaly Valtman 2013
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>

View File

@ -16,6 +16,11 @@
Copyright Vitaly Valtman 2013
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define _FILE_OFFSET_BITS 64
#include <string.h>
#include <memory.h>
@ -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);

View File

@ -17,7 +17,9 @@
Copyright Vitaly Valtman 2013
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <string.h>

32
tools.c
View File

@ -17,6 +17,10 @@
Copyright Vitaly Valtman 2013
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define _GNU_SOURCE
#include <assert.h>
@ -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");

View File

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