]> granicus.if.org Git - php/commitdiff
- Fixed Bug #42272 (var_export() incorrectly escapes char(0)).
authorDerick Rethans <derick@php.net>
Sun, 9 Dec 2007 16:54:30 +0000 (16:54 +0000)
committerDerick Rethans <derick@php.net>
Sun, 9 Dec 2007 16:54:30 +0000 (16:54 +0000)
- Also fixed var_export() in unicode mode, as the function would actually
  generate non-parsable strings which defeats the purpose of var_export().

ext/standard/tests/general_functions/bug42272.phpt [new file with mode: 0644]
ext/standard/tests/general_functions/var_export-locale.phpt
ext/standard/tests/general_functions/var_export.phpt
ext/standard/tests/strings/bug37262.phpt
ext/standard/var.c

diff --git a/ext/standard/tests/general_functions/bug42272.phpt b/ext/standard/tests/general_functions/bug42272.phpt
new file mode 100644 (file)
index 0000000..5a455d7
--- /dev/null
@@ -0,0 +1,11 @@
+--TEST--
+Bug #42272: var_export() incorrectly escapes char(0).
+--FILE--
+<?php
+$foo = var_export("\0", true );
+echo $foo, "\n";
+var_export("a\0b");
+?>
+--EXPECT--
+'' . "\0" . ''
+'a' . "\0" . 'b'
index 6d2df85424f5dde20dadeb1749e2561e6eb7aa74..81896550bceb57a31c22aff9af265c9b7c2b3742 100644 (file)
@@ -572,9 +572,9 @@ string(3) "'\v'"
 
 
 Iteration 12
-'\000'
-'\000'
-string(6) "'\000'"
+'' . "\0" . ''
+'' . "\0" . ''
+string(14) "'' . "\0" . ''"
 
 
 Iteration 13
index add24bc4ae480b5f12d21fa25380954461cf1a4b..f87ffeaee4d694e2de7aba0cc7b5c2316f284355 100644 (file)
@@ -124,7 +124,8 @@ $valid_strings = array(
             "\0",
             '\0',
             '\060',
-            "\070"
+            "\070",
+            "\0hello\0this is an test, to work with ' and \0 and \n and foreign chars too: blåbærøl"
           );
 $counter = 1;
 /* Loop to check for above strings with var_export() */
@@ -568,9 +569,9 @@ string(3) "'\v'"
 
 
 Iteration 12
-'\000'
-'\000'
-string(6) "'\000'"
+'' . "\0" . ''
+'' . "\0" . ''
+string(14) "'' . "\0" . ''"
 
 
 Iteration 13
@@ -590,6 +591,15 @@ Iteration 15
 '8'
 string(3) "'8'"
 
+
+Iteration 16
+'' . "\0" . 'hello' . "\0" . 'this is an test, to work with \' and ' . "\0" . ' and 
+ and foreign chars too: blåbærøl'
+'' . "\0" . 'hello' . "\0" . 'this is an test, to work with \' and ' . "\0" . ' and 
+ and foreign chars too: blåbærøl'
+string(121) "'' . "\0" . 'hello' . "\0" . 'this is an test, to work with \' and ' . "\0" . ' and 
+ and foreign chars too: blåbærøl'"
+
 *** Testing var_export() with valid arrays ***
 
 *** Output for arrays ***
@@ -1320,9 +1330,9 @@ string(3) "'\v'"
 
 
 Iteration 12
-'\000'
-'\000'
-string(6) "'\000'"
+'' . "\0" . ''
+'' . "\0" . ''
+string(14) "'' . "\0" . ''"
 
 
 Iteration 13
@@ -1342,6 +1352,15 @@ Iteration 15
 '8'
 string(3) "'8'"
 
+
+Iteration 16
+'' . "\0" . 'hello' . "\0" . 'this is an test, to work with \' and ' . "\0" . ' and 
+ and foreign chars too: bl' . "\u00E5" . 'b' . "\u00E6" . 'r' . "\u00F8" . 'l'
+'' . "\0" . 'hello' . "\0" . 'this is an test, to work with \' and ' . "\0" . ' and 
+ and foreign chars too: bl' . "\u00E5" . 'b' . "\u00E6" . 'r' . "\u00F8" . 'l'
+string(163) "'' . "\0" . 'hello' . "\0" . 'this is an test, to work with \' and ' . "\0" . ' and 
+ and foreign chars too: bl' . "\u00E5" . 'b' . "\u00E6" . 'r' . "\u00F8" . 'l'"
+
 *** Testing var_export() with valid arrays ***
 
 *** Output for arrays ***
index 474251a816640b121119a1499d75bc3528757029..6fe2d9f379d9c80552ed6a71797f65636cfec9af 100644 (file)
@@ -6,4 +6,4 @@ $func = create_function('$a', 'return $a;');
 var_export($func);
 ?>
 --EXPECT--     
-'\000lambda_1'
+'' . "\0" . 'lambda_1'
index 2d513fcdc85c84c3d7fedabfc35edeee3d57765a..353edef33d4656a54f2b5039768b921eb50ff364 100644 (file)
@@ -520,6 +520,7 @@ static void php_unicode_export(UChar *ustr, int ustr_len TSRMLS_DC) /* {{{ */
        int i = 0;
        char buf[10];
        int buf_len;
+       int state = 0; /* 0 = in single quotes, 1 = in double quotes */
 
        /*
         * We export all codepoints > 128 in escaped form to avoid encoding issues
@@ -529,10 +530,18 @@ static void php_unicode_export(UChar *ustr, int ustr_len TSRMLS_DC) /* {{{ */
                U16_NEXT(ustr, i, ustr_len, cp);
                switch (cp) {
                        case 0x0: /* '\0' */
-                               PHPWRITE("\\000", 4);
+                               if (state == 0) {
+                                       PHPWRITE("' . \"", 5);
+                                       state = 1;
+                               }
+                               PHPWRITE("\\0", 2);
                                break;
 
                        case 0x27: /* '\'' */
+                               if (state == 1) {
+                                       PHPWRITE("\" . '", 5);
+                                       state = 0;
+                               }
                                PHPWRITE("\\'", 2);
                                break;
 
@@ -542,25 +551,40 @@ static void php_unicode_export(UChar *ustr, int ustr_len TSRMLS_DC) /* {{{ */
 
                        default:
                                if ((uint32_t)cp < 128) {
+                                       if (state == 1) {
+                                               PHPWRITE("\" . '", 5);
+                                               state = 0;
+                                       }
                                        buf[0] = (char) (short) cp;
                                        buf_len = 1;
                                } else if (U_IS_BMP(cp)) {
+                                       if (state == 0) {
+                                               PHPWRITE("' . \"", 5);
+                                               state = 1;
+                                       }
                                        buf_len = snprintf(buf, sizeof(buf), "\\u%04X", cp);
                                } else {
+                                       if (state == 0) {
+                                               PHPWRITE("' . \"", 5);
+                                               state = 1;
+                                       }
                                        buf_len = snprintf(buf, sizeof(buf), "\\u%06X", cp);
                                }
                                PHPWRITE(buf, buf_len);
                                break;
                }
        }
+       if (state == 1) { // if we are in double quotes, go back to single */
+               PHPWRITE("\" . '", 5);
+       }
 }
 /* }}} */
 
 PHPAPI void php_var_export(zval **struc, int level TSRMLS_DC) /* {{{ */
 {
        HashTable *myht;
-       char* tmp_str;
-       int tmp_len;
+       char *tmp_str, *tmp_str2;
+       int tmp_len, tmp_len2;
        zstr class_name;
        zend_uint class_name_len;
 
@@ -578,11 +602,13 @@ PHPAPI void php_var_export(zval **struc, int level TSRMLS_DC) /* {{{ */
                php_printf("%.*H", (int) EG(precision), Z_DVAL_PP(struc));
                break;
        case IS_STRING:
-               tmp_str = php_addcslashes(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc), &tmp_len, 0, "'\\\0", 3 TSRMLS_CC);
+               tmp_str = php_addcslashes(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc), &tmp_len, 0, "'\\", 2 TSRMLS_CC);
+               tmp_str2 = php_str_to_str_ex(tmp_str, tmp_len, "\0", 1, "' . \"\\0\" . '", 12, &tmp_len2, 0, NULL);
                PUTS ("'");
-               PHPWRITE(tmp_str, tmp_len);
+               PHPWRITE(tmp_str2, tmp_len2);
                PUTS ("'");
-               efree (tmp_str);
+               efree(tmp_str2);
+               efree(tmp_str);
                break;
        case IS_UNICODE:
                PUTS ("'");