php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80617 GCC throws warning about type narrowing in ZEND_TYPE_INIT_CODE
Submitted: 2021-01-11 23:52 UTC Modified: 2021-01-12 16:22 UTC
From: nathanael at prolegis dot ca Assigned:
Status: Closed Package: Compile Warning
PHP Version: 8.0.1 OS: Linux
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: nathanael at prolegis dot ca
New email:
PHP Version: OS:

 

 [2021-01-11 23:52 UTC] nathanael at prolegis dot ca
Description:
------------
When compiling a php extension with GCC we're getting a warning with gcc which is a fatal error with clang.

When compiling we get the following warning/output:

/usr/include/php/Zend/zend_types.h:264:50: warning: narrowing conversion of '((((12 == 17) ? ((1 << 2) | (1 << 3)) : ((12 == 16) ? (((((((((1 << 1) | (1 << 2)) | (1 << 3)) | (1 << 4)) | (1 << 5)) | (1 << 6)) | (1 << 7)) | (1 << 8)) | (1 << 9)) : (1 << 12))) | ((& arg)->Php::Argument::allowNull() ? 2 : 0)) | 0)' from 'int' to 'uint32_t {aka unsigned int}' inside { } [-Wnarrowing]
   | ((allow_null) ? _ZEND_TYPE_NULLABLE_BIT : 0) | (extra_flags))
                                                  ^
/usr/include/php/Zend/zend_types.h:260:11: note: in definition of macro 'ZEND_TYPE_INIT_MASK'
  { NULL, (_type_mask) }
           ^~~~~~~~~~
zend/callable.h:217:62: note: in expansion of macro 'ZEND_TYPE_INIT_CODE'
             case Type::Callable:    info->type = (zend_type) ZEND_TYPE_INIT_CODE(IS_CALLABLE, arg.allowNull(), 0);  break;  // anything that can be invoke
 

the issue is arg.allowNull() it returns an int, even though its 0 or 1 there's an the warning is there. If I just pass a 0, there is no warning. Another user commented as follows:

https://github.com/CopernicaMarketingSoftware/PHP-CPP/pull/471#discussion_r553716780

Basically saying the issue was "The problem is because the zend_type struct takes a uint32_t, but they defined _ZEND_TYPE_NULLABLE_BIT as 0x2, and in C/C++ constants are signed by default. "

Is this correct? If so can the _ZEND_TYPE_NULLABLE_BIT be defined as 0x2u instead? I've also tried casting the result of arg.allowNull to uint32_t to no avail.

Test script:
---------------
info->type = (zend_type) ZEND_TYPE_INIT_CODE(IS_UNDEF, arg.allowNull(), 0);

Expected result:
----------------
No warnings about type narrowing when passing an int.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-01-12 09:02 UTC] nikic@php.net
Did you try modifying the header to verify that adding the "u" does avoid the warning?
 [2021-01-12 16:22 UTC] nathanael at prolegis dot ca
Yes, if I edit Zend/zend_types.h and change _ZEND_TYPE_NULLABLE_BIT to 0x2u I can compile the extension without any warnings.
 [2021-01-14 09:09 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=880bf6222415f5bb87a73bc830bfa98ef30c267a
Log: Fixed bug #80617: Type narrowing warning in ZEND_TYPE_INIT_CODE
 [2021-01-14 09:09 UTC] nikic@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 08 20:01:27 2024 UTC