|
| 1 | +--TEST-- |
| 2 | +Test unserialize(): error is indistinguishable from deserialized boolean |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | +/* Prototype : proto string serialize(mixed variable) |
| 6 | + * Description: Returns a string representation of variable (which can later be unserialized) |
| 7 | + * Source code: ext/standard/var.c |
| 8 | + * Alias to functions: |
| 9 | + */ |
| 10 | +/* Prototype : proto mixed unserialize(string variable_representation) |
| 11 | + * Description: Takes a string representation of variable and recreates it |
| 12 | + * Source code: ext/standard/var.c |
| 13 | + * Alias to functions: |
| 14 | + */ |
| 15 | + |
| 16 | +echo "*** Testing unserialize() error/boolean distinction ***\n"; |
| 17 | + |
| 18 | +$garbage = "obvious non-serialized data"; |
| 19 | +$serialized_false = serialize(false); |
| 20 | + |
| 21 | +var_dump($serialized_false); |
| 22 | + |
| 23 | +$deserialized_garbage = unserialize($garbage); |
| 24 | +var_dump($deserialized_garbage); |
| 25 | + |
| 26 | +$deserialized_false = unserialize($serialized_false); |
| 27 | +var_dump($deserialized_false); |
| 28 | + |
| 29 | +echo "unserialize error and deserialized false are identical? " . (bool) ($deserialized_false == $deserialized_garbage) . "\n"; |
| 30 | + |
| 31 | +// candidate safe idiom for determining whether data is serialized |
| 32 | +function isSerialized($str) { |
| 33 | + return ($str == serialize(false) || @unserialize($str) !== false); |
| 34 | +} |
| 35 | + |
| 36 | +// Test unserialize error idiom |
| 37 | +var_dump(isSerialized($garbage)); |
| 38 | +var_dump(isSerialized($serialized_false)); |
| 39 | + |
| 40 | +echo "Done"; |
| 41 | +?> |
| 42 | +--EXPECTF-- |
| 43 | +*** Testing unserialize() error/boolean distinction *** |
| 44 | +string(4) "b:0;" |
| 45 | + |
| 46 | +Notice: unserialize(): Error at offset 0 of 27 bytes in %s/serialization_error_002.php on line 20 |
| 47 | +bool(false) |
| 48 | +bool(false) |
| 49 | +unserialize error and deserialized false are identical? 1 |
| 50 | +bool(false) |
| 51 | +bool(true) |
| 52 | +Done |
0 commit comments