fix leaks
authorMichael Wallner <mike@php.net>
Fri, 7 Dec 2018 08:33:59 +0000 (09:33 +0100)
committerMichael Wallner <mike@php.net>
Fri, 7 Dec 2018 08:33:59 +0000 (09:33 +0100)
12 files changed:
TODO
src/cpp.c
src/cpp.h
src/cpp_tokiter.c
src/debug.h
src/dump.h
src/error.c
src/parser_proc.c
src/parser_proc_grammar.y
src/parser_scan.c
src/parser_scan.re
src/token.c

diff --git a/TODO b/TODO
index b26f698..7dc89a4 100644 (file)
--- a/TODO
+++ b/TODO
@@ -22,4 +22,4 @@
 * optimized inlines like htonl
 * real integral type of enums
 
-* #pragma lib "foo" instead of lib "foo";
+* #pragma PSI lib "foo" instead of lib "foo";
index 26e324f..4b11c25 100644 (file)
--- a/src/cpp.c
+++ b/src/cpp.c
@@ -306,10 +306,10 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
                if (cpp->skip) {
                        if (!cpp->do_cpp) {
 #if PSI_CPP_DEBUG
-                               psi_debug_lock(PSI_DATA(cpp->parser));
-                               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP skip ");
-                               PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, current);
-                               psi_debug_unlock(PSI_DATA(cpp->parser));
+                               PSI_DEBUG_LOCK(cpp->parser,
+                                       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP skip ");
+                                       PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, current);
+                               );
 #endif
                                psi_cpp_tokiter_del_cur(cpp, true);
                                continue;
@@ -339,6 +339,7 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
 
                        /* leave EOLs in the input stream, else we might end up
                         * with a hash not preceded with a new line after include */
+                       psi_cpp_tokiter_add(cpp, current);
                        psi_plist_pop(cpp->tokens.exec, NULL);
                        psi_plist_clean(cpp->tokens.exec);
 
@@ -356,6 +357,7 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
        }
 
        psi_plist_free(cpp->tokens.exec);
+       cpp->tokens.exec = NULL;
 
        return true;
 }
@@ -380,19 +382,14 @@ bool psi_cpp_process(struct psi_cpp *cpp, struct psi_plist **tokens,
                zend_hash_del(&cpp->expanding, expanding->text);
        }
 
-       if (cpp->tokens.next) {
-               free(cpp->tokens.iter);
-               cpp->tokens.iter = cpp->tokens.next;
-               cpp->tokens.next = NULL;
+       *tokens = cpp->tokens.next;
+       psi_plist_free(cpp->tokens.iter);
+       if (cpp->tokens.exec) {
+               assert(!psi_plist_count(cpp->tokens.exec));
+               psi_plist_free(cpp->tokens.exec);
        }
 
-       *tokens = cpp->tokens.iter;
-
-       if (temp.tokens.iter) {
-               cpp->tokens.iter = temp.tokens.iter;
-               cpp->tokens.next = temp.tokens.next;
-               cpp->tokens.exec = temp.tokens.exec;
-       }
+       cpp->tokens = temp.tokens;
        cpp->index = temp.index;
        cpp->skip = temp.skip;
        cpp->level = temp.level;
@@ -410,33 +407,29 @@ bool psi_cpp_defined(struct psi_cpp *cpp, struct psi_token *tok)
                if (psi_builtin_exists(tok->text)) {
                        defined = true;
                } else if (!zend_hash_exists(&cpp->expanding, tok->text)) {
-                       struct psi_macro_decl *macro = zend_hash_find_ptr(&cpp->defs, tok->text);
-
-                       if (macro) {
-                               defined = true;
-                       }
+                       defined = zend_hash_exists(&cpp->defs, tok->text);
                }
 #if PSI_CPP_DEBUG
-               psi_debug_lock(PSI_DATA(cpp->parser));
-               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP defined -> %s ", defined ? "true" : "false");
-               if (defined) {
-                       struct psi_cpp_macro_decl *macro = zend_hash_find_ptr(&cpp->defs, tok->text);
-                       if (macro) {
-                               PSI_DEBUG_PRINT(cpp->parser, " @ %s:%u ", macro->token->file->val, macro->token->line);
-                       }
-               } else {
-                       zend_string *key;
+               PSI_DEBUG_LOCK(cpp->parser,
+                               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP defined -> %s ", defined ? "true" : "false");
+                               if (defined) {
+                                       struct psi_cpp_macro_decl *macro = zend_hash_find_ptr(&cpp->defs, tok->text);
+                                       if (macro) {
+                                               PSI_DEBUG_PRINT(cpp->parser, " @ %s:%u ", macro->token->file->val, macro->token->line);
+                                       }
+                               } else {
+                                       zend_string *key;
 
-                       PSI_DEBUG_PRINT(cpp->parser, " expanding=");
-                       ZEND_HASH_FOREACH_STR_KEY(&cpp->expanding, key)
-                       {
-                               PSI_DEBUG_PRINT(cpp->parser, "%s,", key->val);
-                       }
-                       ZEND_HASH_FOREACH_END();
-                       PSI_DEBUG_PRINT(cpp->parser, "\t");
-               }
-               PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, tok);
-               psi_debug_unlock(PSI_DATA(cpp->parser));
+                                       PSI_DEBUG_PRINT(cpp->parser, " expanding=");
+                                       ZEND_HASH_FOREACH_STR_KEY(&cpp->expanding, key)
+                                       {
+                                               PSI_DEBUG_PRINT(cpp->parser, "%s,", key->val);
+                                       }
+                                       ZEND_HASH_FOREACH_END();
+                                       PSI_DEBUG_PRINT(cpp->parser, "\t");
+                               }
+                               PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, tok);
+               );
 #endif
        }
 
@@ -454,15 +447,15 @@ void psi_cpp_define(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl)
                                "'%s' previously defined", old->token->text->val);
        }
 #if PSI_CPP_DEBUG
-       psi_debug_lock(PSI_DATA(cpp->parser));
-       if (decl->exp) {
-               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO num_exp -> ");
-       } else {
-               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO decl    -> ");
-       }
-       PSI_DEBUG_DUMP(cpp->parser, psi_cpp_macro_decl_dump, decl);
-       PSI_DEBUG_PRINT(cpp->parser, "\n");
-       psi_debug_unlock(PSI_DATA(cpp->parser));
+       PSI_DEBUG_LOCK(cpp->parser,
+                       if (decl->exp) {
+                               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO num_exp -> ");
+                       } else {
+                               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO decl    -> ");
+                       }
+                       PSI_DEBUG_DUMP(cpp->parser, psi_cpp_macro_decl_dump, decl);
+                       PSI_DEBUG_PRINT(cpp->parser, "\n");
+       );
 #endif
        zend_hash_update_ptr(&cpp->defs, decl->token->text, decl);
 }
index be7a54c..66a14fb 100644 (file)
--- a/src/cpp.h
+++ b/src/cpp.h
@@ -29,7 +29,7 @@
 #include "data.h"
 
 #ifndef PSI_CPP_DEBUG
-# define PSI_CPP_DEBUG 1
+# define PSI_CPP_DEBUG 0
 #endif
 
 struct psi_cpp {
index 2a8fc39..0e4a0db 100644 (file)
@@ -61,9 +61,11 @@ void psi_cpp_tokiter_dump(struct psi_dump *dump, struct psi_cpp *cpp)
 void psi_cpp_tokiter_reset(struct psi_cpp *cpp)
 {
 #if PSI_CPP_DEBUG
-       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP reset -> iter.count=%zu, next.count=%zu\n",
+       PSI_DEBUG_PRINT(cpp->parser,
+                       "PSI: CPP reset -> iter.count=%zu, next.count=%zu, exec.count=%zu\n",
                        psi_plist_count(cpp->tokens.iter),
-                       psi_plist_count(cpp->tokens.next));
+                       psi_plist_count(cpp->tokens.next),
+                       psi_plist_count(cpp->tokens.exec));
 #      if PSI_CPP_DEBUG > 1
        PSI_DEBUG_DUMP(cpp->parser, psi_cpp_tokiter_dump, cpp);
 #      endif
@@ -76,11 +78,16 @@ void psi_cpp_tokiter_reset(struct psi_cpp *cpp)
        cpp->do_cpp = false;
 
        if (cpp->tokens.next) {
-               free(cpp->tokens.iter);
+               psi_plist_free(cpp->tokens.iter);
                cpp->tokens.iter = cpp->tokens.next;
        }
        cpp->tokens.next = psi_plist_init((psi_plist_dtor) psi_token_free);
-       cpp->tokens.exec = psi_plist_init((psi_plist_dtor) psi_token_free);
+       if (cpp->tokens.exec) {
+               assert(!psi_plist_count(cpp->tokens.exec));
+               psi_plist_clean(cpp->tokens.exec);
+       } else {
+               cpp->tokens.exec = psi_plist_init((psi_plist_dtor) psi_token_free);
+       }
 }
 
 bool psi_cpp_tokiter_seek(struct psi_cpp *cpp, size_t index)
@@ -112,6 +119,7 @@ bool psi_cpp_tokiter_add_cur(struct psi_cpp *cpp)
        struct psi_token *cur = NULL;
 
        if (psi_plist_get(cpp->tokens.iter, cpp->index, &cur)) {
+               psi_plist_unset(cpp->tokens.iter, cpp->index);
                if (cpp->do_cpp) {
                        cpp->tokens.exec = psi_plist_add(cpp->tokens.exec, &cur);
                } else {
@@ -154,8 +162,6 @@ bool psi_cpp_tokiter_add(struct psi_cpp *cpp, struct psi_token *tok)
 
 bool psi_cpp_tokiter_add_range(struct psi_cpp *cpp, size_t num_eles, void **eles)
 {
-       struct psi_plist *tokens;
-
        if (!num_eles) {
                return true;
        }
@@ -207,17 +213,12 @@ bool psi_cpp_tokiter_del_prev(struct psi_cpp *cpp, bool free_token)
 #endif
 
        if (cpp->do_cpp) {
-               if (!psi_plist_pop(cpp->tokens.exec, NULL)) {
+               if (!psi_plist_pop(cpp->tokens.exec, &cur)) {
                        return false;
                }
-       } else if (!psi_plist_pop(cpp->tokens.next, NULL)) {
-               return false;
-       }
-
-       if (!psi_plist_get(cpp->tokens.iter, cpp->index - 1, &cur)) {
+       } else if (!psi_plist_pop(cpp->tokens.next, &cur)) {
                return false;
        }
-       psi_plist_unset(cpp->tokens.iter, cpp->index - 1);
        if (free_token && cur) {
                psi_token_free(&cur);
        }
@@ -239,7 +240,11 @@ bool psi_cpp_tokiter_del_cur(struct psi_cpp *cpp, bool free_token)
 #if PSI_CPP_DEBUG
                PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, cur);
 #endif
-               psi_plist_unset(cpp->tokens.iter, cpp->index);
+               if (cpp->index == psi_plist_count(cpp->tokens.iter) - 1) {
+                       psi_plist_pop(cpp->tokens.iter, NULL);
+               } else {
+                       psi_plist_unset(cpp->tokens.iter, cpp->index);
+               }
                if (free_token && cur) {
                        psi_token_free(&cur);
                }
@@ -280,26 +285,6 @@ bool psi_cpp_tokiter_del_range(struct psi_cpp *cpp, size_t offset, size_t num_el
        return true;
 }
 
-bool psi_cpp_tokiter_ins_range(struct psi_cpp *cpp, size_t num_eles, void **eles)
-{
-       struct psi_plist *tokens;
-
-       if (!num_eles) {
-               return true;
-       }
-
-       cpp->tokens.iter = psi_plist_ins_r(cpp->tokens.iter, cpp->index, num_eles, eles);
-
-#if PSI_CPP_DEBUG
-       PSI_DEBUG_PRINT(cpp->parser,
-                       "PSI: CPP ins_range -> index=%zu, num_eles=%zu, iter.count=%zu, next.count=%zu, exec.count=%zu\n",
-                       cpp->index, num_eles, psi_plist_count(cpp->tokens.iter), psi_plist_count(cpp->tokens.next),
-                       psi_plist_count(cpp->tokens.exec));
-#endif
-
-       return true;
-}
-
 bool psi_cpp_tokiter_defined(struct psi_cpp *cpp)
 {
        if (psi_cpp_tokiter_valid(cpp)) {
@@ -642,6 +627,7 @@ static inline bool psi_cpp_tokiter_expand_def(struct psi_cpp *cpp,
                        /* replace with tokens from macro */
                        psi_cpp_tokiter_expand_tokens(cpp, target, tokens);
                }
+               psi_plist_free(tokens);
                ++cpp->expanded;
        }
 
index eee6d44..02b2481 100644 (file)
@@ -39,6 +39,14 @@ void psi_debug_unlock(struct psi_data *data);
 # define psi_debug_unlock(ctx)
 #endif
 
+#define PSI_DEBUG_LOCK(ctx, group_actions) do { \
+       if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
+               psi_debug_lock(PSI_DATA(ctx)); \
+               group_actions; \
+               psi_debug_unlock(PSI_DATA(ctx)); \
+       } \
+} while(0)
+
 #define PSI_DEBUG_PRINT(ctx, ...) do { \
        if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
                psi_debug_lock(PSI_DATA(ctx)); \
index a887f3d..b64d355 100644 (file)
@@ -48,7 +48,7 @@ const struct psi_dump psi_dump_stderr;
 #define PSI_DUMP(dump, ...) do { \
        const struct psi_dump *_dump_ptr = dump; \
        if (!_dump_ptr) { \
-               _dump_ptr = &psi_dump_stdout; \
+               _dump_ptr = &psi_dump_stderr; \
        } \
        _dump_ptr->fun(_dump_ptr->ctx, __VA_ARGS__); \
 } while(0)
index 50ca092..8abb686 100644 (file)
@@ -78,10 +78,10 @@ void psi_error_wrapper(struct psi_data *context, struct psi_token *t, int type,
        va_end(argv);
 
        va_start(argv, msg);
-       psi_debug_lock(context);
-       PSI_DEBUG_PRINTV(context, msg, argv);
-       PSI_DEBUG_PRINT(context, "\n");
-       psi_debug_unlock(context);
+       PSI_DEBUG_LOCK(context,
+                       PSI_DEBUG_PRINTV(context, msg, argv);
+                       PSI_DEBUG_PRINT(context, "\n");
+       );
        va_end(argv);
 
        if (context) {
index 206056a..cb998f0 100644 (file)
@@ -9442,7 +9442,7 @@ static int psi_parser_proc_lex(YYSTYPE *lvalp, struct psi_parser *P, struct psi_
 #if PSI_DEBUG_LEX
        PSI_DEBUG_PRINT(P, "PSI: LEX index %4zu ", *index);
 #endif
-       if (psi_plist_get(tokens, (*index)++, &token)) {
+       if (psi_plist_get(tokens, (*index)++, &token) && token) {
 #if PSI_DEBUG_LEX      
                PSI_DEBUG_DUMP(P, psi_token_dump, token);
 #endif
index 7a3cf55..e0eca66 100644 (file)
@@ -2296,7 +2296,7 @@ static int psi_parser_proc_lex(YYSTYPE *lvalp, struct psi_parser *P, struct psi_
 #if PSI_DEBUG_LEX
        PSI_DEBUG_PRINT(P, "PSI: LEX index %4zu ", *index);
 #endif
-       if (psi_plist_get(tokens, (*index)++, &token)) {
+       if (psi_plist_get(tokens, (*index)++, &token) && token) {
 #if PSI_DEBUG_LEX      
                PSI_DEBUG_DUMP(P, psi_token_dump, token);
 #endif
index 3f8e51c..9d52d10 100644 (file)
@@ -1,4 +1,4 @@
-/* Generated by re2c 1.1.1 on Tue Dec  4 17:43:28 2018 */
+/* Generated by re2c 1.1.1 on Fri Dec  7 08:54:40 2018 */
 #line 1 "src/parser_scan.re"
 /*******************************************************************************
  Copyright (c) 2016, Michael Wallner <mike@php.net>.
@@ -56,10 +56,10 @@ size_t psi_parser_maxfill(void) {
                token = psi_token_init(t, tok, cur - tok, tok - eol + 1, I->lines, I->file); \
        } \
        tokens = psi_plist_add(tokens, &token); \
-       psi_debug_lock(PSI_DATA(P)); \
-       PSI_DEBUG_PRINT(P, "PSI: scanned < "); \
-       PSI_DEBUG_DUMP(P, psi_token_dump, token); \
-       psi_debug_unlock(PSI_DATA(P)); \
+       PSI_DEBUG_LOCK(P, \
+                       PSI_DEBUG_PRINT(P, "PSI: scanned < "); \
+                       PSI_DEBUG_DUMP(P, psi_token_dump, token); \
+       ); \
 } while(0)
 
 #define CHECKEOF() if (cur >= lim - YYMAXFILL) goto done
index f4ae1fd..f280b86 100644 (file)
@@ -53,10 +53,10 @@ size_t psi_parser_maxfill(void) {
                token = psi_token_init(t, tok, cur - tok, tok - eol + 1, I->lines, I->file); \
        } \
        tokens = psi_plist_add(tokens, &token); \
-       psi_debug_lock(PSI_DATA(P)); \
-       PSI_DEBUG_PRINT(P, "PSI: scanned < "); \
-       PSI_DEBUG_DUMP(P, psi_token_dump, token); \
-       psi_debug_unlock(PSI_DATA(P)); \
+       PSI_DEBUG_LOCK(P, \
+                       PSI_DEBUG_PRINT(P, "PSI: scanned < "); \
+                       PSI_DEBUG_DUMP(P, psi_token_dump, token); \
+       ); \
 } while(0)
 
 #define CHECKEOF() if (cur >= lim - YYMAXFILL) goto done
index 7a61007..f3beb2c 100644 (file)
@@ -52,7 +52,8 @@ struct psi_token *psi_token_init(token_t token_typ, const char *token_txt,
        T->file = zend_string_copy(file);
        T->text = psi_string_init_interned(token_txt, token_len, 1);
 #if PSI_DEBUG_TOKEN_ALLOC
-       PSI_DEBUG_PRINT(cpp->parser, "PSI: token_init %p\n", T);
+       fprintf(stderr, "PSI: token_init %p\t", T);
+       psi_token_dump(NULL, T);
 #endif
        return T;
 }
@@ -61,7 +62,8 @@ void psi_token_free(struct psi_token **token_ptr) {
        if (*token_ptr) {
                struct psi_token *token = *token_ptr;
 #if PSI_DEBUG_TOKEN_ALLOC
-               PSI_DEBUG_PRINT(cpp->parser, "PSI: token_free %p\n", token);
+               fprintf(stderr, "PSI: token_free %p\t", token);
+               psi_token_dump(NULL, token);
 #endif
                *token_ptr = NULL;
                zend_string_release(token->text);
@@ -75,7 +77,8 @@ struct psi_token *psi_token_copy(struct psi_token *src) {
 
        *ptr = *src;
 #if PSI_DEBUG_TOKEN_ALLOC
-       PSI_DEBUG_PRINT(cpp->parser, "PSI: token_copy %p <= %p\n", ptr, src);
+       fprintf(stderr, "PSI: token_copy %p\t", ptr);
+       psi_token_dump(NULL, src);
 #endif
        ptr->text = zend_string_copy(ptr->text);
        ptr->file = zend_string_copy(ptr->file);
@@ -114,7 +117,7 @@ struct psi_token *psi_token_cat(const char *sep, unsigned argc, ...) {
        T->text = smart_str_extract(&text);
 
 #if PSI_DEBUG_TOKEN_ALLOC
-       PSI_DEBUG_PRINT(cpp->parser, "PSI: token_cat  %p\n", T);
+       fprintf(stderr, "PSI: token_cat  %p\n", T);
 #endif
        return T;
 }