]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-match.c
Some potentially unsafe fixes.
[irc/quakenet/newserv.git] / newsearch / ns-match.c
1 /*
2 * MATCH functionality
3 */
4
5 #include "newsearch.h"
6 #include "../lib/irc_string.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 struct match_localdata {
12 struct searchNode *targnode;
13 struct searchNode *patnode;
14 };
15
16 void *match_exe(struct searchNode *thenode, void *theinput);
17 void match_free(struct searchNode *thenode);
18
19 struct searchNode *match_parse(int type, int argc, char **argv) {
20 struct match_localdata *localdata;
21 struct searchNode *thenode;
22 struct searchNode *targnode, *patnode;
23
24 if (argc<2) {
25 parseError="match: usage: match source pattern";
26 return NULL;
27 }
28
29 targnode = search_parse(type, argv[0]);
30 if (!(targnode = coerceNode(targnode, RETURNTYPE_STRING)))
31 return NULL;
32
33 patnode = search_parse(type, argv[1]);
34 if (!(patnode = coerceNode(patnode, RETURNTYPE_STRING))) {
35 (targnode->free)(targnode);
36 return NULL;
37 }
38
39 if (!(localdata=(struct match_localdata *)malloc(sizeof (struct match_localdata)))) {
40 parseError = "malloc: could not allocate memory for this search.";
41 return NULL;
42 }
43
44 localdata->targnode=targnode;
45 localdata->patnode=patnode;
46
47 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
48 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
49 parseError = "malloc: could not allocate memory for this search.";
50 free(localdata);
51 return NULL;
52 }
53
54 thenode->returntype = RETURNTYPE_BOOL;
55 thenode->localdata = localdata;
56 thenode->exe = match_exe;
57 thenode->free = match_free;
58
59 return thenode;
60 }
61
62 void *match_exe(struct searchNode *thenode, void *theinput) {
63 struct match_localdata *localdata;
64 char *pattern, *target;
65
66 localdata = thenode->localdata;
67
68 pattern = (char *)(localdata->patnode->exe) (localdata->patnode, theinput);
69 target = (char *)(localdata->targnode->exe)(localdata->targnode,theinput);
70
71 return (void *)(long)match2strings(pattern, target);
72 }
73
74 void match_free(struct searchNode *thenode) {
75 struct match_localdata *localdata;
76
77 localdata=thenode->localdata;
78
79 (localdata->patnode->free)(localdata->patnode);
80 (localdata->targnode->free)(localdata->targnode);
81 free(localdata);
82 free(thenode);
83 }
84