Skip to content

Fix off-by-one bug when truncating tempnam prefix #11870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ PHP_FUNCTION(tempnam)
ZEND_PARSE_PARAMETERS_END();

p = php_basename(prefix, prefix_len, NULL, 0);
if (ZSTR_LEN(p) > 64) {
if (ZSTR_LEN(p) >= 64) {
ZSTR_VAL(p)[63] = '\0';
}

Expand Down
76 changes: 76 additions & 0 deletions ext/standard/tests/file/tempnam_variation9.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
--TEST--
Test tempnam() function: usage variations - test prefix maximum size
--SKIPIF--
<?php
if(substr(PHP_OS, 0, 3) == "WIN")
die("skip Do not run on Windows");
?>
--FILE--
<?php
/* Testing the maximum prefix size */

echo "*** Testing tempnam() maximum prefix size ***\n";
$file_path = __DIR__."/tempnamVar9";
mkdir($file_path);

$pre_prefix = "begin_";
$post_prefix = "_end";
$fixed_length = strlen($pre_prefix) + strlen($post_prefix);
/* An array of prefixes */
$names_arr = array(
$pre_prefix . str_repeat("x", 7) . $post_prefix,
$pre_prefix . str_repeat("x", 63 - $fixed_length) . $post_prefix,
$pre_prefix . str_repeat("x", 64 - $fixed_length) . $post_prefix,
$pre_prefix . str_repeat("x", 65 - $fixed_length) . $post_prefix,
$pre_prefix . str_repeat("x", 300) . $post_prefix,
);

foreach($names_arr as $i=>$prefix) {
echo "-- Iteration $i --\n";
try {
$file_name = tempnam("$file_path", $prefix);
} catch (Error $e) {
echo $e->getMessage(), "\n";
continue;
}

$base_name = basename($file_name);
echo "File name is => ";
print($base_name);
echo "\n";
echo "File name length is => ";
print(strlen($base_name));
echo "\n";

if (file_exists($file_name)) {
unlink($file_name);
}
}
rmdir($file_path);

?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should have a --CLEAN-- section removing the folder and all files within it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. Added.

--CLEAN--
<?php
$file_path = __DIR__."/tempnamVar9";
if (file_exists($file_path)) {
array_map('unlink', glob($file_path . "/*"));
rmdir($file_path);
}
?>
--EXPECTF--
*** Testing tempnam() maximum prefix size ***
-- Iteration 0 --
File name is => begin_%rx{7}%r_end%r.{6}%r
File name length is => 23
-- Iteration 1 --
File name is => begin_%rx{53}%r_end%r.{6}%r
File name length is => 69
-- Iteration 2 --
File name is => begin_%rx{54}%r_en%r.{6}%r
File name length is => 69
-- Iteration 3 --
File name is => begin_%rx{55}%r_e%r.{6}%r
File name length is => 69
-- Iteration 4 --
File name is => begin_%rx{57}%r%r.{6}%r
File name length is => 69