]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.c
Fix more bugs (mostly possible NULL pointer dereferences)
[xonotic/gmqcc.git] / parser.c
index 38ec85e37b46ee5f8531d63c271e10e7946b3b93..6216997879e461e4c759cb17ec2959b9e362f5aa 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -104,9 +104,6 @@ typedef struct parser_s {
 
     /* collected information */
     size_t     max_param_count;
-
-    /* code generator */
-    code_t     *code;
 } parser_t;
 
 static ast_expression * const intrinsic_debug_typestring = (ast_expression*)0x1;
@@ -3625,7 +3622,8 @@ static bool parse_goto(parser_t *parser, ast_expression **out)
         if (!(expression = parse_expression(parser, false, true)) ||
             !(*out = parse_goto_computed(parser, &expression))) {
             parseerror(parser, "invalid goto expression");
-            ast_unref(expression);
+            if(expression)
+                ast_unref(expression);
             return false;
         }
 
@@ -6212,7 +6210,7 @@ static uint16_t progdefs_crc_both(uint16_t old, const char *str)
     return old;
 }
 
-static void generate_checksum(parser_t *parser)
+static void generate_checksum(parser_t *parser, ir_builder *ir)
 {
     uint16_t   crc = 0xFFFF;
     size_t     i;
@@ -6266,8 +6264,7 @@ static void generate_checksum(parser_t *parser)
         crc = progdefs_crc_both(crc, ";\n");
     }
     crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
-
-    parser->code->crc = crc;
+    ir->code->crc = crc;
 }
 
 parser_t *parser_create()
@@ -6282,11 +6279,6 @@ parser_t *parser_create()
 
     memset(parser, 0, sizeof(*parser));
 
-    if (!(parser->code = code_init())) {
-        mem_d(parser);
-        return NULL;
-    }
-
     for (i = 0; i < operator_count; ++i) {
         if (operators[i].id == opid1('=')) {
             parser->assign_op = operators+i;
@@ -6312,8 +6304,9 @@ parser_t *parser_create()
     vec_push(parser->correct_variables, correct_trie_new());
     vec_push(parser->correct_variables_score, NULL);
 
-    empty_ctx.file = "<internal>";
-    empty_ctx.line = 0;
+    empty_ctx.file   = "<internal>";
+    empty_ctx.line   = 0;
+    empty_ctx.column = 0;
     parser->nil = ast_value_new(empty_ctx, "nil", TYPE_NIL);
     parser->nil->cvq = CV_CONST;
     if (OPTS_FLAG(UNTYPED_NIL))
@@ -6472,8 +6465,6 @@ static void parser_remove_ast(parser_t *parser)
 void parser_cleanup(parser_t *parser)
 {
     parser_remove_ast(parser);
-    code_cleanup(parser->code);
-
     mem_d(parser);
 }
 
@@ -6631,7 +6622,8 @@ bool parser_finish(parser_t *parser, const char *output)
         }
     }
 
-    generate_checksum(parser);
+    generate_checksum(parser, ir);
+
     if (OPTS_OPTION_BOOL(OPTION_DUMP))
         ir_builder_dump(ir, con_out);
     for (i = 0; i < vec_size(parser->functions); ++i) {
@@ -6653,7 +6645,7 @@ bool parser_finish(parser_t *parser, const char *output)
         if (OPTS_OPTION_BOOL(OPTION_DUMPFIN))
             ir_builder_dump(ir, con_out);
 
-        if (!ir_builder_generate(parser->code, ir, output)) {
+        if (!ir_builder_generate(ir, output)) {
             con_out("*** failed to generate output file\n");
             ir_builder_delete(ir);
             return false;