Cleaned up lexer

- separated the lexer into multiple functions so it is more readable
- fixed issue with long label names in macro arguments
- added error checking code to prevent buffer overflows
This commit is contained in:
YamaArashi
2014-08-22 21:44:18 -07:00
parent 6198cc185c
commit 2bf31870a7
3 changed files with 490 additions and 506 deletions

View File

@@ -5,7 +5,8 @@
#include "asm/types.h" #include "asm/types.h"
#define LEXHASHSIZE 512 #define LEXHASHSIZE (1 << 11)
#define MAXSTRLEN 255
struct sLexInitString { struct sLexInitString {
char *tzName; char *tzName;
@@ -18,7 +19,9 @@ struct sLexFloat {
}; };
struct yy_buffer_state { struct yy_buffer_state {
char *pBufferStart; char *pBufferRealStart; // actual starting address
char *pBufferStart; // address where the data is initially written
// after the "safety margin"
char *pBuffer; char *pBuffer;
ULONG nBufferSize; ULONG nBufferSize;
ULONG oAtLineStart; ULONG oAtLineStart;

File diff suppressed because it is too large Load Diff

View File

@@ -21,14 +21,38 @@ extern bool haltnop;
char *tzNewMacro; char *tzNewMacro;
ULONG ulNewMacroSize; ULONG ulNewMacroSize;
ULONG symvaluetostring( char *dest, char *sym ) size_t symvaluetostring(char *dest, size_t maxLength, char *sym)
{ {
if( sym_isString(sym) ) size_t length;
strcpy( dest, sym_GetStringValue(sym) );
else
sprintf( dest, "$%lX", sym_GetConstantValue(sym) );
return( strlen(dest) ); if (sym_isString(sym)) {
char *src = sym_GetStringValue(sym);
size_t i;
for (i = 0; src[i] != 0; i++) {
if (i >= maxLength) {
fatalerror("Symbol value too long to fit buffer");
}
dest[i] = src[i];
}
length = i;
} else {
ULONG value = sym_GetConstantValue(sym);
int fullLength = snprintf(dest, maxLength + 1, "$%lX", value);
if (fullLength < 0) {
fatalerror("snprintf encoding error");
} else {
length = (size_t)fullLength;
if (length > maxLength) {
fatalerror("Symbol value too long to fit buffer");
}
}
}
return length;
} }
ULONG str2int( char *s ) ULONG str2int( char *s )
@@ -335,8 +359,8 @@ void if_skip_to_endc( void )
%union %union
{ {
char tzSym[MAXSYMLEN+1]; char tzSym[MAXSYMLEN + 1];
char tzString[256]; char tzString[MAXSTRLEN + 1];
struct Expression sVal; struct Expression sVal;
SLONG nConstValue; SLONG nConstValue;
} }