From c45ac5fe425e187d980b5593d7a4e3aa318e78fe Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 8 Apr 2017 11:48:09 +0200 Subject: Memory asprintf wrapper (and small free fix) --- command.c | 9 +-------- memory.c | 15 +++++++++++++++ memory.h | 4 ++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/command.c b/command.c index 6edf4ea..13d8252 100644 --- a/command.c +++ b/command.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include "command.h" @@ -34,7 +33,6 @@ static bool send_raw_text(int fd,const char *text,i64 len){ static bool send_ok(int fd,const char *tag){ char *buf=NULL; i64 len=asprintf(&buf,"%s ok\n",tag); - assert(buf); bool closed=send_raw_text(fd,buf,len); free(buf); return closed; @@ -43,7 +41,6 @@ static bool send_ok(int fd,const char *tag){ static bool send_error(int fd,const char *tag,const char *msg){ char *buf=NULL; i64 len=asprintf(&buf,"%s error %s\n",tag,msg); - assert(buf); bool closed=send_raw_text(fd,buf,len); free(buf); return closed; @@ -52,7 +49,6 @@ static bool send_error(int fd,const char *tag,const char *msg){ static bool send_name(int fd,const char *tag,const char *name){ char *buf=NULL; i64 len=asprintf(&buf,"%s name %s\n",tag,name); - assert(buf); bool closed=send_raw_text(fd,buf,len); free(buf); return closed; @@ -61,7 +57,6 @@ static bool send_name(int fd,const char *tag,const char *name){ static bool send_list(int fd,const char *tag,i64 count,const char **list){ char *buf=NULL; i64 len=asprintf(&buf,"%s list %" PRIi64,tag,count); - assert(buf); bool closed=send_raw_text(fd,buf,len); free(buf); if(closed)return true; @@ -262,7 +257,6 @@ static bool cmd_send(struct conn_data *data,const char *tag,const char **args){ char *username=db_get_username(data->userid); i64 pushbuflen=asprintf(&pushbuf,"_push message %s %s %" PRIi64 " %s\n", args[0],username,timestamp,args[1]); - assert(pushbuf); free(username); struct db_user_list members=db_list_members(roomid); @@ -277,6 +271,7 @@ static bool cmd_send(struct conn_data *data,const char *tag,const char **args){ } } + db_nullify_user_list(members); free(pushbuf); return closed; @@ -307,7 +302,6 @@ static bool cmd_history(struct conn_data *data,const char *tag,const char **args struct db_message_list ml=db_get_messages(roomid,nrequested); char *buf=NULL; i64 len=asprintf(&buf,"%s history %" PRIi64 "\n",tag,ml.count); - assert(buf); bool closed=send_raw_text(data->fd,buf,len); free(buf); @@ -320,7 +314,6 @@ static bool cmd_history(struct conn_data *data,const char *tag,const char **args char *username=db_get_username(ml.list[i].userid); len=asprintf(&buf,"%s history_message %" PRIi64 " %s %s %" PRIi64 " %s\n", tag,ml.count-1-i,args[0],username,ml.list[i].timestamp,ml.list[i].message); - assert(buf); closed=send_raw_text(data->fd,buf,len); free(buf); if(closed)break; diff --git a/memory.c b/memory.c index aec3da2..14dddf5 100644 --- a/memory.c +++ b/memory.c @@ -1,3 +1,7 @@ +#define _GNU_SOURCE +#include +#include +#include #include "global.h" #include "memory.h" @@ -14,3 +18,14 @@ void* check_after_allocation_str(const char *func,void *ptr){ } return ptr; } + +__attribute__((format (printf, 2, 3))) +int memory_asprintf_wrapper(char **ret,const char *format,...){ + assert(ret!=NULL); + va_list ap; + va_start(ap,format); + int len=vasprintf(ret,format,ap); + va_end(ap); + check_after_allocation_str("asprintf",*ret); + return len; +} diff --git a/memory.h b/memory.h index 19cfb95..9175dea 100644 --- a/memory.h +++ b/memory.h @@ -10,6 +10,10 @@ ((type*)check_after_allocation("realloc",num,sizeof(type),realloc((ptr),(num)*sizeof(type)))) #define strdup(str) \ ((char*)check_after_allocation_str("strdup",strdup(str))) +#define asprintf(...) \ + (memory_asprintf_wrapper(__VA_ARGS__)) void* check_after_allocation(const char *func,size_t num,size_t sz,void *ptr); void* check_after_allocation_str(const char *func,void *ptr); + +int memory_asprintf_wrapper(char **ret,const char *format,...) __attribute__((format (printf, 2, 3))); -- cgit v1.2.3-70-g09d2