Trying to access array offset on value of type bool

Started by sah62, July 23, 2022, 07:59:36 AM

Previous topic - Next topic

Arantor

No, it's a legitimate bug, albeit one that can't readily be reproduced.
Holder of controversial views, all of which my own.

sah62

I'm still seeing occasional instances of this error with SMF 2.1.4 and PHP 8.1.2. I decided to try a fix by changing this line of code in Subs-Attachments.php:

// What about the extension?
$thumb_ext = isset($context['valid_image_types'][$size[2]]) ? $context['valid_image_types'][$size[2]] : '';

to this:

// What about the extension?
$thumb_ext = is_array($size) && isset($context['valid_image_types'][$size[2]]) ? $context['valid_image_types'][$size[2]] : '';

That is, check to make sure that the value returned from getimagesize() and stored in $size is an array before attempting to access the value of $size[2]. If getimagesize() fails and returns boolean false, is_array($size) will evaluate to false and the value of $thumb_ext will be set to ''. That seems safe given that $thumb_ext will also be set to '' if isset($context['valid_image_types'][$size[2]]) evaluates to false.

Advertisement: