Commit 4826428f authored by 's avatar
Browse files

Arguments checking in mapi_* functions. Bug 430 fixed.

git-svn-id: file:///home/svn/mapi/trunk@531 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent 52b50018
......@@ -305,6 +305,11 @@ int mapi_connect(int fd)
if (!minit) {
DEBUG_CMD(printf("Not initialized! [%s:%d]\n",__FILE__,__LINE__));
return -1;
}else if (fd<=0){
//could not use mapi_set_error since the flow is not connected and may
//cause seg fault
fprintf(stderr,"Invalid Flow descriptor [%s:%d]\n",__FILE__,__LINE__);
return -1;
}
#ifdef DIMAPI
......@@ -436,6 +441,10 @@ int mapi_create_flow(const char *dev)
struct mapiipcbuf qbuf;
flowdescr_t *flow, *tmpflow;
if(dev==NULL){
fprintf(stderr, "Error wrong device name given \n\n");
return -1;
}
#ifdef DIMAPI
remote_flowdescr_t *rflow;
char *hostname=NULL, *s=NULL, *k=NULL;
......@@ -645,7 +654,11 @@ int mapi_create_offline_flow(const char *dev, int format)
pthread_once(&mapi_is_initialized, (void*)mapi_init);
//Check to see if file can be opened
if ((file=open(dev,O_LARGEFILE))==-1) {
if (dev==NULL){
fprintf(stderr,"Error NULL device in mapi_create_offline_flow\n\n");
return -1;
}
else if ((file=open(dev,O_LARGEFILE))==-1) {
local_err=MAPI_ERROR_FILE;
return -1;
}
......@@ -730,7 +743,12 @@ char* mapi_create_offline_device(const char *path, int format)
pthread_once(&mapi_is_initialized, (void*)mapi_init);
//Check to see if file can be opened
if ((file=open(path,O_LARGEFILE))==-1) {
if (path==NULL){
fprintf(stderr,"Error NULL path in mapi_create_offline_device\n\n");
return NULL;
}
else if ((file=open(path,O_LARGEFILE))==-1) {
local_err=MAPI_ERROR_FILE;
return NULL;
}
......@@ -800,6 +818,10 @@ int mapi_start_offline_device(const char *dev)
pthread_once(&mapi_is_initialized, (void*)mapi_init);
if (dev==NULL){
fprintf(stderr,"Error NULL device in mapi_start_offline_device\n\n");
return -1;
}
pthread_spin_lock(&mapi_lock);
if ((get_numflows() == 0) && (get_totalflows() > 0) && minit) { // socket has been closed, re-create it
pthread_spin_lock(&mapi_lock);
......@@ -848,6 +870,10 @@ int mapi_delete_offline_device(char *dev)
printf("closing down...\n");
pthread_once(&mapi_is_initialized, (void*)mapi_init);
if (dev==NULL){
fprintf(stderr,"Error NULL device in mapi_delete_offline_device\n\n");
return -1;
}
/* socet is not closed
pthread_spin_lock(&mapi_lock);
if ((get_numflows() == 0) && (get_totalflows() > 0) && minit) { // socket has been closed, re-create it
......@@ -898,6 +924,10 @@ int mapi_close_flow(int fd)
functdescr_t *f=NULL;
flowdescr_t *flow=NULL;
if (fd<=0){
fprintf(stderr,"Error wrong fd in mapi_close_flow\n\n");
return -1;
}
#ifdef DIMAPI
remote_flowdescr_t *rflow=flist_get(remote_flowlist, fd);
host_flow* hflow;
......@@ -945,16 +975,19 @@ int mapi_close_flow(int fd)
return 0;
}
#endif
pthread_spin_lock(&mapi_lock);
flow = flist_remove(flowlist,fd,FLIST_LEAVE_DATA);
pthread_spin_unlock(&mapi_lock);
if (flow==NULL) {
if(flowlist && (flow = flist_get(flowlist, fd))==NULL){
DEBUG_CMD(printf("Invalid flow: %d [%s:%d]\n",fd,__FILE__,__LINE__));
return -1;
}
else
{
pthread_spin_lock(&mapi_lock);
//XXX MEMORY LEAK why leave data since the node is removed????
flow = flist_remove(flowlist,fd,FLIST_LEAVE_DATA);
pthread_spin_unlock(&mapi_lock);
}
//Delete functions applied first, before mapid is notified.
pthread_spin_lock(&mapi_lock);
while((f = flist_pop_first(flow->flist)) != NULL) {
......@@ -1072,12 +1105,21 @@ int mapi_apply_function(int fd, const char* funct, ...)
flist_node_t* fnode;
struct dmapi_results* dres;
#endif
if (!minit) {
DEBUG_CMD(printf("Not initialized! [%s:%d]\n",__FILE__,__LINE__));
return -1;
}
else if(fd<=0){
fprintf(stderr,"Error wrong fd in mapi_apply_function\n\n");
return -1;
}
if(funct==NULL){
fprintf(stderr,"Error NULL function in mapi_apply_function\n\n");
return -1;
}
#ifdef DIMAPI
if ((rflow=flist_get(remote_flowlist,fd))!=NULL) {
......@@ -1427,6 +1469,10 @@ void* mapi_read_results(int fd, int fid)
DEBUG_CMD(printf("Not initialized! [%s:%d]\n",__FILE__,__LINE__));
return NULL;
}
else if(fd<=0 || fid <=0 ){
fprintf(stderr,"Error wrong fd or fid in mapi_read_results\n\n");
return NULL;
}
#ifdef DIMAPI
if ((rflow=flist_get(remote_flowlist,fd))!=NULL) {
......@@ -1565,6 +1611,10 @@ mapi_get_next_pkt(int fd,int fid)
DEBUG_CMD(printf("Not initialized! [%s:%d]\n",__FILE__,__LINE__));
return NULL;
}
else if(fd<=0 || fid <=0 ){
fprintf(stderr,"Error wrong fd or fid in mapi_get_next_pkt\n\n");
return NULL;
}
#ifdef DIMAPI
if ((rflow=flist_get(remote_flowlist,fd))!=NULL) {
......@@ -1703,6 +1753,10 @@ int mapi_loop(int fd, int fid, int cnt, mapi_handler callback){
DEBUG_CMD(printf("Not initialized! [%s:%d]\n",__FILE__,__LINE__));
return -1;
}
else if(fd<=0 || fid <=0 ){
fprintf(stderr,"Error wrong fd or fid in mapi_loop\n\n");
return -1;
}
if ((flow=flist_get(flowlist,fd))==NULL) {
DEBUG_CMD(printf("Invalid flow: %d [%s:%d]\n",fd,__FILE__,__LINE__));
......@@ -1910,6 +1964,10 @@ int mapi_get_function_info(int fd,int fid, mapi_function_info_t *info)
DEBUG_CMD(printf("Not initialized! [%s:%d]\n",__FILE__,__LINE__));
return -1;
}
else if(fd<=0 || fid <=0 ){
fprintf(stderr,"Error wrong fd or fid in mapi_get_function_info\n\n");
return -1;
}
if ((flow=flist_get(flowlist,fd))==NULL) {
DEBUG_CMD(printf("Invalid flow: %d [%s:%d]\n",fd,__FILE__,__LINE__));
......@@ -1955,6 +2013,14 @@ int mapi_get_next_function_info(int fd,int fid, mapi_function_info_t *info)
qbuf.fd=fd;
qbuf.fid=fid;
if (!minit) {
DEBUG_CMD(printf("Not initialized! [%s:%d]\n",__FILE__,__LINE__));
return -1;
}
else if(fd<=0 || fid <=0 ){
fprintf(stderr,"Error wrong fd or fid in mapi_get_next_function_info\n\n");
return -1;
}
pthread_spin_lock(&mapi_lock);
if (mapiipc_write((struct mapiipcbuf*)&qbuf)<0) {
pthread_spin_unlock(&mapi_lock);
......@@ -1981,7 +2047,10 @@ int mapi_get_flow_info(int fd, mapi_flow_info_t *info)
DEBUG_CMD(printf("Not initialized! [%s:%d]\n",__FILE__,__LINE__));
return -1;
}
else if(fd<=0){
fprintf(stderr,"Error wrong fd in mapi_get_flow_info\n\n");
return -1;
}
qbuf.mtype=1;
qbuf.cmd=GET_FLOW_INFO;
qbuf.fd=fd;
......@@ -2010,6 +2079,10 @@ int mapi_get_next_flow_info(int fd, mapi_flow_info_t *info)
pthread_once(&mapi_is_initialized, (void*)mapi_init);
if(fd<=0){
fprintf(stderr,"Error wrong fd in mapi_get_next_flow_info\n\n");
return -1;
}
qbuf.mtype=1;
qbuf.cmd=GET_NEXT_FLOW_INFO;
qbuf.fd=fd;
......@@ -2232,6 +2305,10 @@ int mapi_set_authdata(int fd,const char *pub,const char *priv,const char *creds)
return -1;
}
if(fd<=0){
fprintf(stderr,"Error wrong fd in mapi_set_authdata\n\n");
return -1;
}
#ifdef DIMAPI
if ((rflow=flist_get(remote_flowlist,fd))!=NULL) {
is_remote=1;
......@@ -2422,6 +2499,10 @@ int mapi_authenticate(int fd, const char *username, const char *password, const
fprintf(stderr, "DIMAPI is not initialised. [%s:%d]\n",__FILE__,__LINE__);
return(-1);
}
if(fd<=0){
fprintf(stderr,"Error wrong fd in mapi_authenticate\n\n");
return -1;
}
/*
* Before we start, some trivial checks.
*/
......
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