php / php/php-src

Multi-Byte character breaks tempnam functionality

Open
#16,845 8 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Extension: standard Feature Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

Hi all,

first of all thanks for all the development on PHP. Love the language more than ever. In a recent project I encountered this bug (?):

tempnam() shortens the given prefix after character 63. When passing a multi-byte character in this position, the shortened prefix contains a malformed character. MacOS does not seem to be able to handle that, hence no file is created and a notice is triggered. The PHP docs do not state, that multi-byte strings are not allowed with tempnam().

To reproduce
  1. Create a new folder
  2. Create script test.php in this folder and paste the following code into it:
<?php
// String concatenation only for highlighting the malicious character
$prefix = 'this_string_pushes_the_multi_byte_character_in_position_______' . 'ä' . 'foobar';
var_dump(tempnam(__DIR__, $prefix));
  1. Run php test.php in that directory.
Desired behaviour

It should create a temporary file in this folder and tempnam() should return the path to the file:

string(108) "/Users/benedict/dev/multibyte/this_string_pushes_the_multi_byte_character_in_position_______atWEta7"
Actual behaviour

On MacOS (tested with Sonoma 14.6.1 and Sequoia 15.1) no temporary file is created, a notice is triggered and tempnam() returns false:

PHP Notice:  tempnam(): file created in the system's temporary directory in /Users/benedict/dev/multibyte/test.php on line 16

Notice: tempnam(): file created in the system's temporary directory in /Users/benedict/dev/multibyte/test.php on line 16
bool(false)

When you change the ä in position 63 for anything but a multi-byte character, everything works as expected

If I can provide any further information, please let me know.

PHP Version

PHP 8.3.13

Operating System

MacOS Sonoma 14.6.1

Contributor guide

Open the contributing guide

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.

Research direction

Start with the tempnam() entry point and reproduce the behavior using the test.php script and the supplied multibyte prefix on macOS. Compare the handling of the 63-character prefix boundary for ä and single-byte characters; done means tempnam() creates the file in the requested folder and returns its path without a notice.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
backend, operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.