php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26906 strlen returns values for non string types
Submitted: 2004-01-14 13:24 UTC Modified: 2004-01-14 14:05 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: scottmacvicar at ntlworld dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4CVS-2004-01-14 (stable) OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: scottmacvicar at ntlworld dot com
New email:
PHP Version: OS:

 

 [2004-01-14 13:24 UTC] scottmacvicar at ntlworld dot com
Description:
------------
strlen will return a value for boolean types as well as a consitent value of 5 for an array.

Reproduce code:
---------------
<?php

var_dump(strlen('foo')); // 3
var_dump(strlen(true)); // 1
var_dump(strlen(false)); // 0
var_dump(strlen(array('foo', 'bar'))); // 5

?>

Expected result:
----------------
string foo = int(3)
bool true = int(-1)
bool false = int(-1)
array('foo', 'bar') = int(-1)

Actual result:
--------------
string foo = int(3)
bool true = int(1)
bool false = int(0)
array('foo', 'bar') = int(5)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-14 13:25 UTC] scottmacvicar at ntlworld dot com
Patch

diff -u zend_builtin_functions.c zend_builtin_functions.c.patched
--- zend_builtin_functions.c    2003-08-28 18:06:52.000000000 +0100
+++ zend_builtin_functions.c.patched    2004-01-14 17:46:02.000000000 +0000
@@ -260,6 +260,16 @@
        if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) {
                ZEND_WRONG_PARAM_COUNT();
        }
+       switch((*str)->type) {
+               case IS_LONG:
+               case IS_DOUBLE:
+               case IS_STRING:
+                       break;
+               default:
+                       zend_error(E_WARNING, "Variable passed to strlen() is not a string or number");
+                       RETURN_LONG(-1);
+                       break;
+       }
        convert_to_string_ex(str);
        RETVAL_LONG((*str)->value.str.len);
 }
 [2004-01-14 13:52 UTC] eru@php.net
As PHP is a typeless language, this behaviour is expected. Please take a look at this extended version of your script:

var_dump((string)'foo', strlen('foo')); // 3
var_dump((string)true, strlen(true)); // 1
var_dump((string)false, strlen(false)); // 0
var_dump((string)array('foo', 'bar'), strlen(array('foo', 'bar'))); // 5

 [2004-01-14 14:05 UTC] scottmacvicar at ntlworld dot com
Indeed PHP is a typeless language and the convert_to_string_ex(str); attempts to convert an invalid value to a string.

A similar change to this was made in PHP 5 regarding array_merge which would cause a Warning if a non array type was used as a parameter and I feel that a likewise change would be of benefit.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 22:01:30 2024 UTC