Make WordPress Core

#63474 closed defect (bug) (fixed)

getchmod() can cause Fatal error

Reported by: apermo Owned by: SergeyBiryukov
Priority: normal Milestone: 7.0
Component: Filesystem API Version: 2.5
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

/wp-admin/includes/class-wp-filesystem-ftpsockets.php:326: Trying to access array offset on value of type bool

In both classes WP_Filesystem_FTPext and WP_Filesystem_ftpsockets

The function getchmod() looks like this:

<?php
public function getchmod( $file ) {
        $dir = $this->dirlist( $file );

        return $dir[ $file ]['permsn'];
}

and dirlist() can in both cases return false under certain conditions.

While it is rare and I could not yet figure out how to reproduce it, this will cause a fatal error.

I suggest to add a simple condition that will check if $dir is an array, and return either an empty string or null if it's not.

This will fix the error, without delivering wrong information.

<?php
public function getchmod( $file ) {
        $dir = $this->dirlist( $file );

        if ( ! empty( $dir[ $file ]['permsn'] ) ) {
                return $dir[ $file ]['permsn'];
        }

        return '';
}

Props to @malt3 for running into it.

Change History (3)

This ticket was mentioned in ​PR #8827 on ​WordPress/wordpress-develop by ​@apermo.


17 months ago
#1

  • Keywords has-patch added

Co-authored-by: @zaphir
Ref: https://core.trac.wordpress.org/ticket/63474#ticket

Fixes 63474 in the way suggested in the ticket.

Trac ticket: https://core.trac.wordpress.org/ticket/63474#ticket

#2 @SergeyBiryukov
11 months ago

  • Milestone Awaiting Review → 7.0

#3 @SergeyBiryukov
11 months ago

  • Owner set to SergeyBiryukov
  • Resolution → fixed
  • Status new → closed

In 61311:

Filesystem API: Check for the correct result in some WP_Filesystem_FTPext methods.

This avoids a PHP warning when ::dirlist() returns false under certain conditions:

Warning: Trying to access array offset on value of type bool in wp-admin/includes/class-wp-filesystem-ftpsockets.php on line 326

Follow-up to [6779], [30678], [45226].

Props apermo, malt3, SergeyBiryukov.
Fixes #63474.

Note: See TracTickets for help on using tickets.