libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

commit dcb34072eddffcb78c5ef0ccafd3bd183809df8f
parent 54e746873cd7358219b93a6ba83189bfe3b707d7
Author: lash <dev@holbrook.no>
Date:   Sun, 30 Mar 2025 23:13:20 +0100

Fixing more memory leaks

Diffstat:
Msrc/cli/Makefile | 6++++--
Msrc/crypto/gcrypt.c | 20+++++++++++---------
Msrc/lq/store.h | 7-------
Msrc/store/mem.c | 30++++++++++++++++++++----------
4 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/src/cli/Makefile b/src/cli/Makefile @@ -1,7 +1,9 @@ INCLUDES := -I.. -I../lq -I../aux/include CFLAGS += $(INCLUDES) -Wall -OBJFILES += ../asn1/*.o ../*.o ../lq/*.o ../store/file.o ../mem/std.o ../io/std.o ../crypto/gcrypt.o -LIBS := `pkg-config --libs libtasn1 libgcrypt libxdg-basedir` -L../aux/lib -llash -lcwalk +#OBJFILES += ../asn1/*.o ../*.o ../lq/*.o ../store/file.o ../mem/std.o ../io/std.o ../crypto/gcrypt.o +OBJFILES += ../asn1/*.o ../*.o ../lq/*.o ../store/mem.o ../mem/std.o ../io/std.o ../crypto/gcrypt.o +#LIBS := `pkg-config --libs libtasn1 libgcrypt libxdg-basedir` -L../aux/lib -llash -lcwalk +LIBS := `pkg-config --libs libtasn1 libgcrypt libxdg-basedir` -L../aux/lib -llash -lcwalk -lhashmap LDFLAGS += -L../aux/lib -L../ $(LIBS) all: diff --git a/src/crypto/gcrypt.c b/src/crypto/gcrypt.c @@ -400,11 +400,7 @@ static int key_create_store(struct gpg_store *gpg, const char *passphrase, size_ if (r) { return debug_logerr(LLOG_ERROR, ERR_KEYFAIL, "key create"); } - pubk = lq_publickey_new(gpg->public_key); - if (pubk == NULL) { - return debug_logerr(LLOG_ERROR, ERR_KEYFAIL, "publickey"); - } - + // Export the S-expression to a text buffer for saving, canonical formatting kl = gcry_sexp_sprint(gpg->k, GCRYSEXP_FMT_CANON, NULL, 0); m = (size_t)kl + 1; @@ -434,12 +430,17 @@ static int key_create_store(struct gpg_store *gpg, const char *passphrase, size_ // Export the key (fingerprint) and value (ciphertext) to put in the store. // (We don't need the inner private key pointer anymore, so we re-use it.) - lq_cpy(buf_val, nonce, CHACHA20_NONCE_LENGTH_BYTES); - lq_cpy(buf_val + CHACHA20_NONCE_LENGTH_BYTES, ciphertext, c); + pubk = lq_publickey_new(gpg->public_key); + if (pubk == NULL) { + return debug_logerr(LLOG_ERROR, ERR_KEYFAIL, "publickey"); + } gpg = (struct gpg_store*)pubk->impl; lq_cpy(buf_key, gpg->fingerprint, LQ_FP_LEN); + lq_cpy(buf_val, nonce, CHACHA20_NONCE_LENGTH_BYTES); + lq_cpy(buf_val + CHACHA20_NONCE_LENGTH_BYTES, ciphertext, c); + lq_publickey_free(pubk); - // Instantiate the store. + // Retrieve the store. store = key_store_get(); if (store == NULL) { return debug_logerr(LLOG_ERROR, ERR_KEYFILE, "create store"); @@ -978,7 +979,8 @@ size_t lq_publickey_fingerprint(LQPubKey* pubk, char **out) { } void lq_crypto_free() { - lq_store_free((void*)gpg_key_store); + //lq_store_free((void*)gpg_key_store); + gpg_key_store->free(gpg_key_store); gpg_key_store = NULL; gpg_version = NULL; } diff --git a/src/lq/store.h b/src/lq/store.h @@ -66,11 +66,4 @@ struct lq_resolve_t { */ LQStore* lq_store_new(const char *spec); -/** - * \brief Free resources used by store instance. - * - * \param[in] Store object. - */ -void lq_store_free(LQStore *store); - #endif // LIBQAEDA_STORE_H_ diff --git a/src/store/mem.c b/src/store/mem.c @@ -52,9 +52,17 @@ static long unsigned int pair_hash(const void *item, long unsigned int s0, long return r; } +static void free_item(void *o) { + struct pair_t *v; + + v = (struct pair_t*)o; + debug_x(LLOG_DEBUG, "store.mem", "freeing key", 1, MORGEL_TYP_BIN, v->key_len, "key", v->key); + lq_free((void*)v); +} + struct hashmap* lq_mem_init(LQStore *store) { if (store->userdata == NULL) { - store->userdata = (void*)hashmap_new(sizeof(struct pair_t) , 1024*1024, 0, 0, pair_hash, pair_cmp, NULL, NULL); + store->userdata = (void*)hashmap_new(sizeof(struct pair_t) , 1024*1024, 0, 0, pair_hash, pair_cmp, free_item, NULL); debug(LLOG_INFO, "store.mem", "created new hashmap for mem store"); } return (struct hashmap *)store->userdata; @@ -92,34 +100,37 @@ int lq_mem_content_get(enum payload_e typ, LQStore *store, const char *key, size int lq_mem_content_put(enum payload_e typ, LQStore *store, const char *key, size_t *key_len, char *value, size_t value_len) { const char *r; struct hashmap *o; - struct pair_t v; + struct pair_t *v; char path[LQ_PATH_MAX]; o = lq_mem_init(store); + + v = lq_alloc(sizeof(struct pair_t)); path[0] = (char)typ; lq_cpy(path+1, key, *key_len); - v.key = path; - v.key_len = *key_len + 1; - v.val = value; - v.val_len = value_len; + v->key = path; + v->key_len = *key_len + 1; + v->val = value; + v->val_len = value_len; - debug_x(LLOG_DEBUG, "store.mem", "store put req", 2, MORGEL_TYP_BIN, v.key_len, "key", v.key, MORGEL_TYP_NUM, 0, "bytes", value_len); + debug_x(LLOG_DEBUG, "store.mem", "store put req", 2, MORGEL_TYP_BIN, v->key_len, "key", v->key, MORGEL_TYP_NUM, 0, "bytes", value_len); - r = hashmap_set(o, &v); + r = hashmap_set(o, v); if (r != NULL) { if (hashmap_oom(o)) { return ERR_WRITE; } } - debug_x(LLOG_DEBUG, "store.mem", "store put res", 2, MORGEL_TYP_BIN, v.key_len, "key", v.key, MORGEL_TYP_NUM, 0, "bytes", value_len); + debug_x(LLOG_DEBUG, "store.mem", "store put res", 2, MORGEL_TYP_BIN, v->key_len, "key", v->key, MORGEL_TYP_NUM, 0, "bytes", value_len); return ERR_OK; } void lq_mem_content_free(LQStore *store) { if (store->userdata != NULL) { + hashmap_clear((struct hashmap*)store->userdata, false); hashmap_free((struct hashmap*)store->userdata); store->userdata = NULL; } @@ -144,5 +155,4 @@ LQStore* lq_store_new(const char *spec) { } void lq_store_free(LQStore *store) { - lq_free(store); }