|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55835 char* field should be const char* to avoid C++ warning
Submitted: 2011-10-03 07:56 UTC Modified: 2017-11-02 20:58 UTC
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: olivier at yakaz dot com Assigned: nikic (profile)
Status: Closed Package: Compile Warning
PHP Version: 5.3.8 OS: All (seen under Linux)
Private report: No CVE-ID: None
 [2011-10-03 07:56 UTC] olivier at yakaz dot com

I'm writing a C++ extension to PHP.
When declaring a INI entry I get the following warning, multiple times:
warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

This only arises when compiling with a C++ compiler.
The right and easy fix seems to set some fields to const char *.
Some may even be set to const char * const (but this alternative seems
to be used

The proposed patch is against the php5-dev-5.3.8-2 package of debian sid:
PHP 5.3.8-2 with Suhosin-Patch (cli) (built: Sep 12 2011 07:28:26)

Test script:
Write a C++ extension:
config.m4 should contain PHP_REQUIRE_CXX().

Declare your module:
zend_module_entry quezako_module_entry = {
   "YourExtensionName", // (1 warning here)
   "0.42", // (1 warning here)

Declare an INI entry:
   "extensionName.variable", // (1 warning here)
   "default value", // (1 warning here)

Expected result:
No compilation warning.

Actual result:
Multiple of the following warning:
warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

Using the very common fix of prepending (char*) to the string constant is
especially harmful here, because of ZEND_INI_ENTRY3_EX using sizeof() on in: it
returns 4/8 (32/64bits platform).
Using a cast to char[] solves the problem, but the above fix is a very very
common mistake.

If the target fields were const char*, no compilation warning would be rose.


field_constness_cpp_compilation_warning_fix.patch (last revision 2011-10-03 07:57 UTC by olivier at yakaz dot com)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-02 20:58 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2017-11-02 20:58 UTC]
Closing this as the fields are const char * as of PHP 7.0.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Feb 26 05:01:25 2024 UTC