diff --git a/src/vde_switch/port.c b/src/vde_switch/port.c index 016fbf1..8c7ed75 100644 --- a/src/vde_switch/port.c +++ b/src/vde_switch/port.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #include @@ -41,6 +43,11 @@ static int stdqlen=128; static struct port **portv; +static int external_numports(void) +{ + return (numports > 0) ? numports - 1 : 0; +} + #ifdef DEBUGOPT #define DBGPORTNEW (dl) #define DBGPORTDEL (dl+1) @@ -730,7 +737,7 @@ void handle_in_packet(struct endpoint *ep, struct packet *packet, int len) static int showinfo(FILE *fd) { - printoutc(fd,"Numports=%d",numports); + printoutc(fd,"Numports=%d",external_numports()); printoutc(fd,"HUB=%s",(pflag & HUB_TAG)?"true":"false"); #ifdef PORTCOUNTERS printoutc(fd,"counters=true"); @@ -745,53 +752,60 @@ static int showinfo(FILE *fd) static int portsetnumports(int val) { - if(val > 0) { + if (val > 0 && val <= INT_MAX - 1) { /*resize structs*/ int i; - for(i=val;i SIZE_MAX / sizeof(struct port *)) { + printlog(LOG_ERR,"Numport resize overflow"); + exit(1); + } + for(i=internal_numports;i #include #include +#include #include #include #include @@ -406,7 +407,11 @@ static int parse_globopt(int c, char *optarg) } break; case 'n': - sscanf(optarg,"%i",&numports); + if (sscanf(optarg,"%i",&numports) != 1 || + numports < 1 || numports > INT_MAX - 1) { + printlog(LOG_ERR,"Invalid number of ports %s",optarg); + Usage(); + } break; case 'v': version(); @@ -601,9 +606,9 @@ int main(int argc, char **argv) atexit(cleanup); hash_init(hash_size); #ifdef FSTP - fst_init(numports); + fst_init(numports + 1); #endif - port_init(numports); + port_init(numports + 1); init_mods(); loadrcfile(); qtimer_init();