X-Git-Url: https://jfr.im/git/irc/quakenet/newserv.git/blobdiff_plain/c86edd1d9e5994aea33cfad3164e4827e591e7e6..8855bb48b449ed06cfd3ce528b3c0a77c37cb24b:/newsearch/ns-or.c diff --git a/newsearch/ns-or.c b/newsearch/ns-or.c index 68d18a9b..e2808b31 100644 --- a/newsearch/ns-or.c +++ b/newsearch/ns-or.c @@ -7,26 +7,40 @@ #include #include -void or_free(struct searchNode *thenode); -void *or_exe(struct searchNode *thenode, int type, void *theinput); +void or_free(searchCtx *ctx, struct searchNode *thenode); +void *or_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); struct or_localdata { int count; searchNode **nodes; }; -struct searchNode *or_parse(int type, int argc, char **argv) { +struct searchNode *or_parse(searchCtx *ctx, int argc, char **argv) { searchNode *thenode, *subnode; struct or_localdata *localdata; int i; /* Set up our local data - a list of nodes to OR together */ - localdata=(struct or_localdata *)malloc(sizeof(struct or_localdata)); - localdata->nodes=(searchNode **)malloc(argc * sizeof(searchNode *)); + if (!(localdata=(struct or_localdata *)malloc(sizeof(struct or_localdata)))) { + parseError = "malloc: could not allocate memory for this search."; + return NULL; + } + if (!(localdata->nodes=(searchNode **)malloc(argc * sizeof(searchNode *)))) { + /* couldn't malloc() memory for localdata->nodes, so free localdata to avoid leakage */ + parseError = "malloc: could not allocate memory for this search."; + free(localdata); + return NULL; + } localdata->count=0; /* Allocate our actual node */ - thenode=(searchNode *)malloc(sizeof(searchNode)); + if (!(thenode=(searchNode *)malloc(sizeof(searchNode)))) { + /* couldn't malloc() memory for thenode, so free localdata->nodes and localdata to avoid leakage */ + parseError = "malloc: could not allocate memory for this search."; + free(localdata->nodes); + free(localdata); + return NULL; + } thenode->returntype = RETURNTYPE_BOOL; thenode->localdata = localdata; @@ -34,11 +48,12 @@ struct searchNode *or_parse(int type, int argc, char **argv) { thenode->free = or_free; for (i=0;iparser(ctx, argv[i]); /* Propogate the search type */ + subnode=coerceNode(ctx, subnode, RETURNTYPE_BOOL); /* BOOL please */ if (subnode) { localdata->nodes[localdata->count++] = subnode; } else { - or_free(thenode); + or_free(ctx, thenode); return NULL; } } @@ -46,13 +61,13 @@ struct searchNode *or_parse(int type, int argc, char **argv) { return thenode; } -void or_free(struct searchNode *thenode) { +void or_free(searchCtx *ctx, struct searchNode *thenode) { struct or_localdata *localdata; int i; localdata=thenode->localdata; for (i=0;icount;i++) { - (localdata->nodes[i]->free)(localdata->nodes[i]); + (localdata->nodes[i]->free)(ctx, localdata->nodes[i]); } free(localdata->nodes); @@ -60,35 +75,16 @@ void or_free(struct searchNode *thenode) { free(thenode); } -void *or_exe(struct searchNode *thenode, int type, void *theinput) { +void *or_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { int i; - void *ret; struct or_localdata *localdata; localdata=thenode->localdata; for (i=0;icount;i++) { - ret = (localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_BOOL, theinput); - if (ret) { - switch (type) { - case RETURNTYPE_STRING: - return "1"; - - case RETURNTYPE_INT: - case RETURNTYPE_BOOL: - default: - return (void *)1; - } - } + if ((localdata->nodes[i]->exe)(ctx, localdata->nodes[i], theinput)) + return (void *)1; } - switch(type) { - case RETURNTYPE_STRING: - return ""; - - case RETURNTYPE_INT: - case RETURNTYPE_BOOL: - default: - return NULL; - } + return NULL; }