Skip to content

Commit

Permalink
Fix pointer arithmetics in dict_set
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Dec 14, 2017
1 parent abc1304 commit 82bb3de
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bool dict_set(dict *m, dicti k, dicti v, void *u) {
const int bucket = k % m->size;
dictkv *kv = m->table[bucket];
if (!kv) {
kv = calloc (sizeof(dictkv), 2);
kv = calloc (sizeof (dictkv), 2);
if (kv) {
m->table[bucket] = kv;
kv->k = MHTNO;
Expand All @@ -82,9 +82,8 @@ bool dict_set(dict *m, dicti k, dicti v, void *u) {
}
kv++;
}
int cursz = (kv - tmp);
int curln = cursz / sizeof(dictkv);
dictkv *newkv = realloc(tmp, (curln + 2) * sizeof(dictkv));
int curln = (kv - tmp);
dictkv *newkv = realloc (tmp, (curln + 2) * sizeof (dictkv));
if (newkv) {
kv = m->table[bucket] = newkv;
kv += curln;
Expand Down Expand Up @@ -115,12 +114,12 @@ dictkv *dict_getr(dict *m, dicti k) {
}

dicti dict_get(dict *m, dicti k) {
dictkv *kv = dict_getr(m, k);
dictkv *kv = dict_getr (m, k);
return kv? kv->v: MHTNO;
}

void *dict_getu(dict *m, dicti k) {
dictkv *kv = dict_getr(m, k);
dictkv *kv = dict_getr (m, k);
return kv? kv->u: NULL;
}

Expand Down Expand Up @@ -183,15 +182,20 @@ static char *dict_str(dict *m) {
}
return res;
}

int main() {
dict m;
dict_init(&m, 32, free);
dict_set (&m, 0x100, 1, NULL);
dict_set (&m, 0x200, 2, NULL);
printf ("%d %d\n", (int)dict_get(&m, 0x100), (int)dict_get(&m, 0x200));

#if 0
dict_set(&m, dict_hash("username"), 1024, NULL);
dict_set(&m, 32, 212, strdup("test"));
dict_del(&m, dict_hash("username"));
printf ("%d\n", (int)dict_get(&m, dict_hash("username")));
printf ("%s\n", dict_getu(&m, 32)); //dict_hash("username")));
#endif
dict_fini(&m);
return 0;
}
Expand Down

0 comments on commit 82bb3de

Please sign in to comment.