Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#2234 closed Bug (No Bug)

_TempFile Infinite loop bug

Reported by: matwachich Owned by:
Milestone: Component: Standard UDFs
Version: Severity: None
Keywords: Cc:


Sometimes, _TempFile enters in an infinite loop, because there is no free file name.

I think the _TempFile function should be modified to accept another parameter: Maximum loop number

Here is my modification

; #FUNCTION# ====================================================================================================================
; Name...........: _TempFile
; Description ...: Generate a name for a temporary file. The file is guaranteed not to exist yet.
; Syntax.........: _TempFile([$s_DirectoryName = @TempDir[, $s_FilePrefix = "~"[, $s_FileExtension = ".tmp"[, $i_RandomLength = 7]]]])
; Parameters ....: $s_DirectoryName - Optional: Name of directory for filename, defaults to the users %TEMP% directory
;                  $s_FilePrefix    - Optional: File prefixname, defaults to "~"
;                  $s_FileExtension - Optional: File extenstion, defaults to ".tmp"
;                  $i_RandomLength  - Optional: Number of characters to use to generate a unique name, defaults to 7
;                  $i_MaxLoops		- Optional: Maximum number of random file names to check, default is 100
; Return values .: Success - Filename of a temporary file which does not exist
; Author ........: Dale (Klaatu) Thompson
; Modified.......: Hans Harder - Added Optional parameters
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _TempFile($s_DirectoryName = @TempDir, $s_FilePrefix = "~", $s_FileExtension = ".tmp", $i_RandomLength = 7, $i_MaxLoops = 100)
	; Check parameters
	If IsKeyword($s_FilePrefix) Then $s_FilePrefix = "~"
	If IsKeyword($s_FileExtension) Then $s_FileExtension = ".tmp"
	If IsKeyword($i_RandomLength) Then $i_RandomLength = 7
	If Not FileExists($s_DirectoryName) Then $s_DirectoryName = @TempDir ; First reset to default temp dir
	If Not FileExists($s_DirectoryName) Then $s_DirectoryName = @ScriptDir ; Still wrong then set to Scriptdir
	; add trailing \ for directory name
	If StringRight($s_DirectoryName, 1) <> "\" Then $s_DirectoryName = $s_DirectoryName & "\"
	Local $s_TempName, $i_loop = 0
		$s_TempName = ""
		While StringLen($s_TempName) < $i_RandomLength
			$s_TempName = $s_TempName & Chr(Random(97, 122, 1))
		$s_TempName = $s_DirectoryName & $s_FilePrefix & $s_TempName & $s_FileExtension
		$i_loop += 1
		If $i_loop > $i_MaxLoops Then
			$s_TempName = ""
	Until Not FileExists($s_TempName)

	Return $s_TempName
EndFunc   ;==>_TempFile


Attachments (0)

Change History (3)

comment:1 Changed 10 years ago by trancexx

  • Resolution set to No Bug
  • Status changed from new to closed

You mean sometimes as once in 100 hundred billion trillion times or never?

comment:2 Changed 10 years ago by matwachich

Yes, very few times, but it happened for me, and it was very hard (for me) to find that the bug was from this function.

For example if you call it like this

_TempFile(@TempDir, "_temporary_number_", ".tmp", 2)

It could happen

Anyway, thanks for reading me.

comment:3 Changed 10 years ago by Valik

You're right, it could happen. Because you wrote shit code and limited your possible unique file names to 99 (or 100 if 00 is used) and didn't bother to clean those files up when they are no longer in use. That isn't the fault of the function, that's the fault of you writing bad code.

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

as closed The ticket will remain with no owner.

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.