Make WordPress Core

Opened 19 months ago

Closed 19 months ago

Last modified 19 months ago

#56620 closed defect (bug) (fixed)

Deprecated: strncmp(): Passing null to parameter #1 ($string1) of type string is deprecated

Reported by: imadarshakshat's profile imadarshakshat Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.1 Priority: normal
Severity: minor Version: 6.1
Component: Formatting Keywords: php81 has-patch has-unit-tests commit
Focuses: Cc:

Description

Deprecated: strncmp(): Passing null to parameter #1 ($string1) of type string is deprecated in D:\xampp\htdocs\wp\wp-includes\class-wp-theme-json.php on line 1762

Change History (17)

#1 @dingo_d
19 months ago

  • Focuses coding-standards removed
  • Keywords php81 added

This ticket was mentioned in PR #3311 on WordPress/wordpress-develop by creedally-dev.


19 months ago
#2

  • Keywords has-patch added

Fixed Deprecated Warning Passing null to parameter #1 in get_property_value()

Trac ticket: Ticket 56620

This ticket was mentioned in Slack in #core by adarshakshatcreedally. View the logs.


19 months ago

#4 @SergeyBiryukov
19 months ago

  • Milestone changed from Awaiting Review to 6.1
  • Version set to trunk

Hi there, welcome to WordPress Trac! Thanks for the ticket.

Moving to 6.1, as this a new issue that causes 35 errors when running tests on PHP 8.1, as seen in a recent test run.

Introduced in [54162], as the corresponding test run has 91 errors, while the one just before has 56 errors.

Version 0, edited 19 months ago by SergeyBiryukov (next)

#5 @jrf
19 months ago

Yes, as @SergeyBiryukov already said, this one is on our radar and a patch is being worked on (by @antonvlasenko).

Also note that I've looked at the patch in GH PR #3311 and I don't think that's the right fix.

This ticket was mentioned in Slack in #core-test by adarshakshatcreedally. View the logs.


19 months ago

This ticket was mentioned in PR #3307 on WordPress/wordpress-develop by anton-vlasenko.


19 months ago
#7

  • Keywords has-unit-tests added

#8 @jrf
19 months ago

For the record, the backtrace of these errors is along the lines of the below:

`1) Tests_Blocks_Editor::test_get_block_editor_settings_theme_json_settings
strncmp(): Passing null to parameter #1 ($string1) of type string is deprecated

/var/www/src/wp-includes/class-wp-theme-json.php:1762
/var/www/src/wp-includes/class-wp-theme-json.php:1649
/var/www/src/wp-includes/class-wp-theme-json.php:2074
/var/www/src/wp-includes/class-wp-theme-json.php:1010
/var/www/src/wp-includes/class-wp-theme-json.php:906
/var/www/src/wp-includes/global-styles-and-settings.php:140
/var/www/src/wp-includes/block-editor.php:421
/var/www/tests/phpunit/tests/blocks/editor.php:388
/var/www/vendor/bin/phpunit:123

22) Tests_Theme_wpThemeJson::test_remove_insecure_properties_removes_unsafe_styles_sub_properties
strncmp(): Passing null to parameter #1 ($string1) of type string is deprecated

/var/www/src/wp-includes/class-wp-theme-json.php:1762
/var/www/src/wp-includes/class-wp-theme-json.php:1649
/var/www/src/wp-includes/class-wp-theme-json.php:2633
/var/www/src/wp-includes/class-wp-theme-json.php:2512
/var/www/tests/phpunit/tests/theme/wpThemeJson.php:2011
/var/www/vendor/bin/phpunit:123

jrfnl commented on PR #3307:


19 months ago
#9

@anton-vlasenko Please open a separate PR for each individual fix using atomic commits, i.e. fix + associated tests in one commit.
This PR is trying to do too much, which makes it unclear what is being addressed by it and why.

#10 @jrf
19 months ago

  • Keywords commit added

Reviewed the open PR by @anton-vlasenko and safe for some minor tidying up, GH PR #3307 is ready for commit.

Last edited 19 months ago by jrf (previous) (diff)

anton-vlasenko commented on PR #3307:


19 months ago
#11

Thanks for setting up this patch & the associated test @anton-vlasenko !

All looks good and I have confirmed that the test would throw an error on PHP 8.1 without the patch and no longer does so with the patch.

I just left some small remarks about the test itself, mostly just nitpicks.

Thanks for the review, @jrfnl! I really appreciate it.
I've fixed the issues you mentioned.

#12 @antonvlasenko
19 months ago

Testing Instructions

Steps to test https://github.com/WordPress/wordpress-develop/pull/3307:

  1. Go to https://github.com/WordPress/wordpress-develop/pull/3307.
  2. Make sure that the unit tests pass.
  3. Check the logs for the PHPUnit Tests / 8.1 on ubuntu-latest (pull_request) CI action, e.g.: https://github.com/WordPress/wordpress-develop/actions/runs/3153793903/jobs/5130675552
  4. Open the Run PHPUnit tests pane.
  5. Make sure you see no strncmp(): Passing null to parameter #1 ($string1) of type string is deprecated errors in the log.

anton-vlasenko commented on PR #3307:


19 months ago
#13

@anton-vlasenko Please open a separate PR for each individual fix using atomic commits, i.e. fix + associated tests in one commit.
This PR is trying to do too much, which makes it unclear what is being addressed by it and why.

You are absolutely right. I've removed unrelated commits from this PR.
These unrelated commits were supposed to fix the esc_url() tests. I cannot reproduce these errors anymore (on the latest trunk).

jrfnl commented on PR #3307:


19 months ago
#14

@anton-vlasenko Please open a separate PR for each individual fix using atomic commits, i.e. fix + associated tests in one commit.
This PR is trying to do too much, which makes it unclear what is being addressed by it and why.

You are absolutely right. I've removed unrelated commits from this PR. These unrelated commits were supposed to fix the esc_url() tests. I cannot reproduce these errors anymore (on the latest trunk).

I suspect that _might_ be related to two closely related patches for PHP 8.1 which got committed yesterday ([54349] and [54351]) (or some other PHP 8.1 patches committed earlier this week, so many moving parts... :joy:)

#15 @SergeyBiryukov
19 months ago

  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#16 @SergeyBiryukov
19 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 54362:

Code Modernization: Fix null to non-nullable deprecation in WP_Theme_JSON::get_property_value().

This commit aims to fix errors caused by incorrect usage of the strncmp() function inside the WP_Theme_JSON::get_property_value() method on PHP 8.1 and above.

Some history of the affected code:

  • [50973] introduced the WP_Theme_JSON::get_property_value() method.
  • [54162] removed the $default parameter from the _wp_array_get() call in the method.

With the latter change, the default value that is returned if the path does not exist within the array, or if $array or $path are not arrays, became null instead of an empty string. Since null would then be unintentionally passed to the strncmp() function further in the code, this caused ~35 errors in the test suite along the lines of:

1) Tests_Blocks_Editor::test_get_block_editor_settings_theme_json_settings
strncmp(): Passing null to parameter #1 ($string1) of type string is deprecated

/var/www/src/wp-includes/class-wp-theme-json.php:1754
/var/www/src/wp-includes/class-wp-theme-json.php:1641
/var/www/src/wp-includes/class-wp-theme-json.php:2066
/var/www/src/wp-includes/class-wp-theme-json.php:1002
/var/www/src/wp-includes/class-wp-theme-json.php:898
/var/www/src/wp-includes/global-styles-and-settings.php:140
/var/www/src/wp-includes/block-editor.php:421
/var/www/tests/phpunit/tests/blocks/editor.php:388
/var/www/vendor/bin/phpunit:123

This commit includes:

  • Restoring the $default value for _wp_array_get() call.
  • Adding an early return if the value is an empty string or null.
  • Adding a dedicated unit test to ensure that the method returns a string for invalid paths or null values.

Follow-up to [50973], [54162].

Props antonvlasenko, jrf, imadarshakshat, SergeyBiryukov.
Fixes #56620.

SergeyBiryukov commented on PR #3307:


19 months ago
#17

Thanks for the PR! Merged in r54362.

Note: See TracTickets for help on using tickets.