Compare commits

...

8 Commits

Author SHA1 Message Date
Anthony J. Bentley
c6c7b99fad PATH_MAX is not exactly portable. Hack around it for now. 2014-10-10 03:48:52 -06:00
Anthony J. Bentley
e28d8384fb Merge branch 'fix-makefile' of https://github.com/chastai/rgbds 2014-10-06 02:01:21 -06:00
Anthony J. Bentley
27a350eaab Merge branch 'fix-uniquearg-segfault' of https://github.com/chastai/rgbds 2014-10-06 01:55:19 -06:00
Anthony J. Bentley
cd8d895c1b Style. 2014-10-06 01:54:03 -06:00
Christophe Staïesse
4577a01c68 Fix out of bounds array access on invalid macro arg references
A reference to an invalid macro argument (\ not followed by a digit
between 1 and 9) will cause an access outside of the bounds of the
currentmacroargs array in sym_FindMacroArg().

Macro arg references are processed in two places:

In CopyMacroArg(): called when scanning tokens between "", {} and
arguments of a macro call. The only problem here is that it accepts \0
as valid and so calls sym_FindMacroArg with a invalid value.

In PutMacroArg(): called by the lexer automata when it encounters a
token matching \\[0-9]? (in other cases than above). So not only it
accepts \0 but also \ alone.
  Memo: In setuplex(), a rule is defined with a regex composed of up to
    three ranges of chars and takes the form:
      [FirstRange]
      or [FirstRange][SecondRange]?
      or [FirstRange]([SecondRange][Range]*)?
    On scanning, when several rules match, the first longuest one is
    choosen.

Regression test:
1)
SECTION "HOME", HOME
	db "\0"

2)
SECTION "HOME", HOME
	db \A

3)
SECTION "HOME", HOME
	db \
2014-10-05 18:15:37 +02:00
Christophe Staïesse
6758387668 Add assertion to symFindMacroArg() to debug oob array access 2014-10-05 18:13:07 +02:00
Christophe Staïesse
424702b272 Fix bug: rgbasm segfault when \@ is used outside a macro
The problem occurs when \@ is accessed outside the definition of a
macro and is not between "" or {}, or when it is used in an argument of
a macro call, i.e. when the access is processed by PutUniqueArg().

PutUniqueArg(), puts back the string value of \@ to the lexer's buffer
(to substitute \@ by the unique label string).
When \@ is not defined, sym_FindMacroArg(-1) returns NULL, which
yyunputstr() doesn't expect and crashes.

Solution:
Generate an error when \@ is not defined.

Regression test:

SECTION "HOME", HOME
        ld a,\@

Will segfault.
On the fixed version, it will return an error as \@ is not available.
2014-10-05 13:32:18 +02:00
Christophe Staïesse
191f98a008 Fix incorrect dependencies in Makefile 2014-10-05 11:37:11 +02:00
5 changed files with 23 additions and 7 deletions

View File

@@ -79,7 +79,7 @@ rgbfix: ${rgbfix_obj}
.c.o:
$Q${CC} ${REALCFLAGS} -c -o $@ $<
src/asm/gameboy/locallex.c src/asm/globlex.c src/asm/lexer.c: src/asm/asmy.h
src/asm/gameboy/locallex.o src/asm/globlex.o src/asm/lexer.o: src/asm/asmy.h
src/asm/asmy.h: src/asm/asmy.c
src/asm/asmy.y: ${yacc_pre}

View File

@@ -19,6 +19,10 @@
#include "extern/err.h"
#include "extern/strl.h"
#ifndef PATH_MAX
#define PATH_MAX 256
#endif
/*
* RGBAsm - FSTACK.C (FileStack routines)
*

View File

@@ -208,20 +208,30 @@ PutMacroArg(char *src, ULONG size)
char *s;
yyskipbytes(size);
if ((size == 2 && src[1] >= '1' && src[1] <= '9')) {
if ((s = sym_FindMacroArg(src[1] - '0')) != NULL) {
yyunputstr(s);
} else {
yyerror("Macro argument not defined");
}
} else {
yyerror("Invalid macro argument");
}
return (0);
}
ULONG
PutUniqueArg(char *src, ULONG size)
{
char *s;
src = src;
yyskipbytes(size);
yyunputstr(sym_FindMacroArg(-1));
if ((s = sym_FindMacroArg(-1)) != NULL) {
yyunputstr(s);
} else {
yyerror("Macro unique label string not defined");
}
return (0);
}
@@ -387,7 +397,7 @@ setuplex(void)
id = lex_FloatAlloc(&tMacroArgToken);
lex_FloatAddFirstRange(id, '\\', '\\');
lex_FloatAddSecondRange(id, '0', '9');
lex_FloatAddSecondRange(id, '1', '9');
id = lex_FloatAlloc(&tMacroUniqueToken);
lex_FloatAddFirstRange(id, '\\', '\\');
lex_FloatAddSecondRange(id, '@', '@');

View File

@@ -437,7 +437,6 @@ CopyMacroArg(char *dest, size_t maxLength, char c)
int argNum;
switch (c) {
case '0':
case '1':
case '2':
case '3':

View File

@@ -5,6 +5,7 @@
*
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
@@ -431,6 +432,8 @@ sym_FindMacroArg(SLONG i)
if (i == -1)
i = MAXMACROARGS + 1;
assert(i-1 >= 0 &&
i-1 < sizeof currentmacroargs / sizeof *currentmacroargs);
return (currentmacroargs[i - 1]);
}