getimagesize(): truncated JPEG SOF exports EOF as 4294967295 channels

Open Beginner friendly
#25,961 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, php
Domain
backend

Research direction

Start in ext/standard/image.c at the SOF reads around lines 481–485 and compare how php_stream_getc() results are handled before the channel count is exported around line 1602. The issue provides a minimal reproducer using getimagesizefromstring(); use it to confirm the truncated JPEG currently returns UINT_MAX as channels. Done when this input returns false and a regression test covers the truncated SOF segment.

Written by the indexing model from the issue text.

Description

Bug Status: Needs Triage
Description

The following code:

<?php
$jpeg = "\xFF\xD8\xFF\xC0" . pack('n', 8) . "\x08"
      . pack('nn', 1, 1);
var_dump(getimagesizefromstring($jpeg));

Resulted in this output:

array(9) {
  [0]=>
  int(1)
  [1]=>
  int(1)
  [2]=>
  int(2)
  [3]=>
  string(20) "width="1" height="1""
  ["bits"]=>
  int(8)
  ["channels"]=>
  int(4294967295)
  ["mime"]=>
  string(10) "image/jpeg"
  ["width_unit"]=>
  string(2) "px"
  ["height_unit"]=>
  string(2) "px"
}

But I expected this output instead:

bool(false)

The SOF field reads in ext/standard/image.c:481-485 assign php_stream_getc() results to unsigned fields without checking for EOF; at line 485 the missing component-count byte therefore becomes UINT_MAX, which is exported at line 1602. The truncated SOF segment must be rejected instead of publishing the error sentinel as channel metadata.

PHP Version
PHP 8.7.0-dev (cli) (built: Nov 29 2026 03:42:09) (NTS)
Copyright © The PHP Group and Contributors
Zend Engine v4.7.0-dev, Copyright © Zend by Perforce
    with Zend OPcache v8.7.0-dev, Copyright ©, by Zend by Perforce
Operating System

Ubuntu 24.04.5 LTS

Dominant language
C
Stars
41.9k
Forks
8.5k
Avg merge
2d 17h
Merged PRs (30d)
111

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from php/php-src

All issues in php/php-src

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.