[php-src] Issue #21113: imagetruecolortopalette() causes Segmentation Fault in Linux CLI only

[email protected] (neptune2)
Newsgroups php.bugs
Message-ID <[email protected]>
Issue: https://github.com/php/php-src/issues/21113
Author: neptune2

### Description

The following minimalized code (test.php):

```
<?php
$im = @imagecreatetruecolor(200, 50)
      or die('Cannot Initialize new GD image stream');

// Code allocating colors, filling background and adding text removed (no impact)

imagetruecolortopalette($im, false, 2); // This line initiates the potential CLI Segfault condition

// usleep(15000); // Uncomment this line to Prevent CLI Segfault !! (approx 0.015 sec or longer for slower system)

imagepng($im, "test.png");  // Works despite of Segfault. No impact on Segfault if commented out
<?php
```

Results in **Segmentation fault** . 

To Reproduce:
```
/usr/bin/php test.php
```

Other observations:
- No Segfault occurs when running as CGI - php initiated from browser (Apache/2.4.66 error.log)
- No Segfault occurs on Windows (Win11, PHP 8.5.1 (cli) (built: Dec 16 2025 16:25:55) (NTS Visual C++ 2022 x64)
- Uncommenting the usleep() line prevents the Segfault - even for much larger images
- Changing the imagecreatetruecolor(200, 50) height arg to 15 or less prevents the Segfault (obviously not a viable workaround) 

### PHP Version

```plain
PHP 8.5.2 (cli) (built: Jan 18 2026 14:15:10) (NTS)
Copyright (c) The PHP Group
Built by Debian
Zend Engine v4.5.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.2, Copyright (c), by Zend Technologies

Also fails on 8.4.17 and 8.3.30.
Does **not** Segfault with PHP 7.4.3 (cli) (built: Mar 25 2025 18:57:03) ( NTS ) Ubuntu 20.04.6 i5-2400 CPU @ 3.10GHz
```

### Operating System

Debian 12.2.0  (i5-7500T CPU @ 2.70GHz)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.