php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66826 date() stopped accepting "\0" as a valid format value
Submitted: 2014-03-05 14:21 UTC Modified: 2017-10-24 10:25 UTC
From: butkus dot justas at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.5.9 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: butkus dot justas at gmail dot com
New email:
PHP Version: OS:

 

 [2014-03-05 14:21 UTC] butkus dot justas at gmail dot com
Description:
------------
This might be either string handling related, or date-specific string handling related, but starting with PHP 5.2.1 and still occurring in PHP 5.6.0alpha2, date() (as well as DateTime::format()) does not accept "\0" (NUL) as valid formatting character as valid and truncates at that point.

Likely it means that at some point date() started treating $format value as null-terminated string. Before PHP 5.2.1 the NUL byte was preserved in formatting output.

Reason behind using such characters: to have some clear and easily distinguishable separator which doesn't take up visual space in output.

Test script:
---------------
<?php

date_default_timezone_set( 'UTC' );
$time = strtotime( '2014-01-31' );

$format = "\\Fm\0 j, Y";

var_dump( date( $format, $time ) );

$datetime = new DateTime( '@' . $time );
var_dump( $datetime->format( $format ) );

Expected result:
----------------
string(12) "F01 31, 2014"
string(12) "F01 31, 2014"

Actual result:
--------------
string(3) "F01"
string(3) "F01"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-03-05 16:06 UTC] derick@php.net
-Assigned To: +Assigned To: derick
 [2015-08-18 17:24 UTC] cmb@php.net
-Status: Assigned +Status: Analyzed
 [2015-08-18 17:24 UTC] cmb@php.net
As <https://3v4l.org/1vQ94> shows, this issue is fixed as of PHP
7.0.0alpha1. Furthermore, the behavior until PHP 5.2.1 was not
really correct, because the null byte in the format string was
simply skipped (compare the string lengths).

To fix the issue for PHP 5.6, RETURN_STRINGL() instead of
RETURN_STRING would have to be used[1], but the actual length of
the string is only known inside of date_format, so this would
require some changes. I'm not sure if that's worth the trouble,
considering that the PHP 7 behavior is already correct, and that
a null byte in a format string is probably rather uncommon.

[1] <https://github.com/php/php-src/blob/php-5.6.12/ext/date/php_date.c#L3084>
 [2017-10-24 05:24 UTC] kalle@php.net
-Status: Analyzed +Status: Assigned
 [2017-10-24 10:25 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2017-10-24 10:25 UTC] cmb@php.net
Active support for PHP 5.6 has ended, so I'm closing this ticket.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 18:01:31 2024 UTC