Skip to content

Commit

Permalink
Fix a 35-year-old bug where the pushback buffer for the C compiler wa…
Browse files Browse the repository at this point in the history
…s being

defined as the wrong size.
davidgiven committed Oct 17, 2024

Verified

This commit was signed with the committer’s verified signature.
agostbiro Agost Biro
1 parent 54fef7e commit 5c28a90
Showing 5 changed files with 25 additions and 11 deletions.
3 changes: 1 addition & 2 deletions lang/cem/cpp.ansi/input.c
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
#include "replace.h"
#include "domacro.h"

#define INP_PUSHBACK 3
#define INP_NPUSHBACK 3
#define INP_TYPE struct file_info
#define INP_VAR finfo
struct file_info finfo;
@@ -23,7 +23,6 @@ struct file_info finfo;
char* getwdir(register char* fn)
{
register char* p;
char* strrchr();

p = strrchr(fn, '/');
while (p && *(p + 1) == '\0')
1 change: 1 addition & 0 deletions lang/m2/comp/input.c
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
#include "f_info.h"
struct f_info file_info;
#include "input.h"
#include "error.h"
#include <inp_pkg.body>


1 change: 1 addition & 0 deletions lang/pc/comp/input.c
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ struct f_info file_info;
#include "input.h"
#include <em_arith.h>
#include "idf.h"
#include "error.h"
#include <inp_pkg.body>


24 changes: 17 additions & 7 deletions modules/src/input/inp_pkg.body
Original file line number Diff line number Diff line change
@@ -89,6 +89,10 @@ struct INP_i_buf {

#endif /* not INP_READ_IN_ONE */

/* Used for the pushback buffer when between input files, where INP_head isn't
* valid. */
static char INP_pushbackbuf[INP_NPUSHBACK + 1];

char *_ipp;
INP_PRIVATE struct INP_buffer_header *INP_head, *INP_free;

@@ -331,6 +335,13 @@ int InsertText(char *text, int length)
return 1;
}

void INP_pushback(void)
{
if ((_ipp == INP_head->bh_text) || (_ipp == &INP_pushbackbuf[0]))
fatal("inp underflow\n");
_ipp--;
}

#define RAWLOAD(dest) \
((void)((dest = *_ipp++) || (dest = loadbuf())))

@@ -361,7 +372,6 @@ int loadchar(void)
int loadbuf(void)
{
register struct INP_buffer_header *bh = INP_head;
static char buf[INP_NPUSHBACK + 1];
int FromFile;

if (!bh) { /* stack exhausted, EOF on sourcefile */
@@ -404,7 +414,7 @@ int loadbuf(void)
#if INP_NPUSHBACK > 1
{
register char *so = &(bh->bh_text[bh->bh_size]);
register char *de = &buf[INP_NPUSHBACK - 1];
register char *de = &INP_pushbackbuf[INP_NPUSHBACK - 1];
register int i = INP_NPUSHBACK - 1;

if (i >= bh->bh_size) i = bh->bh_size - 1;
@@ -414,8 +424,8 @@ int loadbuf(void)
}
}
#endif
buf[INP_NPUSHBACK-1] = 0; /* make PushBack work on EOI */
_ipp = &buf[INP_NPUSHBACK];
INP_pushbackbuf[INP_NPUSHBACK-1] = 0; /* make PushBack work on EOI */
_ipp = &INP_pushbackbuf[INP_NPUSHBACK];

if (bh->bh_fd) { /* unstack a file */
#ifndef INP_READ_IN_ONE
@@ -428,7 +438,7 @@ int loadbuf(void)
free(bh->bh_text);
#endif /* INP_READ_IN_ONE */
}
bh->bh_text = buf;
bh->bh_text = INP_pushbackbuf;
bh->bh_size = INP_NPUSHBACK - 1;
if (FromFile) {
if (AtEoIF()) {
@@ -444,7 +454,7 @@ int loadbuf(void)
}
}

if (bh->bh_eofreturned && _ipp == &buf[INP_NPUSHBACK]) {
if (bh->bh_eofreturned && _ipp == &INP_pushbackbuf[INP_NPUSHBACK]) {
return EOI;
}

@@ -453,6 +463,6 @@ int loadbuf(void)
LoadChar(c);
return c;
}
_ipp = &buf[INP_NPUSHBACK];
_ipp = &INP_pushbackbuf[INP_NPUSHBACK];
return EOI;
}
7 changes: 5 additions & 2 deletions modules/src/input/inp_pkg.spec
Original file line number Diff line number Diff line change
@@ -21,8 +21,11 @@
/* INPUT PRIMITIVES */

#define LoadChar(dest) (dest = loadchar())
#define PushBack() (--_ipp)
#define ChPushBack(ch) (*--_ipp = (ch))

extern void INP_pushback(void);
#define PushBack INP_pushback

#define ChPushBack(ch) (INP_pushback(), *_ipp = (ch))

/* EOI may be defined as -1 in most programs but the character -1 may
be expanded to the int -1 which causes troubles with indexing.

0 comments on commit 5c28a90

Please sign in to comment.