Skip to content

Null value can be passed to openlog(), triggering a deprecation notice in PHP 8 #6933

@richardlampitt

Description

@richardlampitt

Description of the bug

Deprecated function: openlog(): Passing null to parameter #3 ($facility) of type int is deprecated in syslog_watchdog() (line 101 of C:\wamp64\www\_backdrop\sr-backdrop.local\httpdocs\core\modules\syslog\syslog.module).

The ultimate cause of the problem is that the constant LOG_LOCAL0 does not exist on Windows environments.

Steps To Reproduce

Attempt to create an image style variant of an image where the original image file doesn't exist. For example, one of the log messages that triggers this warning for myself is Source image at public://img_1882.png not found while trying to generate derivative image at public://styles/poster_admin/public/img_1882.png.

Actual behavior

Many PHP deprecation notices, depending on the number of messages logged.

Expected behavior

No PHP 8 deprecation warning.

Additional information

Add any other information that could help, such as:

  • Backdrop CMS version: 1.31
  • Web server and its version: apache 2.4.54
  • PHP version: 8.0+ (tested with 8.0, 8.1, 8.2 and 8.3)
  • Database sever (MySQL or MariaDB?) and its version: MariaDB Version: 10.11.11
  • Operating System and its version: Windows NT 10.0 build 19045 (Windows 10) AMD64

The problem is caused by insufficient data integrity checking in the syslog_watchdog() function:

function syslog_watchdog(array $log_entry) {
  global $base_url;

  $log_init = &backdrop_static(__FUNCTION__, FALSE);
  $config = config('system.core');

  if (!$log_init) {
    $log_init = TRUE;
    $facility = $config->get('log_facility');
    if ($facility === '') {
      $facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER;
    }
    // cspell:disable-next-line
    openlog($config->get('log_identity'), LOG_NDELAY, $facility);
  }

  $message = strtr($config->get('log_format'), array(
    '!base_url'    => $base_url,
    '!timestamp'   => $log_entry['timestamp'],
    '!type'        => $log_entry['type'],
    '!ip'          => $log_entry['ip'],
    '!request_uri' => $log_entry['request_uri'],
    '!referer'     => $log_entry['referer'],
    '!uid'         => $log_entry['uid'],
    '!link'        => strip_tags((string) $log_entry['link']),
    '!message'     => strip_tags((string) (!isset($log_entry['variables']) ? $log_entry['message'] : strtr($log_entry['message'], $log_entry['variables']))),
  ));

  syslog($log_entry['severity'], $message);
}

The issue can be fixed by supplying an appropriate fallback value to openlog(). For example:

    // 128 is the value used for LOG_LOCAL0
    openlog($config->get('log_identity'), LOG_NDELAY, $facility ?? 128);

As the minimum system requirements are now PHP 7.1, I'm using the ?? null-coalesce for this example.

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