nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

commit 912bffb4f5a0b8eeabd84ed41259ce470ddcb071
parent 0ff4d2624c145b5951bd0c02163adadb7f2b391d
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 27 Aug 2023 21:10:38 -0700

ndb: assert alignment

Diffstat:
Mnostrdb.c | 17+++++++++++++----
Mtest.c | 6++++--
2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -331,6 +331,7 @@ static void *ndb_lookup_tsid(struct ndb *ndb, enum ndb_dbs ind, } res = v.mv_data; + assert(((uint64_t)res % 4) == 0); if (len) *len = v.mv_size; cleanup: @@ -515,6 +516,7 @@ static int ndb_ingester_process_event(secp256k1_context *ctx, // we didn't find anything. let's send it // to the writer thread note = realloc(note, note_size); + assert(((uint64_t)note % 4) == 0); if (note->kind == 0) { struct ndb_profile_record_builder *b = @@ -583,8 +585,12 @@ static int ndb_write_profile(struct ndb_lmdb *lmdb, MDB_txn *txn, NdbProfileRecord_end_as_root(profile->record.builder); flatbuf = profile->record.flatbuf = - flatcc_builder_finalize_aligned_buffer(profile->record.builder, - &flatbuf_len); + flatcc_builder_finalize_buffer(profile->record.builder, &flatbuf_len); + + assert(((uint64_t)flatbuf % 8) == 0); + + // TODO: this may not be safe!? + flatbuf_len = (flatbuf_len + 3) & ~3; // get dbs profile_db = lmdb->dbs[NDB_DB_PROFILE]; @@ -596,8 +602,8 @@ static int ndb_write_profile(struct ndb_lmdb *lmdb, MDB_txn *txn, // write profile to profile store key.mv_data = &profile_key; key.mv_size = sizeof(profile_key); - val.mv_data = flatbuf + 4; - val.mv_size = flatbuf_len - 4; + val.mv_data = flatbuf; + val.mv_size = flatbuf_len; //ndb_debug("profile_len %ld\n", profile->profile_len); if ((rc = mdb_put(txn, profile_db, &key, &val, 0))) { @@ -1434,6 +1440,9 @@ int ndb_builder_finalize(struct ndb_builder *builder, struct ndb_note **note, return 0; } + // make sure we're aligned + total_size = (total_size + 3) & ~3; + assert((total_size % 4) == 0); return total_size; } diff --git a/test.c b/test.c @@ -277,10 +277,12 @@ static void test_fetch_last_noteid() 0xd1, 0x2c, 0x17, 0xbd, 0xe3, 0x09, 0x4a, 0xd3, 0x2f, 0x4a, 0xb8, 0x62, 0xa6, 0xcc, 0x6f, 0x5c, 0x28, 0x9c, 0xfe, 0x7d, 0x58, 0x02, 0x27, 0x0b, 0xdf, 0x34, 0x90, 0x4d, 0xf5, 0x85, 0xf3, 0x49 }; - void *profile_record = ndb_get_profile_by_pubkey(ndb, pk, &len); + void *root = ndb_get_profile_by_pubkey(ndb, pk, &len); - assert(profile_record); + assert(root); + assert((((uint64_t)root) % 4) == 0); + NdbProfileRecord_table_t profile_record = NdbProfileRecord_as_root(root); NdbProfile_table_t profile = NdbProfileRecord_profile_get(profile_record); const char *lnurl = NdbProfileRecord_lnurl_get(profile_record); const char *name = NdbProfile_name_get(profile);