fix printf with threaded parser; replace SUCCESS/FAILURE with bool
authorMichael Wallner <mike@php.net>
Tue, 4 Dec 2018 10:26:15 +0000 (11:26 +0100)
committerMichael Wallner <mike@php.net>
Tue, 4 Dec 2018 11:13:51 +0000 (12:13 +0100)
src/call.c
src/call.h
src/context.c
src/context.h
src/data.c
src/parser.c

index a46fa4e325196c26edb2e06e9989facb81f5524d..704675c91f444abab532f63f0447fdabfcf68096 100644 (file)
 #else
 # include "php_config.h"
 #endif
 #else
 # include "php_config.h"
 #endif
+
 #include "context.h"
 #include "data.h"
 #include "call.h"
 
 #include "context.h"
 #include "data.h"
 #include "call.h"
 
+#include "php_psi.h"
+
 #include "php.h"
 #include "zend_exceptions.h"
 
 #include "php.h"
 #include "zend_exceptions.h"
 
@@ -259,7 +262,7 @@ struct psi_context *psi_call_frame_get_context(struct psi_call_frame *frame) {
 #else
 #      define PARAM_PROLOGUE(separate) Z_PARAM_PROLOGUE(1, separate)
 #endif
 #else
 #      define PARAM_PROLOGUE(separate) Z_PARAM_PROLOGUE(1, separate)
 #endif
-ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame,
+bool psi_call_frame_parse_args(struct psi_call_frame *frame,
                zend_execute_data *execute_data) {
        size_t i, argc = psi_plist_count(frame->impl->func->args);
        zend_error_handling zeh;
                zend_execute_data *execute_data) {
        size_t i, argc = psi_plist_count(frame->impl->func->args);
        zend_error_handling zeh;
@@ -271,7 +274,7 @@ ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame,
 
                rv = zend_parse_parameters_none();
                zend_restore_error_handling(&zeh);
 
                rv = zend_parse_parameters_none();
                zend_restore_error_handling(&zeh);
-               return rv;
+               return rv == SUCCESS;
        }
 
        ZEND_PARSE_PARAMETERS_START(
        }
 
        ZEND_PARSE_PARAMETERS_START(
@@ -337,7 +340,7 @@ ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame,
        }
        ZEND_PARSE_PARAMETERS_END_EX(
                zend_restore_error_handling(&zeh);
        }
        ZEND_PARSE_PARAMETERS_END_EX(
                zend_restore_error_handling(&zeh);
-               return FAILURE;
+               return false;
        );
 
        /* set up defaults */
        );
 
        /* set up defaults */
@@ -352,7 +355,7 @@ ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame,
        }
 
        zend_restore_error_handling(&zeh);
        }
 
        zend_restore_error_handling(&zeh);
-       return SUCCESS;
+       return true;
 }
 
 void psi_call_frame_enter(struct psi_call_frame *frame) {
 }
 
 void psi_call_frame_enter(struct psi_call_frame *frame) {
@@ -374,7 +377,7 @@ void psi_call_frame_enter(struct psi_call_frame *frame) {
        frame->rpointer = rv_sym->ptr = rv_sym->ival_ptr;
 }
 
        frame->rpointer = rv_sym->ptr = rv_sym->ival_ptr;
 }
 
-ZEND_RESULT_CODE psi_call_frame_do_let(struct psi_call_frame *frame) {
+bool psi_call_frame_do_let(struct psi_call_frame *frame) {
        size_t i;
        struct psi_let_stmt *let;
        struct psi_decl_arg *arg;
        size_t i;
        struct psi_let_stmt *let;
        struct psi_decl_arg *arg;
@@ -420,10 +423,10 @@ ZEND_RESULT_CODE psi_call_frame_do_let(struct psi_call_frame *frame) {
                }
        }
 
                }
        }
 
-       return SUCCESS;
+       return true;
 }
 
 }
 
-ZEND_RESULT_CODE psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi_assert_kind kind) {
+bool psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi_assert_kind kind) {
        size_t i = 0;
        struct psi_assert_stmt *ass;
 
        size_t i = 0;
        struct psi_assert_stmt *ass;
 
@@ -431,12 +434,12 @@ ZEND_RESULT_CODE psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi
                if (ass->kind == kind) {
                        if (!psi_assert_stmt_exec(ass, frame)) {
                                psi_assert_stmt_throw(ass);
                if (ass->kind == kind) {
                        if (!psi_assert_stmt_exec(ass, frame)) {
                                psi_assert_stmt_throw(ass);
-                               return FAILURE;
+                               return false;
                        }
                }
        }
 
                        }
                }
        }
 
-       return SUCCESS;
+       return true;
 }
 
 void psi_call_frame_do_call(struct psi_call_frame *frame) {
 }
 
 void psi_call_frame_do_call(struct psi_call_frame *frame) {
@@ -536,8 +539,6 @@ static void psi_call_frame_local_auto_dtor(void *auto_list)
        efree(auto_list);
 }
 
        efree(auto_list);
 }
 
-#include "php_psi.h"
-
 void psi_call_frame_free(struct psi_call_frame *frame) {
        zend_hash_destroy(&frame->arguments);
        zend_hash_destroy(&frame->symbols);
 void psi_call_frame_free(struct psi_call_frame *frame) {
        zend_hash_destroy(&frame->arguments);
        zend_hash_destroy(&frame->symbols);
index a6ad873b559a5738d2f7fa5bf113b025c7184a89..eb8361a5476e7be84ac15733519cc0ff8e1a80fc 100644 (file)
@@ -26,6 +26,7 @@
 #ifndef PSI_CALL_H
 #define PSI_CALL_H
 
 #ifndef PSI_CALL_H
 #define PSI_CALL_H
 
+#include <stdbool.h>
 #include "Zend/zend_types.h"
 
 #include "data.h"
 #include "Zend/zend_types.h"
 
 #include "data.h"
@@ -71,7 +72,7 @@ struct psi_call_frame {
 
 struct psi_call_frame *psi_call_frame_init(struct psi_context *context, struct psi_decl *decl, struct psi_impl *impl);
 
 
 struct psi_call_frame *psi_call_frame_init(struct psi_context *context, struct psi_decl *decl, struct psi_impl *impl);
 
-ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame, zend_execute_data *execute_data);
+bool psi_call_frame_parse_args(struct psi_call_frame *frame, zend_execute_data *execute_data);
 
 size_t psi_call_frame_num_var_args(struct psi_call_frame *frame);
 size_t psi_call_frame_num_fixed_args(struct psi_call_frame *frame);
 
 size_t psi_call_frame_num_var_args(struct psi_call_frame *frame);
 size_t psi_call_frame_num_fixed_args(struct psi_call_frame *frame);
@@ -92,8 +93,8 @@ struct psi_impl *psi_call_frame_get_impl(struct psi_call_frame *frame);
 void **psi_call_frame_get_arg_pointers(struct psi_call_frame *frame);
 void *psi_call_frame_get_rpointer(struct psi_call_frame *frame);
 
 void **psi_call_frame_get_arg_pointers(struct psi_call_frame *frame);
 void *psi_call_frame_get_rpointer(struct psi_call_frame *frame);
 
-ZEND_RESULT_CODE psi_call_frame_do_let(struct psi_call_frame *frame);
-ZEND_RESULT_CODE psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi_assert_kind kind);
+bool psi_call_frame_do_let(struct psi_call_frame *frame);
+bool psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi_assert_kind kind);
 void psi_call_frame_do_call(struct psi_call_frame *frame);
 void psi_call_frame_do_callback(struct psi_call_frame *frame, struct psi_call_frame_callback *cb);
 void psi_call_frame_do_return(struct psi_call_frame *frame, zval *return_value);
 void psi_call_frame_do_call(struct psi_call_frame *frame);
 void psi_call_frame_do_callback(struct psi_call_frame *frame, struct psi_call_frame_callback *cb);
 void psi_call_frame_do_return(struct psi_call_frame *frame, zval *return_value);
index f480ebdce05c7d9fd888a42ccb36a8acdd899ce7..308e11badbccfe6facb7b3ed5ddb97c63acff4f8 100644 (file)
@@ -154,10 +154,12 @@ static bool psi_context_add(struct psi_context *C, struct psi_parser *P)
 }
 
 struct psi_context_build_worker {
 }
 
 struct psi_context_build_worker {
+#if PSI_THREADED_PARSER
        pthread_t tid;
        pthread_t tid;
+#endif
        struct psi_parser parser;
        struct psi_parser_input *input;
        struct psi_parser parser;
        struct psi_parser_input *input;
-       char psi_file[MAXPATHLEN];
+       char psi_file[PATH_MAX];
 };
 
 static struct psi_context_build_worker *psi_context_build_worker_init(
 };
 
 static struct psi_context_build_worker *psi_context_build_worker_init(
@@ -165,7 +167,7 @@ static struct psi_context_build_worker *psi_context_build_worker_init(
 {
        struct psi_context_build_worker *w = pecalloc(1, sizeof(*w), 1);
 
 {
        struct psi_context_build_worker *w = pecalloc(1, sizeof(*w), 1);
 
-       if (MAXPATHLEN <= slprintf(w->psi_file, MAXPATHLEN, "%s/%s", dir, file)) {
+       if (PATH_MAX <= slprintf(w->psi_file, PATH_MAX, "%s/%s", dir, file)) {
                C->error(PSI_DATA(C), NULL, PSI_WARNING, "Path to PSI file too long: %s/%s",
                        dir, file);
                pefree(w, 1);
                C->error(PSI_DATA(C), NULL, PSI_WARNING, "Path to PSI file too long: %s/%s",
                        dir, file);
                pefree(w, 1);
@@ -370,7 +372,9 @@ void psi_context_build(struct psi_context *C, const char *paths)
                                }
                        }
                }
                                }
                        }
                }
+               psi_plist_free(running);
        }
        }
+       psi_plist_free(workers);
 
        psi_context_compile(C);
 }
 
        psi_context_compile(C);
 }
@@ -712,46 +716,6 @@ void **psi_context_composite_type_elements(struct psi_context *C,
        return psi_plist_eles(*eles);
 }
 
        return psi_plist_eles(*eles);
 }
 
-/*
-void psi_context_decl_func_array_elements(struct psi_context *C,
-               struct psi_decl *fn, struct psi_plist **els)
-{
-       void *type;
-       size_t i;
-
-       if (fn->func->var->pointer_level > 1) {
-               type = C->ops->typeof_decl(C, PSI_T_POINTER);
-       } else {
-               type = psi_context_decl_type(C, fn->func->type);
-       }
-
-       for (i = 0; i < fn->func->var->array_size; ++i) {
-               void *copy = C->ops->copyof_type(C, type);
-               *els = psi_plist_add(*els, &copy);
-       }
-}
-
-void *psi_context_decl_func_type(struct psi_context *C, struct psi_decl *fn)
-{
-       struct psi_decl_arg *darg = fn->func;
-
-       if (darg->engine.type) {
-               return darg->engine.type;
-       }
-
-       if (darg->var->pointer_level) {
-               if (!darg->var->array_size) {
-                       return C->ops->typeof_decl(C, PSI_T_POINTER);
-               } else {
-                       C->ops->composite_init(C, darg);
-                       return darg->engine.type;
-               }
-       }
-
-       return psi_context_decl_type(C, darg->type);
-}
-*/
-
 void psi_context_compile(struct psi_context *C)
 {
        psi_context_consts_init(C);
 void psi_context_compile(struct psi_context *C)
 {
        psi_context_consts_init(C);
@@ -767,32 +731,32 @@ void psi_context_compile(struct psi_context *C)
        EG(current_module) = NULL;
 }
 
        EG(current_module) = NULL;
 }
 
-ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *execute_data, zval *return_value, struct psi_impl *impl)
+bool psi_context_call(struct psi_context *C, zend_execute_data *execute_data, zval *return_value, struct psi_impl *impl)
 {
        struct psi_call_frame *frame;
 
        frame = psi_call_frame_init(C, impl->decl, impl);
 
 {
        struct psi_call_frame *frame;
 
        frame = psi_call_frame_init(C, impl->decl, impl);
 
-       if (SUCCESS != psi_call_frame_parse_args(frame, execute_data)) {
+       if (!psi_call_frame_parse_args(frame, execute_data)) {
                psi_call_frame_free(frame);
 
                psi_call_frame_free(frame);
 
-               return FAILURE;
+               return false;
        }
 
        psi_call_frame_enter(frame);
 
        }
 
        psi_call_frame_enter(frame);
 
-       if (SUCCESS != psi_call_frame_do_let(frame)) {
+       if (!psi_call_frame_do_let(frame)) {
                psi_call_frame_do_return(frame, return_value);
                psi_call_frame_free(frame);
 
                psi_call_frame_do_return(frame, return_value);
                psi_call_frame_free(frame);
 
-               return FAILURE;
+               return false;
        }
 
        }
 
-       if (SUCCESS != psi_call_frame_do_assert(frame, PSI_ASSERT_PRE)) {
+       if (!psi_call_frame_do_assert(frame, PSI_ASSERT_PRE)) {
                psi_call_frame_do_return(frame, return_value);
                psi_call_frame_free(frame);
 
                psi_call_frame_do_return(frame, return_value);
                psi_call_frame_free(frame);
 
-               return FAILURE;
+               return false;
        }
 
        if (psi_call_frame_num_var_args(frame)) {
        }
 
        if (psi_call_frame_num_var_args(frame)) {
@@ -801,11 +765,11 @@ ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *exec
                C->ops->call(frame);
        }
 
                C->ops->call(frame);
        }
 
-       if (SUCCESS != psi_call_frame_do_assert(frame, PSI_ASSERT_POST)) {
+       if (!psi_call_frame_do_assert(frame, PSI_ASSERT_POST)) {
                psi_call_frame_do_return(frame, return_value);
                psi_call_frame_free(frame);
 
                psi_call_frame_do_return(frame, return_value);
                psi_call_frame_free(frame);
 
-               return FAILURE;
+               return false;
        }
 
        psi_call_frame_do_return(frame, return_value);
        }
 
        psi_call_frame_do_return(frame, return_value);
@@ -813,7 +777,7 @@ ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *exec
        psi_call_frame_do_free(frame);
        psi_call_frame_free(frame);
 
        psi_call_frame_do_free(frame);
        psi_call_frame_free(frame);
 
-       return SUCCESS;
+       return true;
 }
 
 
 }
 
 
index 62d2a12269959a2ae149c4693c1c24d1d1ba5ce5..e2ee35d174a78b5fec35312bda5ff59a0a2123e2 100644 (file)
@@ -26,6 +26,7 @@
 #ifndef PSI_CONTEXT_H
 #define PSI_CONTEXT_H
 
 #ifndef PSI_CONTEXT_H
 #define PSI_CONTEXT_H
 
+#include <stdbool.h>
 #include "token.h"
 
 /* zend_function_entry */
 #include "token.h"
 
 /* zend_function_entry */
@@ -99,7 +100,7 @@ void **psi_context_composite_type_elements(struct psi_context *C,
 void *psi_context_decl_arg_call_type(struct psi_context *C, struct psi_decl_arg *arg);
 void *psi_context_decl_arg_full_type(struct psi_context *C, struct psi_decl_arg *arg);
 
 void *psi_context_decl_arg_call_type(struct psi_context *C, struct psi_decl_arg *arg);
 void *psi_context_decl_arg_full_type(struct psi_context *C, struct psi_decl_arg *arg);
 
-ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *execute_data, zval *return_value, struct psi_impl *impl);
+bool psi_context_call(struct psi_context *C, zend_execute_data *execute_data, zval *return_value, struct psi_impl *impl);
 void psi_context_dump(struct psi_dump *dump, struct psi_context *C);
 void psi_context_dtor(struct psi_context *C);
 void psi_context_free(struct psi_context **C);
 void psi_context_dump(struct psi_dump *dump, struct psi_context *C);
 void psi_context_dtor(struct psi_context *C);
 void psi_context_free(struct psi_context **C);
index 6ce20a76eb08dcd4a144a361c2a6225399e94cfd..2898ff1e332ee8732dfcc26bf3bdba08eafbd21d 100644 (file)
@@ -40,7 +40,7 @@
 #if PSI_THREADED_PARSER
 # include <pthread.h>
 
 #if PSI_THREADED_PARSER
 # include <pthread.h>
 
-pthread_mutex_t psi_string_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t psi_string_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 zend_string *psi_string_init_interned(const char *buf, size_t len, int p)
 {
 
 zend_string *psi_string_init_interned(const char *buf, size_t len, int p)
 {
index 91a47d8fe9390738030a71976b998ecffe60d3af..9781ed161cbfdb311fe6ffdd99c1e95f3a058951 100644 (file)
@@ -146,41 +146,45 @@ bool psi_parser_process(struct psi_parser *P, struct psi_plist *tokens,
        return true;
 }
 
        return true;
 }
 
+#if PSI_THREADED_PARSER
+static void psi_smart_str_printf(smart_str *ss, const char *fmt, ...)
+{
+       va_list argv;
+       char *buf;
+       int len;
+
+       va_start(argv, fmt);
+       len = vasprintf(&buf, fmt, argv);
+       va_end(argv);
+
+       if (len != -1) {
+               smart_str_appendl_ex(ss, buf, len, 1);
+               free(buf);
+       }
+}
+#else
+# define psi_smart_str_printf smart_str_append_printf
+#endif
+
 static inline zend_string *macro_to_constant(struct psi_parser *parser,
                zend_string *name, struct psi_validate_scope *scope)
 {
        smart_str str = {0};
 static inline zend_string *macro_to_constant(struct psi_parser *parser,
                zend_string *name, struct psi_validate_scope *scope)
 {
        smart_str str = {0};
-
        size_t i = 0;
        struct psi_token *tok;
        size_t i = 0;
        struct psi_token *tok;
-
-#if HAVE_ASPRINTF
+#if PSI_THREADED_PARSER
        int persistent = 1;
        int persistent = 1;
-
-       smart_str_appendl_ex(&str, ZEND_STRL("const psi\\"), 1);
-       smart_str_append_ex(&str, name, 1);
-       smart_str_appendl_ex(&str, ZEND_STRL(" = "), 1);
 #else
        int persistent = 0;
 #else
        int persistent = 0;
-
-       smart_str_append_printf(&str, "const psi\\%s = ", name->val);
 #endif
 #endif
+
+       psi_smart_str_printf(&str, "const psi\\%s = ", name->val);
        if (scope->macro->exp) {
        if (scope->macro->exp) {
-#if HAVE_ASPRINTF
-               char *astr = NULL;
-               struct psi_dump dump = {{.hn = &astr},
-                               .fun = (psi_dump_cb) asprintf};
-#else
                struct psi_dump dump = {{.hn = &str},
                struct psi_dump dump = {{.hn = &str},
-                               .fun = (psi_dump_cb) smart_str_append_printf};
-#endif
+                               .fun = (psi_dump_cb) psi_smart_str_printf};
 
                psi_num_exp_dump(&dump, scope->macro->exp);
 
 
                psi_num_exp_dump(&dump, scope->macro->exp);
 
-#if HAVE_ASPRINTF
-               smart_str_appends_ex(&str, astr, 1);
-               free(astr);
-#endif
        } else while (psi_plist_get(scope->macro->tokens, i++, &tok)) {
                if (tok->type == PSI_T_QUOTED_STRING) {
                        smart_str_appendc_ex(&str, '"', persistent);
        } else while (psi_plist_get(scope->macro->tokens, i++, &tok)) {
                if (tok->type == PSI_T_QUOTED_STRING) {
                        smart_str_appendc_ex(&str, '"', persistent);