Commit d1c82a6e authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Realloc xroutes on table overflow.

This is safe, since no part of the code ever holds a pointer to an xroute.
parent eade5273
...@@ -23,7 +23,6 @@ THE SOFTWARE. ...@@ -23,7 +23,6 @@ THE SOFTWARE.
#define MAXROUTES 512 #define MAXROUTES 512
#define MAXSRCS 256 #define MAXSRCS 256
#define MAXNEIGHBOURS 64 #define MAXNEIGHBOURS 64
#define MAXXROUTES 64
#define INFINITY ((unsigned short)(~0)) #define INFINITY ((unsigned short)(~0))
......
...@@ -37,8 +37,9 @@ THE SOFTWARE. ...@@ -37,8 +37,9 @@ THE SOFTWARE.
#include "filter.h" #include "filter.h"
#include "network.h" #include "network.h"
struct xroute xroutes[MAXXROUTES]; struct xroute *xroutes;
int numxroutes = 0; int numxroutes = 0;
int maxxroutes = 0;
struct xroute * struct xroute *
find_xroute(const unsigned char *prefix, unsigned char plen) find_xroute(const unsigned char *prefix, unsigned char plen)
...@@ -85,8 +86,20 @@ add_xroute(int kind, unsigned char prefix[16], unsigned char plen, ...@@ -85,8 +86,20 @@ add_xroute(int kind, unsigned char prefix[16], unsigned char plen,
return 1; return 1;
} }
if(numxroutes >= MAXXROUTES) if(numxroutes >= maxxroutes) {
return -1; struct xroute *new_xroutes;
int n = maxxroutes < 1 ? 8 : 2 * maxxroutes;
new_xroutes = xroutes == NULL ?
malloc(n * sizeof(struct xroute)) :
realloc(xroutes, n * sizeof(struct xroute));
if(new_xroutes == NULL)
return -1;
maxxroutes = n;
xroutes = new_xroutes;
}
xroutes[numxroutes].kind = kind; xroutes[numxroutes].kind = kind;
memcpy(xroutes[numxroutes].prefix, prefix, 16); memcpy(xroutes[numxroutes].prefix, prefix, 16);
......
...@@ -34,7 +34,7 @@ struct xroute { ...@@ -34,7 +34,7 @@ struct xroute {
int proto; int proto;
}; };
extern struct xroute xroutes[MAXXROUTES]; extern struct xroute *xroutes;
extern int numxroutes; extern int numxroutes;
struct xroute *find_xroute(const unsigned char *prefix, unsigned char plen); struct xroute *find_xroute(const unsigned char *prefix, unsigned char plen);
......
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