aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-04-08 11:48:09 +0200
committertomsmeding <tom.smeding@gmail.com>2017-04-08 11:48:09 +0200
commitc45ac5fe425e187d980b5593d7a4e3aa318e78fe (patch)
treefd953e6fc29e70a51ffb70987f0b219611321218
parente9f1a60fcc4e0d6ccc10b0961c0256fa0a729568 (diff)
Memory asprintf wrapper (and small free fix)
-rw-r--r--command.c9
-rw-r--r--memory.c15
-rw-r--r--memory.h4
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 <stdio.h>
#include <string.h>
#include <errno.h>
-#include <assert.h>
#include <sys/time.h>
#include <sys/socket.h>
#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 <stdio.h>
+#include <stdarg.h>
+#include <assert.h>
#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)));