From cb43b28c8f344628dc873ac41605dc7c28eed18b Mon Sep 17 00:00:00 2001 From: fall-leaf Date: Fri, 4 Aug 2023 16:42:41 +0300 Subject: [PATCH] added reallocation to sql callback --- src/api.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/api.c b/src/api.c index 8c2d508..cbf0ae8 100644 --- a/src/api.c +++ b/src/api.c @@ -147,7 +147,27 @@ rtos(void * data, ){ (void) colname; - char *const *const r = (char**)data; + char ** r = (char**)data; + + size_t data_len = 0; + for(int i = 0; i < argc; i++) + { + if(argv[i]) + { + data_len += strlen(argv[i]); + } + else + { + /* strlen("NULL") == 4 */ + data_len += 4; + } + /* strlen("|") * 2 == 2 */ + data_len += 2; + } + ++data_len; + + *r = (char *)calloc(data_len, sizeof(char)); + for(int i = 0; i < argc; i++){ strcat(*r, "|"); if(argv[i]){ @@ -167,9 +187,10 @@ DECL char * dump() { char* errmsg; - /* TODO: allow for reallocing in rtos, start with a smaller value */ - char* r = (char*)calloc(sizeof(char), 10000); + char* r = NULL; + DBERR(sqlite3_exec(connection, dump_stmt, rtos, &r, &errmsg)); + return r; } @@ -177,9 +198,10 @@ DECL char * raw(char const * const sql) { char* errmsg; - /* TODO: allow for reallocing in rtos, start with a smaller value */ - char *r = (char*)calloc(sizeof(char), 10000); + char *r = NULL; + sqlite3_exec(connection, sql, rtos, &r, &errmsg); + if (errmsg){ free(r); r = errmsg;