php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #18428 ctype functions return TRUE on zero-length strings
Submitted: 2002-07-19 01:12 UTC Modified: 2002-07-19 12:34 UTC
From: busterb at mail dot utexas dot edu Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4CVS-2002-07-19 OS: Linux
Private report: No CVE-ID: None
 [2002-07-19 01:12 UTC] busterb at mail dot utexas dot edu
The PHP ctype functions return TRUE when passed a zero 
length string. This is incorrect behavior; see the 
documentation for the contradiction. This is not an issue 
with the C ctype functions, since they deal with 
characters, which cannot be zero-length. 
 
The following patch corrects this behavior 
--- ctype.c     2002-07-18 23:58:02.000000000 -0500 
+++ ctype_fixed.c       2002-07-18 23:57:48.000000000 
-0500 
@@ -102,8 +102,11 @@ 
                { \ 
                        char *p; \ 
                        int n, len; \ 
-                       p=Z_STRVAL_P(c); \ 
                        len = Z_STRLEN_P(c); \ 
+            if (!len) { \ 
+                RETURN_FALSE; \ 
+            } \ 
+                       p = Z_STRVAL_P(c); \ 
                        for(n=0;n<len;n++) { \ 
                                if(!iswhat(*p++)) 
RETURN_FALSE; \ 
                        } \ 
 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-19 06:01 UTC] hholzgra@php.net
an empty string is a string ;)

the documentation says:
" When called with a string argument they will check every character in the string and will only return TRUE if every character in the string matches the requested criteria. "

can't see the contradiction here as in an empty string every of the 0 charactetrs matches the criteriy ...

will add a note about that in the documentation,
but the behaviour will not change for backwards 
compatibility reasons
 [2002-07-19 09:03 UTC] busterb at mail dot utexas dot edu
While I accept the backwards-compatibility argument 
conditionaly, the rest of the reasoning, i.e. "can't see 
the contradiction here as in an empty string every of the 
0 charactetrs matches the criteriy ..." is hard to 
believe. 
 
If there is nothing, there is nothing to match. With the C 
versions of these functions, it is obviously not possible 
to run into this situation; essentially they _require_ a 
'string' that is one character long, taking a character 
argument. Therefore, these functions should have required 
that the string be at least one character long in the 
first place. It is unfortunate that what inconsistent 
behavior with the real ctype functions must be maintained 
for backwards compatibility, especially for an extension 
that is so young (it has only been a default option since 
4.2). If there were ever a time to correct its behavior, 
better sooner than later. 
 
I had included ctype_ equivalent functions in PEAR under 
DBA/ctype.php due to the fact that these functions are not 
commonly included with PHP installations. I wrote that 
exhibit what I feel is the correct behavior before 
noticing the ctype functions built-in to PHP. Should I  
now rewrite them so that they and are inconsistent with 
C's ctype functions but compatible with PHP's?
 [2002-07-19 11:58 UTC] hholzgra@php.net
"If there is nothing, there is nothing to match."

i'm more for "if theres nothing, there's nothing to fail"

and comparing this to the ctype functions just makes
no sense, C operates on single characters only and 
the behaviour of the PHP functions on single characters
(either single character string or integer character code)
exactly matches the c behavior

if i was to implement the functions now i would
maybe consider the "empty is false" variant the
better one, but back then i didn't 

the extension has been marked EXPERIMENTAL for
a loooooong time and nobody objected, now it
has been declared stable and so it will be
 [2002-07-19 12:34 UTC] busterb at mail dot utexas dot edu
I must have overlooked the extension while it was 
experimental or not had a need for it then. Anyway, I 
reread the documentation, and realized that I had missed 
ctype's behavior for integers. ctype_xxx(ord($c)), does 
what I need.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Nov 19 23:00:01 2025 UTC