Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GoogleCodeExporter opened this issue Oct 14, 2015 · 2 comments
Open

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 10 Feb 2014 at 4:49

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 5 Aug 2015 at 2:30

@GoogleCodeExporter
Copy link
Author

This is fixed on my fork at https://github.com/galacticstudios/picoc

Original comment by [email protected] on 17 Aug 2015 at 3:09

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant