commit e6662014ba1f32e1f20fb5ea564e1ebad879b772
parent a5c7d68df3c6aecf0c3ba18a37ee5c0d51a52d12
Author: William Casarin <jb55@jb55.com>
Date: Thu, 20 Jul 2023 16:30:45 -0700
fix json token memory calc
apparently this behavior changes between compilers. great...
Diffstat:
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/nostrdb.c b/nostrdb.c
@@ -59,8 +59,11 @@ static inline int
ndb_json_parser_init(struct ndb_json_parser *p, const char *json, int json_len, unsigned char *buf, int bufsize) {
int half = bufsize / 2;
- p->toks = (jsmntok_t*)buf + half;
- p->toks_end = (jsmntok_t*)buf + bufsize;
+ unsigned char *tok_start = buf + half;
+ unsigned char *tok_end = buf + bufsize;
+
+ p->toks = (jsmntok_t*)tok_start;
+ p->toks_end = (jsmntok_t*)tok_end;
p->num_tokens = 0;
p->json = json;
p->json_len = json_len;
@@ -83,7 +86,7 @@ ndb_json_parser_init(struct ndb_json_parser *p, const char *json, int json_len,
static inline int
ndb_json_parser_parse(struct ndb_json_parser *p) {
- int cap = (p->toks_end - p->toks)/sizeof(*p->toks);
+ int cap = ((unsigned char *)p->toks_end - (unsigned char*)p->toks)/sizeof(*p->toks);
p->num_tokens =
jsmn_parse(&p->json_parser, p->json, p->json_len, p->toks, cap);
diff --git a/test.c b/test.c
@@ -105,7 +105,8 @@ static void test_parse_json() {
"{\"id\": \"" HEX_ID "\",\"pubkey\": \"" HEX_PK "\",\"created_at\": 1689836342,\"kind\": 1,\"tags\": [[\"p\",\"" HEX_ID "\"], [\"word\", \"words\", \"w\"]],\"content\": \"共通語\",\"sig\": \"e4d528651311d567f461d7be916c37cbf2b4d530e672f29f15f353291ed6df60c665928e67d2f18861c5ca88\"}";
int ok;
- ndb_note_from_json(json, strlen(json), ¬e, buffer, sizeof(buffer));
+ ok = ndb_note_from_json(json, strlen(json), ¬e, buffer, sizeof(buffer));
+ assert(ok);
const char *content = ndb_note_content(note);
unsigned char *id = ndb_note_id(note);