Commit bd977fc0 authored by 's avatar
Browse files

Eliminate memory leaks from function regexp and update testing file of this function

git-svn-id: file:///home/svn/mapi/trunk@923 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent 69d7d09e
......@@ -60,12 +60,18 @@ static int regexp_instance(mapidflib_function_instance_t *instance,
if(!pattern)
return(MFUNCT_INVALID_ARGUMENT);
if((compiled_pattern = pcre_compile(pattern, 0, (const char **) &error, &erroffset, 0)) == NULL)
{
if(pattern == NULL)
return(MFUNCT_INVALID_ARGUMENT);
if((compiled_pattern = pcre_compile(pattern, 0, (const char **) &error, &erroffset, 0)) == NULL){
fprintf(stderr, "pcre_compile failed (offset: %d), %s\n", erroffset, error);
return(MFUNCT_INVALID_ARGUMENT);
}
pcre_free(compiled_pattern); // free pcre struct point
return(0);
}
......@@ -87,13 +93,26 @@ static int regexp_init(mapidflib_function_instance_t *instance, MAPI_UNUSED int
data->compiled_pattern= pcre_compile (data->pattern,0, (const char **) &error, &erroffset,0);
if (!(data->compiled_pattern)) {
printf("pcre_compile failed (offset: %d), %s\n", erroffset, error);
return MFUNCT_INVALID_ARGUMENT_1;
printf("pcre_compile failed (offset: %d), %s\n", erroffset, error);
return MFUNCT_INVALID_ARGUMENT_1;
}
return 0;
}
static int regexp_cleanup(mapidflib_function_instance_t *instance){
struct mapid_regexp *data;
data = (struct mapid_regexp *)(instance->internal_data);
pcre_free(data->compiled_pattern); // free pcre struct point
free(data->pattern);
free(data);
return 0;
}
static mapidflib_function_def_t regexp_finfo={
"", //libname
"REGEXP", //name
......@@ -110,7 +129,7 @@ static mapidflib_function_def_t regexp_finfo={
regexp_process,
NULL, //get_result,
NULL,
NULL, //cleanup
regexp_cleanup, //cleanup
NULL, //client_init
NULL, //client_read_result
NULL //client_cleanup
......@@ -121,6 +140,3 @@ mapidflib_function_def_t* regexp_get_funct_info();
mapidflib_function_def_t* regexp_get_funct_info() {
return &regexp_finfo;
};
/*******************************************
/****************************************************
*
* test REG_EXP
* Testing REGEXP - Extra MAPI Function Library
*
******************************************/
***************************************************/
#include <stdio.h>
#include <unistd.h>
......@@ -10,213 +10,226 @@
#include <mapi.h>
#include "../test.h"
int main(int argc, char *argv[])
{
int main(int argc, char *argv[]){
int fd;
int fid;
int fidc, fids;
int fid, fidc, fids;
mapi_results_t *cntc, *cnts;
int err_no =0 , flag=0;
int err_no =0, flag=0;
char error[512];
mapi_flow_info_t info;
if(argc!=2){
printf("\nWrong arguments\n");
mapi_flow_info_t info;
if(argc != 2){
printf("Usage: %s <interface> \n", argv[0]);
return -1;
}
if ((fd = mapi_create_flow(argv[1]))<0){
if( (fd = mapi_create_flow(argv[1])) < 0){
fprintf(stderr, "Could not create flow using '%s'\n", argv[1]);
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if((fidc=mapi_apply_function(fd, "PKT_COUNTER"))<0){
if( (fidc = mapi_apply_function(fd, "PKT_COUNTER")) < 0){
fprintf(stderr, "Could not apply PKT_COUNTER to flow %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if((fid=mapi_apply_function(fd, "REGEXP" , "HTTP.*OK"))<0){
if( (fid = mapi_apply_function(fd, "REGEXP", "HTTP.*OK")) < 0){
fprintf(stderr, "Could not apply REGEXP to flow %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if((fids=mapi_apply_function(fd, "PKT_COUNTER"))<0){
if( (fids = mapi_apply_function(fd, "PKT_COUNTER")) < 0){
fprintf(stderr, "Could not apply PKT_COUNTER to flow %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if(mapi_connect(fd)<0){
if(mapi_connect(fd) < 0){
fprintf(stderr, "Connecting to flow failed %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
/*
* Sanity checks
*/
/*
* Sanity checks
*/
sleep(5);
cntc = mapi_read_results(fd,fidc);
cntc = mapi_read_results(fd, fidc);
DOT;
cnts = mapi_read_results(fd,fids);
cnts = mapi_read_results(fd, fids);
DOT;
printf("\nPackets read: %d\t Packets matched : %d", *((int*)cntc->res), *((int*)cnts->res));
if (*((int*)cntc->res) <= *((int*)cnts->res)){
fprintf(stderr,"\nWARNING: REGXEP failed\n");
}
printf("\n\nPackets read: %d\t Packets matched : %d", *((int*)cntc->res), *((int*)cnts->res));
if (*((int*)cntc->res) <= *((int*)cnts->res))
fprintf(stderr,"\nWARNING: REGXEP failed\n");
else
printf("\nREGEXP OK\n");
printf("\nREGEXP OK\n\n");
if(mapi_close_flow(fd)<0){
fprintf(stderr,"Close flow failed\n");
if(mapi_close_flow(fd) < 0){
fprintf(stderr, "Close flow failed\n");
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
/*
*Error Checking
*/
if ((fd = mapi_create_flow(argv[1]))<0){
/*
*Error Checking
*/
if ( (fd = mapi_create_flow(argv[1])) < 0){
fprintf(stderr, "Could not create flow using '%s'\n", argv[1]);
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if( (fid = mapi_apply_function(fd , "REGEXP" , "!@#@!##############@$#@@@@@@@@@@@@@@@@^^^^^^^^^^^^^^^^^^^#$$^^^^^^^^^^^^$#!!!!!!!!!!!!!!!!!!!!!!#$^^^^^^^^^^^^^^%%%%%%%%&&&&&&&&&&&&&&%%%%%%%%%%%%%%*&&&&&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@%%%%%%%%%%%%%%%%^^^^^^^^^^^^^^^@@@@@@@@@@@@@"))<0 ){
if( (fid = mapi_apply_function(fd, "REGEXP", "*)!@#@!##############@$#@@@@@@@@@@@@@@@@^^^^^^^^^^^^^^^^^^^#$$^^^^^^^^^^^^ \
$#!!!!!!!!!!!!!!!!!!!!!!#$^^^^^^^^^^^^^^%%%%%%%%&&&&&&&&&&&&&&%%%%%%%%%%%%%% \
*&&&&&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@%%%%%%%%%%%%%%%%^^^^^^^^^^^^^^^ \
@@@@@@@@@@@@@")) < 0){ /* invalid pattern - nothing to repeat */
mapi_read_error(&err_no, error);
printf("Testing error case1: Erorcode :%d description: %s \n" ,err_no, error);
printf("Testing error case1: Errorcode :%d description: %s \n", err_no, error);
if(err_no != 7000){
printf(" Wrong ERRORCODE returned\n");
printf("\t\tWrong ERRORCODE returned\n");
flag = 1;
}
}
}
DOT;
if( (fid = mapi_apply_function(fd, "REGEXP", ")(")) < 0){ /* invalid pattern - unmatched parentheses */
mapi_read_error(&err_no, error);
printf("Testing error case2: Errorcode :%d description: %s \n", err_no, error);
if(err_no != 7000){
printf("\t\tWrong ERRORCODE returned\n");
flag = 1;
}
}
DOT;
if(mapi_close_flow(fd)<0){
fprintf(stderr,"Close flow failed\n");
if(mapi_close_flow(fd) < 0){
fprintf(stderr, "Close flow failed\n");
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
/*
* Offline checking
*/
if ((fd = mapi_create_offline_flow("./tracefile" , MFF_PCAP))<0){
fprintf(stderr, "Could not create offline flow using './tracefile'\n");
/*
* Offline checking
*/
if ( (fd = mapi_create_offline_flow("../tracefile", MFF_PCAP)) < 0){
fprintf(stderr, "Could not create offline flow using tracefile\n");
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if((fidc=mapi_apply_function(fd, "PKT_COUNTER"))<0){
if( (fidc = mapi_apply_function(fd, "PKT_COUNTER")) < 0){
fprintf(stderr, "Could not apply PKT_COUNTER to flow %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if((fid=mapi_apply_function(fd, "REGEXP" , "HTTP.*OK"))<0){
if( (fid = mapi_apply_function(fd,"REGEXP", "HTTP.*OK")) < 0){
fprintf(stderr, "Could not apply REGEXP to flow %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if((fids=mapi_apply_function(fd, "PKT_COUNTER"))<0){
if( (fids = mapi_apply_function(fd, "PKT_COUNTER"))<0){
fprintf(stderr, "Could not apply PKT_COUNTER to flow %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if(mapi_connect(fd)<0){
if(mapi_connect(fd) < 0){
fprintf(stderr, "Connecting to flow failed %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
/*
* Sanity checks
*/
do
{
sleep(1);
mapi_get_flow_info(fd,&info);
} while(info.status!=FLOW_FINISHED);
cntc = mapi_read_results(fd,fidc);
/*
* Sanity checks
*/
do{
sleep(1);
mapi_get_flow_info(fd, &info);
} while(info.status != FLOW_FINISHED);
cntc = mapi_read_results(fd, fidc);
DOT;
cnts = mapi_read_results(fd,fids);
cnts = mapi_read_results(fd, fids);
DOT;
printf("\nPackets read: %d\t Packets matched : %d", *((int*)cntc->res), *((int*)cnts->res));
if (*((int*)cntc->res)!=893 && *((int*)cnts->res)!=4){
fprintf(stderr,"\nWARNING: REGXEP failed\n");
}
printf("\n\nPackets read: %d\t Packets matched : %d", *((int*)cntc->res), *((int*)cnts->res));
if (*((int*)cntc->res) != 893 && *((int*)cnts->res) != 4)
fprintf(stderr,"\nWARNING: REGXEP failed\n");
else
printf("\nREGEXP OK\n");
printf("\nOffline REGEXP OK\n\n");
if(mapi_close_flow(fd)<0){
fprintf(stderr,"Close flow failed\n");
if(mapi_close_flow(fd) < 0){
fprintf(stderr, "Close flow failed\n");
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
/*
*Error Checking
*/
if ((fd = mapi_create_offline_flow("./tracefile" , MFF_PCAP))<0){
fprintf(stderr, "Could not create offline flow using './tracefile'\n");
/*
*Error Checking
*/
if ( (fd = mapi_create_offline_flow("../tracefile", MFF_PCAP))<0){
fprintf(stderr, "Could not create offline flow using tracefile\n");
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if( (fid = mapi_apply_function(fd , "REGEXP" , "!@#@!##############@$#@@@@@@@@@@@@@@@@^^^^^^^^^^^^^^^^^^^#$$^^^^^^^^^^^^$#!!!!!!!!!!!!!!!!!!!!!!#$^^^^^^^^^^^^^^%%%%%%%%&&&&&&&&&&&&&&%%%%%%%%%%%%%%*&&&&&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@%%%%%%%%%%%%%%%%^^^^^^^^^^^^^^^@@@@@@@@@@@@@"))<0 ){
if( (fid = mapi_apply_function(fd, "REGEXP", "*)!@#@!##############@$#@@@@@@@@@@@@@@@@^^^^^^^^^^^^^^^^^^^#$$^^^^^^^^^^^^ \
$#!!!!!!!!!!!!!!!!!!!!!!#$^^^^^^^^^^^^^^%%%%%%%%&&&&&&&&&&&&&&%%%%%%%%%%%%%% \
*&&&&&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@%%%%%%%%%%%%%%%%^^^^^^^^^^^^^^^ \
@@@@@@@@@@@@@")) < 0){ /* invalid pattern - nothing to repeat */
mapi_read_error(&err_no, error);
printf("Testing error case1: Erorcode :%d description: %s \n" ,err_no, error);
printf("Testing error case1: Errorcode :%d description: %s \n", err_no, error);
if(err_no != 7000){
printf(" Wrong ERRORCODE returned\n");
printf("\t\tWrong ERRORCODE returned\n");
flag = 1;
}
}
DOT;
if(mapi_close_flow(fd)<0){
fprintf(stderr,"Close flow failed\n");
DOT;
if( (fid = mapi_apply_function(fd, "REGEXP", ")(")) < 0){ /* invalid pattern - unmatched parentheses */
mapi_read_error(&err_no, error);
fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
printf("Testing error case2: Errorcode :%d description: %s \n", err_no, error);
if(err_no != 7000){
printf("\t\tWrong ERRORCODE returned\n");
flag = 1;
}
}
DOT;
if(mapi_close_flow(fd) < 0){
fprintf(stderr, "Close flow failed\n");
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
if(!flag)
printf("\nRegexp Error Checking OK\n");
printf("\nRegexp Error Checking OK\n\n");
else
printf("\nRegexp Flow Error Checking :FAILED:\n");
printf("\nRegexp Flow Error Checking :FAILED:\n\n");
return 0;
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment