Re: [ZEND-ENGINE-CVS] cvs: Zend / zend.c zend_execute_API.c zend_globals.h
| From: | Ilia A. | Date: | Thu, 10 Oct 2002 23:58:26 +0000 |
| Subject: | Re: [ZEND-ENGINE-CVS] cvs: Zend / zend.c zend_execute_API.c zend_globals.h | ||
| References: | 1 | Groups: | php.zend-engine.cvs |
| Request: | Send a blank email to php-zend-engine-cvs+get-500@lists.php.net to get a copy of this message | ||
On October 10, 2002 06:43 pm, Andi Gutmans wrote:
> Isn't there a way to make this nicer? Isn't there an sprintf() method which
> honors locale? convert_to_string() uses php_sprintf(). Then we wouldn't
> need this separator.
You may be seeing the problem from a slightly different angle then I am, which
may be the cause of some confusion or my explanation is really poor. Allow me
to clarify the problem, hopefully better then I did before.
When a user in PHP setlocale(LC_NUMERIC, "german"); this causes PHP to set the
enviroment locale to german. The part of the patch you do not see here (php
source code), which is inside setlocale, records this change and if
decimal_point inside struct lconv does not equal '.' it records decimal_point
inside EG(float_separator). After recording this it resets the LC_NUMERIC
locale back to C so that Zend Engine as well as PHP can continue using
floats/doubles without worrying about locales.
Otherwise, since the system locale has changed floats should now always be
treated with locale in mind. This causes all kinds of problems, such as:
<?php
error_reporting(E_ALL);
setlocale(LC_ALL, "german");
$a = 2.3;
$b = serialize($a);
var_dump(serialize($a));
var_dump(unserialize($b));
?>
Prior to the patch this results in
Notice: unserialize() failed at offset 0 of 6 bytes in /home/ilia/l2.php on
line 7
bool(false)
The problem occurs because we honour locale and consequently create data we
cannot properly parse and thus cause the problem. zend_sprintf() which is
used by _convert_to_string uses vsprintf(buffer, format, args); which
actually observes locale consequently causes us problems.
IMHO the fact the patch only comes into play when we are printing floats &
decimal point is not '.' makes this the fastest approach and the most
harmless ones (performance wise) to people who use C/POSIX/English locale.
Ilia
> Andi
>
> At 04:27 PM 10/10/2002 +0000, Ilia Alshanetsky wrote:
> >iliaa Thu Oct 10 12:27:52 2002 EDT
> >
> > Modified files:
> > /Zend zend.c zend_execute_API.c zend_globals.h
> > Log:
> > This patch fixes handling of floats on locales where decimal point is
> > not a '.'. Problem is best demonstrated by bug #17079.
> >
> >
> >Index: Zend/zend.c
> >diff -u Zend/zend.c:1.157 Zend/zend.c:1.158
> >--- Zend/zend.c:1.157 Tue Sep 24 06:58:33 2002
> >+++ Zend/zend.c Thu Oct 10 12:27:51 2002
> >@@ -154,6 +154,19 @@
> > expr_copy->value.str.len = sizeof("Object")-1;
> > expr_copy->value.str.val = estrndup("Object",
> > expr_copy->value.str.len);
> > break;
> >+ case IS_DOUBLE:
> >+ *expr_copy = *expr;
> >+ zval_copy_ctor(expr_copy);
> >+ convert_to_string(expr_copy);
> >+
> >+ /* locale hanling */
> >+ if (EG(float_separator)[0] != '.') {
> >+ char *p = expr_copy->value.str.val;
> >+ if ((p = strchr(p, '.'))) {
> >+ *p = EG(float_separator)[0];
> >+ }
> >+ }
> >+ break;
> > default:
> > *expr_copy = *expr;
> > zval_copy_ctor(expr_copy);
> >Index: Zend/zend_execute_API.c
> >diff -u Zend/zend_execute_API.c:1.154 Zend/zend_execute_API.c:1.155
> >--- Zend/zend_execute_API.c:1.154 Wed Oct 9 10:17:53 2002
> >+++ Zend/zend_execute_API.c Thu Oct 10 12:27:52 2002
> >@@ -166,6 +166,8 @@
> > #ifdef ZEND_WIN32
> > EG(timed_out) = 0;
> > #endif
> >+
> >+ EG(float_separator)[0] = '.';
> > }
> >
> >
> >Index: Zend/zend_globals.h
> >diff -u Zend/zend_globals.h:1.91 Zend/zend_globals.h:1.92
> >--- Zend/zend_globals.h:1.91 Mon Sep 23 08:00:38 2002
> >+++ Zend/zend_globals.h Thu Oct 10 12:27:52 2002
> >@@ -207,6 +207,9 @@
> >
> > HashTable *ini_directives;
> >
> >+ /* locale stuff */
> >+ char float_separator[1];
> >+
> > void *reserved[ZEND_MAX_RESERVED_RESOURCES];
> > };
> >
> >
> >
> >
> >--
> >Zend Engine CVS Mailing List (http://cvs.php.net/)
> >To unsubscribe, visit: http://www.php.net/unsub.php