add functions tstrndup (auto check return value of strndup calls)

This commit is contained in:
antma 2014-01-13 16:26:48 +04:00
parent 2724878bfc
commit 5de0d75942
5 changed files with 25 additions and 7 deletions

View File

@ -100,7 +100,7 @@ void replay_log_event (void) {
if (verbosity) { if (verbosity) {
logprintf ( "id = %d, name = %.*s ip = %.*s port = %d\n", id, l1, name, l2, ip, port); 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; rptr = in_ptr;
break; break;

View File

@ -675,7 +675,7 @@ void interpreter (char *line UU) {
printf ("Empty file name\n"); printf ("Empty file name\n");
RET; 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")) { } else if (IS_WORD("send_video")) {
GET_PEER; GET_PEER;
int t; int t;
@ -684,7 +684,7 @@ void interpreter (char *line UU) {
printf ("Empty file name\n"); printf ("Empty file name\n");
RET; 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")) { } else if (IS_WORD ("send_text")) {
GET_PEER; GET_PEER;
int t; int t;
@ -693,7 +693,7 @@ void interpreter (char *line UU) {
printf ("Empty file name\n"); printf ("Empty file name\n");
RET; RET;
} }
do_send_text (id, strndup (s, t)); do_send_text (id, tstrndup (s, t));
} else if (IS_WORD ("fwd")) { } else if (IS_WORD ("fwd")) {
GET_PEER; GET_PEER;
int num = next_token_int (); int num = next_token_int ();
@ -950,7 +950,7 @@ void interpreter (char *line UU) {
printf ("Empty file name\n"); printf ("Empty file name\n");
RET; 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")) { } else if (IS_WORD("send_document")) {
GET_PEER; GET_PEER;
int t; int t;
@ -959,7 +959,7 @@ void interpreter (char *line UU) {
printf ("Empty file name\n"); printf ("Empty file name\n");
RET; 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")) { } else if (IS_WORD ("load_audio")) {
long long num = next_token_int (); long long num = next_token_int ();
if (num == NOT_FOUND) { if (num == NOT_FOUND) {

View File

@ -386,7 +386,7 @@ int send_code_on_answer (struct query *q UU) {
if (phone_code_hash) { if (phone_code_hash) {
tfree_str (phone_code_hash); tfree_str (phone_code_hash);
} }
phone_code_hash = strndup (s, l); phone_code_hash = tstrndup (s, l);
want_dc_num = -1; want_dc_num = -1;
return 0; return 0;
} }

17
tools.c
View File

@ -166,6 +166,23 @@ char *tstrdup (const char *s) {
#endif #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) { void ensure (int r) {
if (!r) { if (!r) {
logprintf ("Open SSL error\n"); 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 *trealloc (void *ptr, size_t old_size, size_t size);
void *talloc0 (size_t size); void *talloc0 (size_t size);
char *tstrdup (const char *s); char *tstrdup (const char *s);
char *tstrndup (const char *s, size_t n);
int tinflate (void *input, int ilen, void *output, int olen); int tinflate (void *input, int ilen, void *output, int olen);
void ensure (int r); void ensure (int r);
void ensure_ptr (void *p); void ensure_ptr (void *p);