there's a buffer size problem that is very rarely hit by any normal programs but appears when compiling some of the nuttier lunix code. solution: if a line of code is longer than the default buffer size (30k) then we realloc the buffer. this isn't currently triggered by any plan9 code and will hopefully never be. for an example of what could happen in loonix-land see this gcc-parsed code: http://pages.cpsc.ucalgary.ca/~mirtchov/screenshots/ffmpeg.gif andrey Reference: /n/sources/patch/applied/cpp-buffer Date: Sat Oct 16 06:20:32 CES 2004 --- /sys/src/cmd/cpp/cpp.c Sat Oct 16 06:20:31 2004 +++ /sys/src/cmd/cpp/cpp.c Sat Oct 16 06:20:31 2004 @@ -251,6 +251,16 @@ } void * +dorealloc(void *ptr, int size) +{ + void *p = realloc(ptr, size); + + if (p==NULL) + error(FATAL, "Out of memory from realloc"); + return p; +} + +void * domalloc(int size) { void *p = malloc(size); --- /sys/src/cmd/cpp/cpp.h Sat Oct 16 06:20:31 2004 +++ /sys/src/cmd/cpp/cpp.h Sat Oct 16 06:20:31 2004 @@ -55,6 +55,7 @@ uchar *inb; /* input buffer */ uchar *inp; /* input pointer */ uchar *inl; /* end of input */ + int ins; /* input buffer size */ int fd; /* input source */ int ifdepth; /* conditional nesting in include */ struct source *next; /* stack for #include */ @@ -93,6 +94,7 @@ void unsetsource(void); void puttokens(Tokenrow *); void process(Tokenrow *); +void *dorealloc(void *, int); void *domalloc(int); void dofree(void *); void error(enum errtype, char *, ...); --- /sys/src/cmd/cpp/lex.c Sat Oct 16 06:20:32 2004 +++ /sys/src/cmd/cpp/lex.c Sat Oct 16 06:20:31 2004 @@ -319,7 +319,7 @@ s->inl = s->inb; fillbuf(s); ip = s->inp = s->inb; - } else if (ip >= s->inb+(3*INS/4)) { + } else if (ip >= s->inb+(3*s->ins/4)) { memmove(s->inb, ip, 4+s->inl-ip); s->inl = s->inb+(s->inl-ip); ip = s->inp = s->inb; @@ -445,7 +445,7 @@ state = COM2; ip += runelen; runelen = 1; - if (ip >= s->inb+(7*INS/8)) { /* very long comment */ + if (ip >= s->inb+(7*s->ins/8)) { /* very long comment */ memmove(tp->t, ip, 4+s->inl-ip); s->inl -= ip-tp->t; ip = tp->t+1; @@ -537,9 +537,20 @@ { int n; - if ((char *)s->inl+INS/8 > (char *)s->inb+INS) - error(FATAL, "Input buffer overflow"); - if (s->fd<0 || (n=read(s->fd, (char *)s->inl, INS/8)) <= 0) + while((char *)s->inl+s->ins/8 > (char *)s->inb+s->ins) { + uint l = s->inl - s->inb; + uint p = s->inp - s->inb; + if(l < 0) + error(FATAL, "negative end of input!?"); + if(p < 0) + error(FATAL, "negative input pointer!?"); + /* double the buffer size and try again */ + s->ins *= 2; + s->inb = dorealloc(s->inb, s->ins); + s->inl = s->inb + l; + s->inp = s->inb + p; + } + if (s->fd<0 || (n=read(s->fd, (char *)s->inl, s->ins/8)) <= 0) n = 0; if ((*s->inp&0xff) == EOB) /* sentinel character appears in input */ *s->inp = EOFC; @@ -592,6 +603,8 @@ s->inp = s->inb; len = 0; } + + s->ins = INS; s->inl = s->inp+len; s->inl[0] = s->inl[1] = EOB; return s;