pabru / picoc

Automatically exported from code.google.com/p/picoc
0 stars 0 forks source link

#define within a #include file from interactive mode crashes #190

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I've tested this under both Linux and Max OS X with crashes under each system. 
Under OS X, I've tested several versions and not found it to work under any... 
tested: 603, 595, 580, 562, 520.

What steps will reproduce the problem?
[root@linux picoc-603]# cat includeme.h 
#define DEFINE_ME 1

[root@linux picoc-603]# ./picoc -i
starting picoc v2.2 beta r
picoc> #define DEFINE_ME_FIRST 1
picoc> #include "includeme.h"
Segmentation fault (core dumped)
[root@linux picoc-603]# 

What is the expected output? What do you see instead?
Expected output is that there should be no output. The test include file, 
includeme.h, has only a single line, a simple #define (as seen in the paste 
above). If the essential content of this file is typed at the interactive 
prompt then there is no problem, but if the file is #include'd instead, the 
#define causes picoc to crash. If the #define is removed or commented out, the 
crash does not occur.

Original issue reported on code.google.com by m...@heilpern.com on 10 Feb 2014 at 4:49

GoogleCodeExporter commented 9 years ago
The problem occurs whenever a #include statement is entered in interactive 
mode. The crash occurs in LexCopyTokens at the line:

if (EndParser->Pos >= StartParser->Pos && EndParser->Pos < 
&pc->InteractiveCurrentLine->Tokens[pc->InteractiveCurrentLine->NumBytes])

because pc->InteractiveCurrentLine is null. The root of the problem appears to 
be that Picoc_struct has global lexer data, but when you're in immediate mode 
and you #include, you're re-entering the lexer.

I believe I've solved this.

In include.c, in void IncludeFile(Picoc *pc, char *FileName), replace:

   PicocPlatformScanFile(pc, FileName);

with code that saves the global values, sets them to NULL, then restores them 
afterwards:

    {
        struct TokenLine *head = pc->InteractiveHead, *tail = pc->InteractiveTail, *line = pc->InteractiveCurrentLine;
        pc->InteractiveHead = pc->InteractiveTail = pc->InteractiveCurrentLine = NULL;
        PicocPlatformScanFile(pc, FileName);
        pc->InteractiveHead = head;
        pc->InteractiveTail = tail;
        pc->InteractiveCurrentLine = line;
    }

Original comment by goo...@LoadAccumulator.com on 5 Aug 2015 at 2:30

GoogleCodeExporter commented 9 years ago
This is fixed on my fork at https://github.com/galacticstudios/picoc

Original comment by goo...@LoadAccumulator.com on 17 Aug 2015 at 3:09