From 5122a99e441b1819a2bf47ce6eb4566e724c5800 Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Fri, 28 Jun 2019 20:52:13 +0700 Subject: [PATCH 1/2] Fix local variables being able to be self-assigned through initialization --- source/compiler/sc1.c | 3 +++ source/compiler/sc3.c | 2 ++ 2 files changed, 5 insertions(+) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 2fe3934..52bbc96 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -2359,7 +2359,10 @@ static int declloc(int fstatic) int ctag = tag; /* set to "tag" by default */ int explicit_init=FALSE;/* is the variable explicitly initialized? */ if (matchtoken('=')) { + sym->usage &= ~uDEFINE; /* temporarily mark the variable as undefined to prevent + * possible self-assignment through its initialization expression */ doexpr(FALSE,FALSE,FALSE,FALSE,&ctag,NULL,TRUE); + sym->usage |= uDEFINE; explicit_init=TRUE; } else { ldconst(0,sPRI); /* uninitialized variable, set to zero */ diff --git a/source/compiler/sc3.c b/source/compiler/sc3.c index cf9454d..993e358 100644 --- a/source/compiler/sc3.c +++ b/source/compiler/sc3.c @@ -1833,6 +1833,8 @@ static int primary(value *lval) ldconst(0,sPRI); /* load 0 */ return FALSE; /* return 0 for labels (expression error) */ } /* if */ + if ((sym->usage & uDEFINE)==0) + error_suggest(17,st,NULL,estSYMBOL,esfVARCONST); /* undefined symbol */ lval->sym=sym; lval->ident=sym->ident; lval->tag=sym->tag; From 1bb4e42917127ccd64e133a7f2e3b78ddeba9ab4 Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Fri, 28 Jun 2019 23:54:40 +0700 Subject: [PATCH 2/2] Add tests for #436 --- source/compiler/tests/gh_436_local_var_self_init.meta | 7 +++++++ source/compiler/tests/gh_436_local_var_self_init.pwn | 9 +++++++++ 2 files changed, 16 insertions(+) create mode 100644 source/compiler/tests/gh_436_local_var_self_init.meta create mode 100644 source/compiler/tests/gh_436_local_var_self_init.pwn diff --git a/source/compiler/tests/gh_436_local_var_self_init.meta b/source/compiler/tests/gh_436_local_var_self_init.meta new file mode 100644 index 0000000..ec50d22 --- /dev/null +++ b/source/compiler/tests/gh_436_local_var_self_init.meta @@ -0,0 +1,7 @@ +{ + 'test_type': 'output_check', + 'errors': """ +gh_436_local_var_self_init.pwn(6) : error 017: undefined symbol "test" +gh_436_local_var_self_init.pwn(7) : error 017: undefined symbol "test2"; did you mean "test"? +""" +} diff --git a/source/compiler/tests/gh_436_local_var_self_init.pwn b/source/compiler/tests/gh_436_local_var_self_init.pwn new file mode 100644 index 0000000..d796d46 --- /dev/null +++ b/source/compiler/tests/gh_436_local_var_self_init.pwn @@ -0,0 +1,9 @@ +#include + +main() +{ + new arr[2]; + new test = test; + new test2 = arr[test2]; + #pragma unused test, test2 +}