Skip to content
Browse files

- Provide a unified way to display uncaught exceptions, which shows

  file/line/message info if possible.
- Add zend_eval_string_ex() to be able to handle exceptions in eval'd code.
- Use above function to fix memleaks in CLI.
  • Loading branch information...
1 parent 669603f commit d3473d1f58c3801805ceac5c6c7df839d12c2a04 Marcus Boerger committed Aug 24, 2003
View
6 Zend/zend.c
@@ -1052,9 +1052,9 @@ ZEND_API int zend_execute_scripts(int type TSRMLS_DC, zval **retval, int file_co
zval_ptr_dtor(&retval2);
}
} else {
+ zend_exception_error(EG(exception) TSRMLS_CC);
zval_ptr_dtor(&EG(exception));
EG(exception) = NULL;
- zend_error(E_ERROR, "Uncaught exception '%s'!", ex_class_name);
}
efree(params);
zval_ptr_dtor(&old_exception);
@@ -1063,17 +1063,17 @@ ZEND_API int zend_execute_scripts(int type TSRMLS_DC, zval **retval, int file_co
EG(exception) = NULL;
}
} else {
+ zend_exception_error(EG(exception) TSRMLS_CC);
zval_ptr_dtor(&EG(exception));
EG(exception) = NULL;
- zend_error(E_ERROR, "Uncaught exception '%s'!", ex_class_name);
}
if (retval == NULL && *EG(return_value_ptr_ptr) != NULL) {
zval_ptr_dtor(EG(return_value_ptr_ptr));
local_retval = NULL;
}
#else
+ zend_exception_error(EG(exception) TSRMLS_CC);
zval_ptr_dtor(&EG(exception));
- zend_error(E_ERROR, "Uncaught exception '%s'!", ex_class_name);
#endif
} else if (!retval) {
zval_ptr_dtor(EG(return_value_ptr_ptr));
View
21 Zend/zend_default_classes.c
@@ -153,6 +153,27 @@ ZEND_API void zend_throw_exception(char *message, long code TSRMLS_DC)
EG(exception) = ex;
}
+static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ zend_error_cb(E_ERROR, file, lineno, format, args);
+ va_end(args);
+}
+
+ZEND_API void zend_exception_error(zval *exception TSRMLS_DC)
+{
+ if (instanceof_function(Z_OBJCE_P(exception), default_exception_ptr TSRMLS_CC)) {
+ zval *message = zend_read_property(default_exception_ptr, exception, "message", sizeof("message")-1, 1 TSRMLS_CC);
+ zval *file = zend_read_property(default_exception_ptr, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
+ zval *lineno = zend_read_property(default_exception_ptr, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
+ zend_error_va(E_ERROR, Z_STRVAL_P(file), Z_LVAL_P(lineno), "Uncaught exception '%s' with message '%s'", Z_OBJCE_P(exception)->name, Z_STRVAL_P(message));
+ } else {
+ zend_error(E_ERROR, "Uncaught exception '%s'!", Z_OBJCE_P(exception)->name);
+ }
+}
+
ZEND_API void zend_register_default_classes(TSRMLS_D)
{
zend_register_default_exception(TSRMLS_C);
View
1 Zend/zend_default_classes.h
@@ -26,6 +26,7 @@ BEGIN_EXTERN_C()
ZEND_API zend_class_entry *zend_exception_get_default(void);
ZEND_API void zend_throw_exception(char *message, long code TSRMLS_DC);
ZEND_API void zend_register_default_classes(TSRMLS_D);
+ZEND_API void zend_exception_error(zval *exception TSRMLS_DC);
END_EXTERN_C()
View
21 Zend/zend_exceptions.c
@@ -153,6 +153,27 @@ ZEND_API void zend_throw_exception(char *message, long code TSRMLS_DC)
EG(exception) = ex;
}
+static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ zend_error_cb(E_ERROR, file, lineno, format, args);
+ va_end(args);
+}
+
+ZEND_API void zend_exception_error(zval *exception TSRMLS_DC)
+{
+ if (instanceof_function(Z_OBJCE_P(exception), default_exception_ptr TSRMLS_CC)) {
+ zval *message = zend_read_property(default_exception_ptr, exception, "message", sizeof("message")-1, 1 TSRMLS_CC);
+ zval *file = zend_read_property(default_exception_ptr, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
+ zval *lineno = zend_read_property(default_exception_ptr, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
+ zend_error_va(E_ERROR, Z_STRVAL_P(file), Z_LVAL_P(lineno), "Uncaught exception '%s' with message '%s'", Z_OBJCE_P(exception)->name, Z_STRVAL_P(message));
+ } else {
+ zend_error(E_ERROR, "Uncaught exception '%s'!", Z_OBJCE_P(exception)->name);
+ }
+}
+
ZEND_API void zend_register_default_classes(TSRMLS_D)
{
zend_register_default_exception(TSRMLS_C);
View
1 Zend/zend_exceptions.h
@@ -26,6 +26,7 @@ BEGIN_EXTERN_C()
ZEND_API zend_class_entry *zend_exception_get_default(void);
ZEND_API void zend_throw_exception(char *message, long code TSRMLS_DC);
ZEND_API void zend_register_default_classes(TSRMLS_D);
+ZEND_API void zend_exception_error(zval *exception TSRMLS_DC);
END_EXTERN_C()
View
1 Zend/zend_execute.h
@@ -69,6 +69,7 @@ static inline void safe_free_zval_ptr(zval *p)
}
ZEND_API int zend_lookup_class(char *name, int name_length, zend_class_entry ***ce TSRMLS_DC);
ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC);
+ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
static inline int i_zend_is_true(zval *op)
{
View
21 Zend/zend_execute_API.c
@@ -29,6 +29,7 @@
#include "zend_ptr_stack.h"
#include "zend_constants.h"
#include "zend_extensions.h"
+#include "zend_default_classes.h"
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -860,6 +861,26 @@ ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSR
}
+ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC)
+{
+ int result;
+ zval *exception = EG(exception);
+
+
+ if (handle_exceptions) {
+ EG(exception) = NULL;
+ }
+ result = zend_eval_string(str, retval_ptr, string_name TSRMLS_CC);
+ if (handle_exceptions && EG(exception)) {
+ zend_exception_error(EG(exception) TSRMLS_CC);
+ zval_ptr_dtor(&EG(exception));
+ EG(exception) = exception;
+ result = FAILURE;
+ }
+ return result;
+}
+
+
void execute_new_code(TSRMLS_D)
{
zend_op *opline, *end;
View
8 sapi/cli/php_cli.c
@@ -945,7 +945,7 @@ int main(int argc, char *argv[])
#endif
case PHP_MODE_CLI_DIRECT:
cli_register_file_handles(TSRMLS_C);
- if (zend_eval_string(exec_direct, NULL, "Command line code" TSRMLS_CC) == FAILURE) {
+ if (zend_eval_string_ex(exec_direct, NULL, "Command line code", 1 TSRMLS_CC) == FAILURE) {
exit_status=254;
}
break;
@@ -958,7 +958,7 @@ int main(int argc, char *argv[])
cli_register_file_handles(TSRMLS_C);
- if (exec_begin && zend_eval_string(exec_begin, NULL, "Command line begin code" TSRMLS_CC) == FAILURE) {
+ if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1 TSRMLS_CC) == FAILURE) {
exit_status=254;
}
ALLOC_ZVAL(argi);
@@ -979,7 +979,7 @@ int main(int argc, char *argv[])
zend_hash_update(&EG(symbol_table), "argn", sizeof("argn"), &argn, sizeof(pval *), NULL);
Z_LVAL_P(argi) = ++index;
if (exec_run) {
- if (zend_eval_string(exec_run, NULL, "Command line run code" TSRMLS_CC) == FAILURE) {
+ if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1 TSRMLS_CC) == FAILURE) {
exit_status=254;
}
} else {
@@ -995,7 +995,7 @@ int main(int argc, char *argv[])
}
efree(input);
}
- if (exec_end && zend_eval_string(exec_end, NULL, "Command line end code" TSRMLS_CC) == FAILURE) {
+ if (exec_end && zend_eval_string_ex(exec_end, NULL, "Command line end code", 1 TSRMLS_CC) == FAILURE) {
exit_status=254;
}

0 comments on commit d3473d1

Please sign in to comment.
Something went wrong with that request. Please try again.