From ca635e488484838db2a4ab11e407a58271fbe917 Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Mon, 22 Sep 2014 00:52:22 +0200 Subject: [PATCH] A few general segfault fixes --- phpdbg.c | 32 ++++++++------- phpdbg_cmd.c | 3 ++ phpdbg_prompt.c | 7 +++- phpdbg_utils.c | 102 ++++++++++++++++++++++++++---------------------- 4 files changed, 81 insertions(+), 63 deletions(-) diff --git a/phpdbg.c b/phpdbg.c index b27e774bdd..ab2f8256ee 100644 --- a/phpdbg.c +++ b/phpdbg.c @@ -539,11 +539,11 @@ static size_t phpdbg_stdiop_write(php_stream *stream, const char *buf, size_t co } if (stat[0].st_dev == stat[1].st_dev && stat[0].st_ino == stat[1].st_ino) { - phpdbg_script(P_STDOUT, "%.*s", buf, count); + phpdbg_script(P_STDOUT, "%.*s", (int) count, buf); return count; } if (stat[2].st_dev == stat[1].st_dev && stat[2].st_ino == stat[1].st_ino) { - phpdbg_script(P_STDERR, "%.*s", buf, count); + phpdbg_script(P_STDERR, "%.*s", (int) count, buf); return count; } break; @@ -1244,10 +1244,15 @@ phpdbg_main: sigaction(SIGBUS, &signal_struct, &PHPDBG_G(old_sigsegv_signal)); #endif + php_output_activate(TSRMLS_C); + php_output_deactivate(TSRMLS_C); + + php_output_activate(TSRMLS_C); + if (php_request_startup(TSRMLS_C) == SUCCESS) { int i; - - SG(request_info).argc = argc - php_optind + 1; + + SG(request_info).argc = argc - php_optind + 1; SG(request_info).argv = emalloc(SG(request_info).argc * sizeof(char *)); for (i = SG(request_info).argc; --i;) { SG(request_info).argv[i] = estrdup(argv[php_optind - 1 + i]); @@ -1257,10 +1262,6 @@ phpdbg_main: php_hash_environment(TSRMLS_C); } - /* make sure to turn off buffer for ev command */ - php_output_activate(TSRMLS_C); - php_output_deactivate(TSRMLS_C); - /* do not install sigint handlers for remote consoles */ /* sending SIGINT then provides a decent way of shutting down the server */ #ifndef _WIN32 @@ -1425,7 +1426,7 @@ phpdbg_out: } phpdbg_out: #endif - + { int i; /* free argv */ @@ -1438,8 +1439,7 @@ phpdbg_out: #ifndef ZTS /* force cleanup of auto and core globals */ zend_hash_clean(CG(auto_globals)); - memset( - &core_globals, 0, sizeof(php_core_globals)); + memset( &core_globals, 0, sizeof(php_core_globals)); #endif if (ini_entries) { free(ini_entries); @@ -1448,15 +1448,17 @@ phpdbg_out: if (ini_override) { free(ini_override); } - + /* this must be forced */ CG(unclean_shutdown) = 0; - + /* this is just helpful */ PG(report_memleaks) = 0; php_request_shutdown((void*)0); + php_output_deactivate(TSRMLS_C); + zend_try { php_module_shutdown(TSRMLS_C); } zend_end_try(); @@ -1468,7 +1470,7 @@ phpdbg_out: if (cleaning || remote) { goto phpdbg_main; } - + #ifdef ZTS /* bugggy */ /* tsrm_shutdown(); */ @@ -1483,7 +1485,7 @@ phpdbg_out: if (sapi_name) { free(sapi_name); } - + #ifdef _WIN32 free(bp_tmp_file); #else diff --git a/phpdbg_cmd.c b/phpdbg_cmd.c index 587007e900..5c6ac072ea 100644 --- a/phpdbg_cmd.c +++ b/phpdbg_cmd.c @@ -763,6 +763,9 @@ PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack TSRMLS_DC) { if (handler) { if (phpdbg_stack_verify(handler, &top TSRMLS_CC) == SUCCESS) { + phpdbg_activate_err_buf(0 TSRMLS_CC); + phpdbg_free_err_buf(TSRMLS_C); + return handler->handler(top TSRMLS_CC); } } diff --git a/phpdbg_prompt.c b/phpdbg_prompt.c index 0081fd2312..b877f3511c 100644 --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@ -161,6 +161,9 @@ static inline int phpdbg_call_register(phpdbg_param_t *stack TSRMLS_DC) /* {{{ * fci.param_count = 0; } + phpdbg_activate_err_buf(0 TSRMLS_CC); + phpdbg_free_err_buf(TSRMLS_C); + phpdbg_debug("created %d params from arguments", fci.param_count); zend_call_function(&fci, NULL TSRMLS_CC); @@ -603,11 +606,11 @@ PHPDBG_COMMAND(run) /* {{{ */ } zend_try { - php_output_activate(TSRMLS_C); +// php_output_activate(TSRMLS_C); PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE; zend_execute(EG(active_op_array) TSRMLS_CC); PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE; - php_output_deactivate(TSRMLS_C); +// php_output_deactivate(TSRMLS_C); } zend_catch { EG(active_op_array) = orig_op_array; EG(opline_ptr) = orig_opline; diff --git a/phpdbg_utils.c b/phpdbg_utils.c index 25c2199649..b68a0b6d2d 100644 --- a/phpdbg_utils.c +++ b/phpdbg_utils.c @@ -734,7 +734,7 @@ static int format_converter(register buffy *odp, const char *fmt, zend_bool esca } *s_ptr++ = ';'; } else { - *s_ptr++ = s[i]; + *s_ptr++ = old_s[i]; } } while (i++ < old_slen); } @@ -1082,7 +1082,8 @@ static int phpdbg_process_print(FILE *fp, int type, const char *tag, const char if (msg) { msgoutlen = asprintf(&msgout, "%.*s\n", msglen, msg); } else { - msgoutlen = asprintf(&msgout, "\n"); + msgoutlen = 1; + msgout = strdup("\n"); } break; @@ -1091,7 +1092,8 @@ static int phpdbg_process_print(FILE *fp, int type, const char *tag, const char if (msg) { msgoutlen = asprintf(&msgout, "%.*s\n", msglen, msg); } else { - msgoutlen = asprintf(&msgout, ""); + msgoutlen = 0; + msgout = strdup(""); } break; @@ -1103,7 +1105,7 @@ static int phpdbg_process_print(FILE *fp, int type, const char *tag, const char fprintf(fp, "", type == P_STDERR ? "stderr" : "stdout"); PHPDBG_G(in_script_xml) = type; } - buf = php_escape_html_entities((char *) msg, msglen, (size_t *) &buflen, 0, ENT_NOQUOTES, PG(internal_encoding) && PG(internal_encoding)[0] ? PG(internal_encoding) : (SG(default_charset) ? SG(default_charset) : "UTF-8") TSRMLS_CC); + buf = php_escape_html_entities((unsigned char *) msg, msglen, (size_t *) &buflen, 0, ENT_NOQUOTES, PG(internal_encoding) && PG(internal_encoding)[0] ? PG(internal_encoding) : (SG(default_charset) ? SG(default_charset) : "UTF-8") TSRMLS_CC); fprintf(fp, "%.*s", buflen, buf); efree(buf); } else { @@ -1130,7 +1132,7 @@ static int phpdbg_process_print(FILE *fp, int type, const char *tag, const char if ((PHPDBG_G(flags) & PHPDBG_WRITE_XML)) { if (msgout) { - buf = php_escape_html_entities(msgout, msgoutlen, (size_t *) &buflen, 0, ENT_COMPAT, PG(internal_encoding) && PG(internal_encoding)[0] ? PG(internal_encoding) : (SG(default_charset) ? SG(default_charset) : "UTF-8") TSRMLS_CC); + buf = php_escape_html_entities((unsigned char *) msgout, msgoutlen, (size_t *) &buflen, 0, ENT_COMPAT, PG(internal_encoding) && PG(internal_encoding)[0] ? PG(internal_encoding) : (SG(default_charset) ? SG(default_charset) : "UTF-8") TSRMLS_CC); xmloutlen = fprintf(fp, "<%s severity=\"%s\" %.*s msgout=\"%.*s\" />", tag, severity, xmllen, xml, buflen, buf); efree(buf); @@ -1148,62 +1150,30 @@ static int phpdbg_process_print(FILE *fp, int type, const char *tag, const char return msgout ? msgoutlen : xmloutlen; } /* }}} */ -PHPDBG_API void phpdbg_free_err_buf(TSRMLS_D) { - if (PHPDBG_G(err_buf).type == 0) { - return; - } - - PHPDBG_G(err_buf).type = 0; - - efree(PHPDBG_G(err_buf).tag); - efree(PHPDBG_G(err_buf).msg); - efree(PHPDBG_G(err_buf).xml); -} - -PHPDBG_API void phpdbg_activate_err_buf(zend_bool active TSRMLS_DC) { - PHPDBG_G(err_buf).active = active; -} - -PHPDBG_API int phpdbg_output_err_buf(const char *tag, const char *xmlfmt, const char *strfmt TSRMLS_DC, ...) { - int len; - va_list args; - int errbuf_active = PHPDBG_G(err_buf).active; - - PHPDBG_G(err_buf).active = 0; - -#ifdef ZTS - va_start(args, tsrm_ls); -#else - va_start(args, strfmt); -#endif - len = phpdbg_vprint(PHPDBG_G(err_buf).type TSRMLS_CC, PHPDBG_G(err_buf).fp, tag ? tag : PHPDBG_G(err_buf).tag, xmlfmt, strfmt, args); - va_end(args); - - PHPDBG_G(err_buf).active = errbuf_active; - phpdbg_free_err_buf(TSRMLS_C); - - return len; -} - PHPDBG_API int phpdbg_vprint(int type TSRMLS_DC, FILE *fp, const char *tag, const char *xmlfmt, const char *strfmt, va_list args) { char *msg = NULL, *xml = NULL; int msglen = 0, xmllen = 0; int len; + va_list argcpy; if (strfmt != NULL && strlen(strfmt) > 0L) { - msglen = phpdbg_xml_vasprintf(&msg, strfmt, 0, args TSRMLS_CC); + va_copy(argcpy, args); + msglen = phpdbg_xml_vasprintf(&msg, strfmt, 0, argcpy TSRMLS_CC); + va_end(argcpy); } if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) { if (xmlfmt != NULL && strlen(xmlfmt) > 0L) { - xmllen = phpdbg_xml_vasprintf(&xml, xmlfmt, 1, args TSRMLS_CC); + va_copy(argcpy, args); + xmllen = phpdbg_xml_vasprintf(&xml, xmlfmt, 1, argcpy TSRMLS_CC); + va_end(argcpy); } } if (PHPDBG_G(err_buf).active && type != P_STDOUT && type != P_STDERR) { PHPDBG_G(err_buf).type = type; PHPDBG_G(err_buf).fp = fp; - PHPDBG_G(err_buf).tag = estrdup(tag); + PHPDBG_G(err_buf).tag = strdup(tag); PHPDBG_G(err_buf).msg = msg; PHPDBG_G(err_buf).msglen = msglen; PHPDBG_G(err_buf).xml = xml; @@ -1225,12 +1195,52 @@ PHPDBG_API int phpdbg_vprint(int type TSRMLS_DC, FILE *fp, const char *tag, cons return len; } +PHPDBG_API void phpdbg_free_err_buf(TSRMLS_D) { + if (PHPDBG_G(err_buf).type == 0) { + return; + } + + PHPDBG_G(err_buf).type = 0; + + free(PHPDBG_G(err_buf).tag); + free(PHPDBG_G(err_buf).msg); + free(PHPDBG_G(err_buf).xml); +} + +PHPDBG_API void phpdbg_activate_err_buf(zend_bool active TSRMLS_DC) { + PHPDBG_G(err_buf).active = active; +} + +PHPDBG_API int phpdbg_output_err_buf(const char *tag, const char *xmlfmt, const char *strfmt TSRMLS_DC, ...) { + int len; + va_list args; + int errbuf_active = PHPDBG_G(err_buf).active; + + PHPDBG_G(err_buf).active = 0; + +#ifdef ZTS + va_start(args, tsrm_ls); +#else + va_start(args, strfmt); +#endif + len = phpdbg_vprint(PHPDBG_G(err_buf).type TSRMLS_CC, PHPDBG_G(err_buf).fp, tag ? tag : PHPDBG_G(err_buf).tag, xmlfmt, strfmt, args); + va_end(args); + + PHPDBG_G(err_buf).active = errbuf_active; + phpdbg_free_err_buf(TSRMLS_C); + + return len; +} + PHPDBG_API int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *tag, const char *xmlfmt, const char *strfmt, ...) { va_list args; + int len; va_start(args, strfmt); - phpdbg_vprint(type TSRMLS_CC, fp, tag, xmlfmt, strfmt, args); + len = phpdbg_vprint(type TSRMLS_CC, fp, tag, xmlfmt, strfmt, args); va_end(args); + + return len; } PHPDBG_API int phpdbg_xml_internal(FILE *fp TSRMLS_DC, const char *fmt, ...) { -- 2.40.0