php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48465 sys_get_temp_dir() possibly inconsistent when using TMPDIR
Submitted: 2009-06-03 17:51 UTC Modified: 2011-03-28 18:51 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: marcel dot esser at gmail dot com Assigned: pajoye (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.*, 6CVS (2009-06-04) OS: *
Private report: No CVE-ID: None
 [2009-06-03 17:51 UTC] marcel dot esser at gmail dot com
Description:
------------
Some systems add a terminating directory separator to the TMPDIR env variable, such as Mac OS X. This is not consistent with the fallback default for sys_get_temp_dir(), which is '/tmp'. This causes problems when constructing filenames when not using tempnam().

Here is a short patch against 5.2 HEAD

203c203,209
< 			temporary_directory = strdup(s);
---
> 			/* on some systems (notably mac os x) TMPDIR has a DIRECTORY_SEPARATOR appended */
> 			if(s[strlen(s)-1] == DEFAULT_SLASH) {
> 				temporary_directory = tsrm_strndup(s,strlen(s)-1);
> 			} else {
> 				temporary_directory = strdup(s);
> 			}
> 
205a212
> 

Reproduce code:
---------------
<?php
echo sys_get_temp_dir() . "\n";


Expected result:
----------------
/var/folders/yC/yCto0tGnHIqmgIv2v8-pEk+++TI/-Tmp-

Actual result:
--------------
/var/folders/yC/yCto0tGnHIqmgIv2v8-pEk+++TI/-Tmp-/


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-03 17:53 UTC] marcel dot esser at gmail dot com
Pardon, I meant 5.2.9 CVS

Also, here is diff -u output:

--- main/php_open_temporary_file.c	2009-06-03 13:42:44.000000000 -0400
+++ /Users/marcelesser/php_open_temporary_file.c	2009-06-03 13:43:48.000000000 -0400
@@ -200,9 +200,16 @@
 	{
 		char* s = getenv("TMPDIR");
 		if (s) {
-			temporary_directory = strdup(s);
+			/* on some systems (notably mac os x) TMPDIR has a DIRECTORY_SEPARATOR appended */
+			if(s[strlen(s)-1] == DEFAULT_SLASH) {
+				temporary_directory = tsrm_strndup(s,strlen(s)-1);
+			} else {
+				temporary_directory = strdup(s);
+			}
+
 			return temporary_directory;
 		}
+
 	}
 #ifdef P_tmpdir
 	/* Use the standard default temporary directory. */
 [2009-06-04 06:00 UTC] kalle@php.net
Instead of two strlen() call, i could see it being defined to a value. Adding something like int len = strlen(s); after the if and before the first strdup, however im not on OSX so i wont touch it :)
 [2009-06-04 06:23 UTC] marcel dot esser at gmail dot com
Alternative patch:

--- php52/php5/main/php_open_temporary_file.c	2009-06-03 13:42:44.000000000 -0400
+++ /Users/marcelesser/php_open_temporary_file.c	2009-06-04 02:19:29.000000000 -0400
@@ -200,9 +200,18 @@
 	{
 		char* s = getenv("TMPDIR");
 		if (s) {
-			temporary_directory = strdup(s);
+			int last_char_idx = strlen(s) - 1;
+
+			/* on some systems (notably mac os x) TMPDIR has a DIRECTORY_SEPARATOR appended */
+			if(s[last_char_idx] == DEFAULT_SLASH) {
+				temporary_directory = tsrm_strndup(s,last_char_idx);
+			} else {
+				temporary_directory = strdup(s);
+			}
+
 			return temporary_directory;
 		}
+
 	}
 #ifdef P_tmpdir
 	/* Use the standard default temporary directory. */
 [2009-06-04 11:57 UTC] jani@php.net
It's not MacOSX specific, you can always set TMPDIR whatever you want.
 [2009-06-24 12:21 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2011-03-28 15:35 UTC] pajoye@php.net
-Status: Closed +Status: Assigned -Assigned To: +Assigned To: pajoye
 [2011-03-28 15:35 UTC] pajoye@php.net
Not fixed on windows
 [2011-03-28 18:43 UTC] pajoye@php.net
Automatic comment from SVN on behalf of pajoye
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=309792
Log: -  Fixed bug #48465 (sys_get_temp_dir() possibly inconsistent, windows fix
 [2011-03-28 18:51 UTC] pajoye@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC