[wp-trac] [WordPress Trac] #56620: Deprecated: strncmp(): Passing null to parameter #1 ($string1) of type string is deprecated

WordPress Trac noreply at wordpress.org
Fri Sep 30 00:39:15 UTC 2022


#56620: Deprecated: strncmp(): Passing null to parameter #1 ($string1) of type
string is deprecated
-------------------------------------------------+-------------------------
 Reporter:  imadarshakshat                       |       Owner:
                                                 |  SergeyBiryukov
     Type:  defect (bug)                         |      Status:  closed
 Priority:  normal                               |   Milestone:  6.1
Component:  Formatting                           |     Version:  trunk
 Severity:  minor                                |  Resolution:  fixed
 Keywords:  php81 has-patch has-unit-tests       |     Focuses:
  commit                                         |
-------------------------------------------------+-------------------------
Changes (by SergeyBiryukov):

 * status:  reviewing => closed
 * resolution:   => fixed


Comment:

 In [changeset:"54362" 54362]:
 {{{
 #!CommitTicketReference repository="" revision="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.
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56620#comment:16>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list