Skip to content

_GNU_SOURCE definition not effective - memrchr() missing #8834

@rainerjung

Description

@rainerjung

Description

Compilation warns

warning: cast to pointer from integer of different size [-Wint-to-pointe
r-cast]
  return (const void*)memrchr(s, c, n);
...
warning: implicit declaration of function 'memrchr'; did you mean 'memc
hr'? [-Wimplicit-function-declaration]
  return (const void*)memrchr(s, c, n);

Although configure sets _GNU_SOURCE in main/php_config.h which should make memrchr() available, in practise this does not suffice. For example Zend/zend_strtod.c includes zend_operators.h, which includes Zend/zend_portability.h, Zend/zend_config.h and finally main/php_config. There _GNU_SOURCE gets defined and then string.h is included, but nevertheles memrchr ist not available. Looking at string.h, memrchr needs __USE_GNU to define memrchr. __USE_GNU should not be set directly, but gets set from _GNU_SOURCE in features.h. This header file as most system header files gets loaded only once and is inluded by most system header files. So whenever another system header file is included before php_main.h, the setting of _GNU_SOURCE there no longer is effective.

For example Zend/zend_operators.h loads errno.h and assert.h before it loads zend_portability.h, Zend/zend_config.h and finally main/php_config. Both if these system header files already load features.h

Note that this leads to runtime crashes eg. in the phar extension, see #8828

Since reorganizing he header file orders might be brittle, it seems better to add _GNU_SOURCE to CFLAGS or CPPFLAGS by the Makefile (which already does it for a few compilation units) and probably to make the Makefile or an include of it configure controlled.

PHP Version

PHP 8.2.0alpha1

Operating System

Linux eg. RHEL 8

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions