Skip to content
This repository has been archived by the owner on Apr 13, 2019. It is now read-only.

Commit

Permalink
Add some sanity checks to skiplist functions
Browse files Browse the repository at this point in the history
  • Loading branch information
erayd committed May 21, 2015
1 parent 495f840 commit 5942ad1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/libec/include/ec.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ char *ec_errstr(ec_err_t error);
#define EC_ENOCTX 26 /*no context*/
#define EC_ENOVALIDATOR 27 /*no validator*/
#define EC_EREQUIRED 28 /*required record does not validate*/
#define EC_EUNDEFINED 29 /*undefined data*/

//flags
#define EC_CERT_TRUSTED (1 << 0) /*cert is a trust anchor*/
Expand Down
10 changes: 9 additions & 1 deletion src/libec/misc/skiplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ ec_sl_cursor_t *ec_sl_cursor(ec_sl_t *l, ec_sl_cursor_t *c, void *key) {
* Sets & returns errno on failure, otherwise returns zero
*/
int ec_sl_insert(ec_sl_t *l, ec_sl_cursor_t *c, void *data) {
if(!data)
return EC_EUNDEFINED;
//set element level
int level = 1;
while(level < EC_SL_MAXLEVEL && rand() % 2)
Expand All @@ -94,12 +96,14 @@ int ec_sl_insert(ec_sl_t *l, ec_sl_cursor_t *c, void *data) {
* Get the element data for a given key. Returns NULL if not found.
*/
void *ec_sl_get(ec_sl_t *l, void *key) {
if(!key)
return NULL;
ec_sl_node_t *n = l->start;
for(int level = l->level; level; level--) {
while(n[level].next && (!key || l->compfn(key, n[level].next->data) >= 0))
n = n[level].next;
}
return (n->data && !l->compfn(key, n->data)) ?n->data : NULL;
return (n->data && !l->compfn(key, n->data)) ? n->data : NULL;
}

/**
Expand All @@ -108,6 +112,8 @@ void *ec_sl_get(ec_sl_t *l, void *key) {
* Sets & returns errno on failure, otherwise returns zero.
*/
int ec_sl_set(ec_sl_t *l, void *key, void *data, ec_sl_freefn_t freefn) {
if(!key || !data)
return EC_EUNDEFINED;
ec_sl_cursor_t c;
ec_sl_cursor(l, &c, key);
if(c[1]->next && !l->compfn(key, c[1]->next->data)) {
Expand All @@ -124,6 +130,8 @@ int ec_sl_set(ec_sl_t *l, void *key, void *data, ec_sl_freefn_t freefn) {
* Remove the element for a given key, if it exists
*/
void ec_sl_remove(ec_sl_t *l, void *key, ec_sl_freefn_t freefn) {
if(!key)
return NULL;
ec_sl_cursor_t c;
ec_sl_cursor(l, &c, key);
ec_sl_node_t *n = c[1]->next;
Expand Down

0 comments on commit 5942ad1

Please sign in to comment.