From 84cd9f2db9bea744d99b2afc2c89d480732c3e23 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Fri, 27 Mar 2020 11:37:29 +0100 Subject: [PATCH] Fix segfaults when using PC outside a section --- src/asm/rpn.c | 5 ++++- src/asm/symbol.c | 4 +++- test/asm/assert@-no-sect.asm | 1 + test/asm/assert@-no-sect.err | 3 +++ test/asm/assert@-no-sect.out | 0 test/asm/if@-no-sect.asm | 2 ++ test/asm/if@-no-sect.err | 3 +++ test/asm/if@-no-sect.out | 0 8 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 test/asm/assert@-no-sect.asm create mode 100644 test/asm/assert@-no-sect.err create mode 100644 test/asm/assert@-no-sect.out create mode 100644 test/asm/if@-no-sect.asm create mode 100644 test/asm/if@-no-sect.err create mode 100644 test/asm/if@-no-sect.out diff --git a/src/asm/rpn.c b/src/asm/rpn.c index 24f43239..6ca9174c 100644 --- a/src/asm/rpn.c +++ b/src/asm/rpn.c @@ -106,7 +106,10 @@ void rpn_Symbol(struct Expression *expr, char *tzSym) { struct sSymbol *sym = sym_FindSymbol(tzSym); - if (!sym || !sym_IsConstant(sym)) { + if (sym == pPCSymbol && !pPCSymbol->pSection) { + yyerror("PC has no value outside a section"); + rpn_Number(expr, 0); + } else if (!sym || !sym_IsConstant(sym)) { rpn_Init(expr); expr->isSymbol = true; diff --git a/src/asm/symbol.c b/src/asm/symbol.c index 6b18ed3e..9d969539 100644 --- a/src/asm/symbol.c +++ b/src/asm/symbol.c @@ -230,7 +230,9 @@ uint32_t sym_GetConstantValue(char const *s) struct sSymbol const *psym = sym_FindSymbol(s); if (psym == pPCSymbol) { - if (pCurrentSection->nOrg == -1) + if (!pCurrentSection) + yyerror("PC has no value outside a section"); + else if (pCurrentSection->nOrg == -1) yyerror("Expected constant PC but section is not fixed"); else return sym_GetValue(psym); diff --git a/test/asm/assert@-no-sect.asm b/test/asm/assert@-no-sect.asm new file mode 100644 index 00000000..7df8df79 --- /dev/null +++ b/test/asm/assert@-no-sect.asm @@ -0,0 +1 @@ +assert @ || 1 diff --git a/test/asm/assert@-no-sect.err b/test/asm/assert@-no-sect.err new file mode 100644 index 00000000..68c7d037 --- /dev/null +++ b/test/asm/assert@-no-sect.err @@ -0,0 +1,3 @@ +ERROR: assert@-no-sect.asm(1): + PC has no value outside a section +error: Assembly aborted (1 errors)! diff --git a/test/asm/assert@-no-sect.out b/test/asm/assert@-no-sect.out new file mode 100644 index 00000000..e69de29b diff --git a/test/asm/if@-no-sect.asm b/test/asm/if@-no-sect.asm new file mode 100644 index 00000000..7c6072d9 --- /dev/null +++ b/test/asm/if@-no-sect.asm @@ -0,0 +1,2 @@ +if {@} +endc diff --git a/test/asm/if@-no-sect.err b/test/asm/if@-no-sect.err new file mode 100644 index 00000000..197db7bd --- /dev/null +++ b/test/asm/if@-no-sect.err @@ -0,0 +1,3 @@ +ERROR: if@-no-sect.asm(1): + PC has no value outside a section +error: Assembly aborted (1 errors)! diff --git a/test/asm/if@-no-sect.out b/test/asm/if@-no-sect.out new file mode 100644 index 00000000..e69de29b