|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23162 user_error() crashs if $error_msg > 1024 bytes
Submitted: 2003-04-11 04:17 UTC Modified: 2003-05-04 13:22 UTC
From: dimon at postmark dot net Assigned: helly (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 4.3.2-RC OS: Windows 2000 Server (only!)
Private report: No CVE-ID: None
 [2003-04-11 04:17 UTC] dimon at postmark dot net
function qq(&$type, &$details, &$file, &$line, $context){
	echo "$details";
user_error(str_repeat('q', 1025), E_USER_WARNING);
// will cause endless loop

set_error_handler(create_function('$type, &$details, $file, $line, $context', 'echo "$details";'));
user_error(str_repeat('q', 1025), E_USER_WARNING);
// will cause crash

// btw $context passed by reference will cause crash also
set_error_handler(create_function('$type, &$details, $file, $line, &$context', 'echo "$details";'));
user_error(str_repeat('q', 1024), E_USER_WARNING);


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-14 03:47 UTC] dimon at postmark dot net
In Version 4.3.2-RC Apr 14 2003 02:12:05 
bug still exists.
 [2003-04-14 15:27 UTC]
I have no idea if this hurts anything (hasn't given me any 
trouble), but adding "z_context->is_ref = 1;" to zend.c 
before calling call_user_function_ex() in zend_error() 
seems to fix the "$context passed by reference" segfault. 
Not being much of a ZE engine hacker, I don't know if that 
makes things better or worse. 
 [2003-05-02 06:30 UTC]
Does not crash under Linux.

 [2003-05-02 16:10 UTC]
Segfault could happen on platforms where vsnprintf() is missing.

 [2003-05-02 16:57 UTC]
If vsnprintf is the cause then it should be easy to expand the <whatever>printf broken tests what would result in using the internal one.
 [2003-05-02 17:19 UTC]
(where valid vsnprintf() implementation is missing.)

case (a) and case (b): MS libc's vsnprintf() returns -1 if the resulting string has exceeded the limit length specified in the second parameter. This may cause segfaults in some cases like those.

case (c): jay's suggestion looks like a valid fix to me since that's essentially a referenced variable.

Here's the patch. Could anyone with ZE karma apply this one please?

Index: Zend/zend.c
RCS file: /repository/Zend/zend.c,v
retrieving revision
diff -u -r1.162.2.2 zend.c
--- Zend/zend.c	31 Dec 2002 16:22:56 -0000
+++ Zend/zend.c	2 May 2003 22:17:45 -0000
@@ -754,11 +754,14 @@
 			z_error_message->value.str.len = vsnprintf(z_error_message->value.str.val, ZEND_ERROR_BUFFER_SIZE, format, args);
-			if (z_error_message->value.str.len > ZEND_ERROR_BUFFER_SIZE-1) {
+			if (z_error_message->value.str.len < 0 || z_error_message->value.str.len > ZEND_ERROR_BUFFER_SIZE-1) {
+				z_error_message->value.str.val[ZEND_ERROR_BUFFER_SIZE-1] = '\0';
 				z_error_message->value.str.len = ZEND_ERROR_BUFFER_SIZE-1;
-			strncpy(z_error_message->value.str.val, format, ZEND_ERROR_BUFFER_SIZE);
+			strncpy(z_error_message->value.str.val, va_arg(format, char *), ZEND_ERROR_BUFFER_SIZE);
+			z_error_message->value.str.val[ZEND_ERROR_BUFFER_SIZE - 1] = '\0';
+			z_error_message->value.str.len = strlen(z_error_message->value.str.val);
 			/* This is risky... */
 			/* z_error_message->value.str.len = vsprintf(z_error_message->value.str.val, format, args); */
@@ -778,7 +781,8 @@
 			z_context-> = EG(active_symbol_table);
 			z_context->type = IS_ARRAY;
-			ZVAL_ADDREF(z_context);  /* we don't want this one to be freed */
+			z_context->is_ref = 1;
+			z_context->refcount = 2; /* we don't want this one to be freed */
 			params = (zval ***) emalloc(sizeof(zval **)*5);
 			params[0] = &z_error_type;

 [2003-05-02 17:38 UTC]
[v]snprintf of MSVC is not C99 compliant which we rely on so the patch is irrelevant and we must use our implementation fo windows.
 [2003-05-04 13:22 UTC]
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at
In case this was a documentation problem, the fix will show up soon at

In case this was a website problem, the change will show
up on the site and on the mirror sites in short time.
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Sep 22 19:01:25 2023 UTC