zsaleeba / picoc

A very small C interpreter
1.45k stars 183 forks source link

Uninitialised structure field #25

Open klopp opened 8 years ago

klopp commented 8 years ago
$ cat main.c
void main() {}

$ valgrind --leak-check=yes --track-origins=yes ./picoc main.c
==30986== Memcheck, a memory error detector
==30986== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==30986== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==30986== Command: ./picoc main.c
==30986== 
==30986== Conditional jump or move depends on uninitialised value(s)
==30986==    at 0x4194A7: VariableScopeBegin (variable.c:177)
==30986==    by 0x414D3B: ParseBlock (parse.c:519)
==30986==    by 0x41516D: ParseStatement (parse.c:659)
==30986==    by 0x413638: ParseStatementMaybeRun (parse.c:30)
==30986==    by 0x413BCE: ParseFunctionDefinition (parse.c:146)
==30986==    by 0x414484: ParseDeclaration (parse.c:336)
==30986==    by 0x41564A: ParseStatement (parse.c:772)
==30986==    by 0x415E85: PicocParse (parse.c:966)
==30986==    by 0x404207: PicocPlatformScanFile (platform_unix.c:131)
==30986==    by 0x4161BD: main (picoc.c:54)
==30986==  Uninitialised value was created by a stack allocation
==30986==    at 0x415D58: PicocParse (parse.c:937)
==30986== 
==30986== Conditional jump or move depends on uninitialised value(s)
==30986==    at 0x4194A7: VariableScopeBegin (variable.c:177)
==30986==    by 0x414D3B: ParseBlock (parse.c:519)
==30986==    by 0x41516D: ParseStatement (parse.c:659)
==30986==    by 0x40FB6A: ExpressionParseFunctionCall (expression.c:1545)
==30986==    by 0x40EDBD: ExpressionParse (expression.c:1251)
==30986==    by 0x415123: ParseStatement (parse.c:653)
==30986==    by 0x415E85: PicocParse (parse.c:966)
==30986==    by 0x416523: PicocCallMain (platform.c:77)
==30986==    by 0x416242: main (picoc.c:57)
==30986==  Uninitialised value was created by a stack allocation
==30986==    at 0x415D58: PicocParse (parse.c:937)
==30986== 
==30986== 
==30986== HEAP SUMMARY:
==30986==     in use at exit: 0 bytes in 0 blocks
==30986==   total heap usage: 125 allocs, 125 frees, 136,140 bytes allocated
==30986== 
==30986== All heap blocks were freed -- no leaks are possible
==30986== 
==30986== For counts of detected and suppressed errors, rerun with: -v
==30986== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

Solution (lex.c):

void LexInitParser(struct ParseState *Parser, Picoc *pc, const char *SourceText, void *TokenSource, char *FileName, int RunIt, int EnableDebugger)
{
    Parser->ScopeID = 0; /* HERE */
    Parser->pc = pc;
yanghao commented 7 years ago

Is this project still maintained? Fix works for me.

dodona2 commented 7 years ago

Is this project still maintained? there is a more recent fork: https://github.com/jpoirier/picoc