mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2024-11-23 20:33:05 +00:00
type parsing for constants, globals and locals. Sanatize constants to select internal functions to prevent possible runtime issues that could be a result of atoi working for what we consider invalid strings containing constants.
This commit is contained in:
parent
fd31c203dc
commit
de01d34925
4 changed files with 58 additions and 9 deletions
46
asm.c
46
asm.c
|
@ -78,11 +78,34 @@ void asm_clear() {
|
|||
* are locals.
|
||||
*/
|
||||
static inline bool asm_parse_type(const char *skip, size_t line, asm_state *state) {
|
||||
if (strstr(skip, "FLOAT:") == &skip[0]) { return true; }
|
||||
if (strstr(skip, "VECTOR:") == &skip[0]) { return true; }
|
||||
if (strstr(skip, "ENTITY:") == &skip[0]) { return true; }
|
||||
if (strstr(skip, "FIELD:") == &skip[0]) { return true; }
|
||||
if (strstr(skip, "STRING:") == &skip[0]) { return true; }
|
||||
if (!(strstr(skip, "FLOAT:") == &skip[0]) &&
|
||||
(strstr(skip, "VECTOR:") == &skip[0]) &&
|
||||
(strstr(skip, "ENTITY:") == &skip[0]) &&
|
||||
(strstr(skip, "FIELD:") == &skip[0]) &&
|
||||
(strstr(skip, "STRING:") == &skip[0])) return false;
|
||||
|
||||
/* TODO: determine if constant, global, or local */
|
||||
switch (*skip) {
|
||||
/* VECTOR */ case 'V': {
|
||||
const char *find = skip + 7;
|
||||
while (*find == ' ' || *find == '\t') find++;
|
||||
printf("found VECTOR %s\n", find);
|
||||
break;
|
||||
}
|
||||
/* ENTITY */ case 'E': {
|
||||
const char *find = skip + 7;
|
||||
while (*find == ' ' || *find == '\t') find++;
|
||||
printf("found ENTITY %s\n", find);
|
||||
break;
|
||||
}
|
||||
/* STRING */ case 'S': {
|
||||
const char *find = skip + 7;
|
||||
while (*find == ' ' || *find == '\t') find++;
|
||||
printf("found STRING %s\n", find);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -165,10 +188,19 @@ static inline bool asm_parse_func(const char *skip, size_t line, asm_state *stat
|
|||
code_chars_put(name, strlen(name));
|
||||
code_chars_add('\0');
|
||||
|
||||
/* TODO: sanatize `find` to ensure all numerical digits */
|
||||
|
||||
/*
|
||||
* Sanatize the numerical constant used to select the
|
||||
* internal function. Must ensure it's all numeric, since
|
||||
* atoi can silently drop characters from a string and still
|
||||
* produce a valid constant that would lead to runtime problems.
|
||||
*/
|
||||
if (util_strdigit(find))
|
||||
printf("found internal function %s, -%d\n", name, atoi(find));
|
||||
else
|
||||
printf("invalid internal function identifier, must be all numeric\n");
|
||||
|
||||
} else {
|
||||
/* TODO: function bodies */
|
||||
}
|
||||
|
||||
mem_d(copy);
|
||||
|
|
|
@ -82,4 +82,7 @@ FUNCTION: pow, $97
|
|||
FUNCTION: findfloat, $98
|
||||
FUNCTION: checkextension, $99
|
||||
|
||||
; todo support other crap
|
||||
; constants test
|
||||
VECTOR: 1, 2, 3
|
||||
FLOAT: 1
|
||||
STRING: "hello world"
|
||||
|
|
1
gmqcc.h
1
gmqcc.h
|
@ -195,6 +195,7 @@ void util_memory_d (void *, unsigned int, const char *);
|
|||
void util_meminfo ();
|
||||
|
||||
bool util_strupper (const char *);
|
||||
bool util_strdigit (const char *);
|
||||
char *util_strdup (const char *);
|
||||
char *util_strrq (char *);
|
||||
char *util_strrnl (char *);
|
||||
|
|
13
util.c
13
util.c
|
@ -179,6 +179,19 @@ bool util_strupper(const char *str) {
|
|||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns true if string is all digits, otherwise
|
||||
* it returns false.
|
||||
*/
|
||||
bool util_strdigit(const char *str) {
|
||||
while (*str) {
|
||||
if(!isdigit(*str))
|
||||
return false;
|
||||
str++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void util_debug(const char *area, const char *ms, ...) {
|
||||
if (!opts_debug)
|
||||
return;
|
||||
|
|
Loading…
Reference in a new issue