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