Skip to content

Add -Wextra compiler flag exclude some of the major offender #5151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Feb 5, 2020

Although I don't think we will be able to add the -Wextra flag without disabling some of the warnings which are enabled within this short cut. It seems better to be aware that these issues exist within the codebase.

Currently I've disabled 54 flags from -Wextra:

  • -Wclobbered which arises a lot within the Opcache extensions and the SAPI.
  • -Wimplicit-fallthrough even with -W-implicit-fallthrough=1 there are still a large amount of them and a lot in the JIT/Opcache which may be generated files?
  • -Wunused-parameter this one happens a lot in macros and ZEND/PHP_API functions for extensions BC
  • -Wsign-compare if someone wants to tackle it, the compiler output for -W-sign-compare is a bit more than 1000 lines: https://gist.github.com/Girgias/8c7aefc9687515a31efce589e95fc942
  • -Wmissing-field-initializers only has seven offender but five of them are in PHPDBG, compiler output: https://gist.github.com/Girgias/74251f0a2a2fb08e2262186cc0ec925d

@Girgias
Copy link
Member Author

Girgias commented Feb 5, 2020

Forgot that the basic ./configure doesn't enable everything we test on CI

Edit: And obviously I also forgot that Travis checks less extensions than Azure Pipelines ...

@Girgias
Copy link
Member Author

Girgias commented Feb 5, 2020

So it seems there are a couple of issues remaining:

/home/travis/build/Girgias/php-src/ext/gd/libgd/gd_interpolation.c: In function ‘gdImageRotateBilinear’:

/home/travis/build/Girgias/php-src/ext/gd/libgd/gd_interpolation.c:1777:11: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]

    if ((m >= 0) && (m < src_h - 1) && (n >= 0) && (n < src_w - 1)) {

           ^

/home/travis/build/Girgias/php-src/ext/gd/libgd/gd_interpolation.c:1777:42: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]

    if ((m >= 0) && (m < src_h - 1) && (n >= 0) && (n < src_w - 1)) {

                                          ^

/home/travis/build/Girgias/php-src/ext/gd/libgd/gd_interpolation.c: At top level:

The GD ones need some investigations as our GD bundled lib is custom and it isn't clear if m and n should be unsigned.

Fixed with ac89139

/home/travis/build/Girgias/php-src/ext/mbstring/php_mbregex.c: In function ‘_php_mb_regex_ereg_search_exec’:

/home/travis/build/Girgias/php-src/ext/mbstring/php_mbregex.c:1465:13: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]

     if (beg >= 0 && beg <= end && end <= len) {

             ^

/home/travis/build/Girgias/php-src/ext/mbstring/php_mbregex.c: In function ‘zif_mb_ereg_search_getregs’:

/home/travis/build/Girgias/php-src/ext/mbstring/php_mbregex.c:1604:12: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]

    if (beg >= 0 && beg <= end && end <= len) {

            ^

/home/travis/build/Girgias/php-src/ext/mbstring/php_mbregex.c: At top level:

Fixed by c7094d8

In file included from /home/travis/build/Girgias/php-src/Zend/zend_execute.c:4511:0:

/home/travis/build/Girgias/php-src/Zend/zend_vm_execute.h:59488:23: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]

 static const uint32_t ZEND_FASTCALL zend_vm_get_opcode_handler_idx(uint32_t spec, const zend_op* op)

                       ^

Fixed with: d4de3f9

ext/phar/phar_path_check.c: In function ‘phar_path_check’:

ext/phar/phar_path_check.c:48:24: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]

  if ((YYLIMIT - YYCURSOR) < 4) YYFILL(4);

                        ^

ext/phar/phar_path_check.re: At top level:

The mbstring ones I have trouble understanding how the compiler figures out that it's unsigned as from my limit search it seems that in oniguruma those are *int

Finally the VM and Phar ones I don't have a clue how to fix.

@Girgias Girgias force-pushed the add-compiler-warnings branch from a7beb9f to 89216d1 Compare February 6, 2020 00:37
@Girgias
Copy link
Member Author

Girgias commented Feb 6, 2020

Azure pipelines also flags the following extensions:

Tidy:

In file included from /home/vsts/work/1/s/ext/tidy/tidy.c:30:0:
/usr/include/tidy/tidy.h:2095:13: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
 TIDY_EXPORT const ctmbstr TIDY_CALL TidyLangWindowsName( const tidyLocaleMapItem *item );
             ^~~~~
/usr/include/tidy/tidy.h:2101:13: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
 TIDY_EXPORT const ctmbstr TIDY_CALL TidyLangPosixName( const tidyLocaleMapItem *item );
             ^~~~~

And LDAP:

/home/vsts/work/1/s/ext/ldap/ldap.c: In function ‘_php_ldap_control_to_array’:
/home/vsts/work/1/s/ext/ldap/ldap.c:260:37: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
    if ( context && (context->bv_len >= 0) ) {
                                     ^~

Edit fixed by: c3e65d9

@nikic
Copy link
Member

nikic commented Feb 6, 2020

The mbstring ones I have trouble understanding how the compiler figures out that it's unsigned as from my limit search it seems that in oniguruma those are *int

While the values coming from onig are ptrdiff_t, we store them in size_t. I'm not sure whether that's a bug, or whether these values are always non-negative...

@Girgias
Copy link
Member Author

Girgias commented Feb 6, 2020

The mbstring ones I have trouble understanding how the compiler figures out that it's unsigned as from my limit search it seems that in oniguruma those are *int

While the values coming from onig are ptrdiff_t, we store them in size_t. I'm not sure whether that's a bug, or whether these values are always non-negative...

Well I'm imagining that the beginning part must be always greater than 0 otherwise it doesn't make sense? Or could there be a thing with negative offsets?

@Girgias Girgias force-pushed the add-compiler-warnings branch 2 times, most recently from 47f95a3 to 2320191 Compare February 10, 2020 10:55
@Girgias
Copy link
Member Author

Girgias commented Feb 10, 2020

I've locally enabled the -Wmissing-field-initializers compiler flag and there are way more than 7 but most (95%) of them are located in the mbstring extension.

https://gist.github.com/Girgias/273fe236e58d4f1e96e85700f55f568b

@Girgias
Copy link
Member Author

Girgias commented Feb 27, 2020

So 32 bits has now these added warnings since the addition of Sodium:

/home/vsts/work/1/s/ext/sodium/libsodium.c: In function ‘zif_sodium_crypto_aead_aes256gcm_encrypt’:
/home/vsts/work/1/s/ext/sodium/libsodium.c:2025:35: warning: comparison is always false due to limited range of data type [-Wtype-limits]
 if ((unsigned long long) msg_len > (16ULL * ((1ULL << 32) - 2ULL)) - crypto_aead_aes256gcm_ABYTES) {
                                  ^
/home/vsts/work/1/s/ext/sodium/libsodium.c: In function ‘zif_sodium_crypto_aead_aes256gcm_decrypt’:
/home/vsts/work/1/s/ext/sodium/libsodium.c:2090:52: warning: comparison is always false due to limited range of data type [-Wtype-limits]
 if (ciphertext_len - crypto_aead_aes256gcm_ABYTES > 16ULL * ((1ULL << 32) - 2ULL)) {
                                                   ^
/home/vsts/work/1/s/ext/sodium/libsodium.c: In function ‘zif_sodium_crypto_aead_chacha20poly1305_ietf_encrypt’:
/home/vsts/work/1/s/ext/sodium/libsodium.c:2284:35: warning: comparison is always false due to limited range of data type [-Wtype-limits]
 if ((unsigned long long) msg_len > 64ULL * (1ULL << 32) - 64ULL) {
                                  ^

The one I'm a bit more concerned about is the MacOs/Clang warning: performing pointer arithmetic on a null pointer has undefined behaviour [-Wnull-pointer-arithmetic] which, from my understanding, is the same as the GCC -Wclobbered which is UB, which we probably should fix.

@Girgias
Copy link
Member Author

Girgias commented Feb 27, 2020

Seems I missed some FFI warnings (only on ARM):

/home/travis/build/Girgias/php-src/ext/ffi/ffi.c: In function ‘zend_ffi_add_enum_val’:

/home/travis/build/Girgias/php-src/ext/ffi/ffi.c:5411:29: warning: comparison is always false due to limited range of data type [-Wtype-limits]

   if (!is_signed && val->ch < 0) {

                             ^

Fixed by: 69b7d01

@Girgias Girgias force-pushed the add-compiler-warnings branch 6 times, most recently from 2f97307 to 98af091 Compare February 28, 2020 00:12
@Girgias Girgias force-pushed the add-compiler-warnings branch from 98af091 to e63f3f7 Compare March 25, 2020 14:53
@Girgias
Copy link
Member Author

Girgias commented Mar 25, 2020

Sees a new warning got introduced in the JIT:

In file included from /home/travis/build/php/php-src/ext/opcache/jit/zend_jit.c:3242:0:

ext/opcache/jit/zend_jit_trace.c: In function ‘is_checked_guard’:

ext/opcache/jit/zend_jit_trace.c:779:11: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]

   if (idx >= 0) {

           ^

/home/travis/build/php/php-src/ext/opcache/jit/zend_jit.c: At top level:

Edit: Fixed by 3e6667d

@Girgias Girgias force-pushed the add-compiler-warnings branch 12 times, most recently from 107cca1 to efb655e Compare April 14, 2020 15:12
Copy link
Member

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG assuming CI passes.

Zend/Zend.m4 Outdated
@@ -221,7 +221,10 @@ else
AC_DEFINE(ZEND_DEBUG,0,[ ])
fi

test -n "$GCC" && CFLAGS="$CFLAGS -Wall -Wno-strict-aliasing"
test -n "$GCC" && CFLAGS="$CFLAGS -Wall -Wno-strict-aliasing -Wextra -Wno-implicit-fallthrough -Wno-unused-parameter -Wno-sign-compare"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move -Wextra before -Wno-strict-aliasing.

Zend/Zend.m4 Outdated
@@ -221,7 +221,10 @@ else
AC_DEFINE(ZEND_DEBUG,0,[ ])
fi

test -n "$GCC" && CFLAGS="$CFLAGS -Wall -Wno-strict-aliasing"
test -n "$GCC" && CFLAGS="$CFLAGS -Wall -Wno-strict-aliasing -Wextra -Wno-implicit-fallthrough -Wno-unused-parameter -Wno-sign-compare"
dnl Check if compiler supports -Wn-clobbered (only GCC)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dnl Check if compiler supports -Wn-clobbered (only GCC)
dnl Check if compiler supports -Wno-clobbered (only GCC)

Girgias added 3 commits April 14, 2020 17:53
This is an issue in the provided library and needs to be fixed upstream.
The compile warnings which are explicitly suppressed are:
 * -Wno-implicit-fallthrough
 * -Wno-unused-parameter
 * -Wno-sign-compare
 * -Wno-clobbered, only with GCC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants