Modify

Opened 9 months ago

Closed 8 months ago

#3996 closed Bug (No Bug)

Unexpected silent conversion of several AutoIt types when used as keys in maps

Reported by: anonymous Owned by:
Milestone: Component: AutoIt
Version: 3.3.15.0 Severity: None
Keywords: Cc:

Description

(this applies to all versions from 3.3.15.0 to 3.3.16.1)
In maps, when we use a float, a pointer, or a keyword (any of default/null/true/false) as a key, then the following unexpected (i.e. this behaviour is undocumented) conversion happens:

  • floats become ints; eg float 3.14159 becomes int 3
  • pointers/handles become ints as well: Ptr(0xCAFE) becomes int 51966
  • true becomes int 1;
  • any other keyword (default/null/false), and in fact any other type - funcs, maps(!), arrays(!!), dllstructs(!!!), even ur mom(untested...), - all that becomes int 0

What would be the most best, is to allow these additional sensible types to be used as keys. The additional sensible types are of course pointers/handles (i use them all the time when working with windows and GUIs as keys in a map; super useful!), floats, and (in my opinion) keywords.

And at the very least, this should be documented in the helpfile.
Thank you for consideration! :)

PS. Well well well.. while researching this bug, i found even more unexpected things!
the following snippet works perfectly fine in 3.3.15.0 and 3.3.15.1, but starting from 3.3.15.2 to 3.3.16.1 AutoIt completely crashes on it!

Global $m[], $p1 = ptr(0xCAFE), $p2 = ptr(0xCAFEBABE)
ConsoleWrite('setting Ptr('&$p1&') to 2357' & @CRLF)
$m[$p1] = 2357
ConsoleWrite('retrieving: '&$m[$p1] & @CRLF)
ConsoleWrite('setting Ptr('&$p2&') to 777' & @CRLF)
$m[$p2] = 777
ConsoleWrite('retrieving: '&$m[$p2] & @CRLF)
ConsoleWrite('-----now do the same, but via MapKeys in  For..in..next  loop---------' & @CRLF)
for $key in MapKeys($m)
  ConsoleWrite('key '&VarGetType($key)&' : '& $key & @CRLF)
  ConsoleWrite('data '&VarGetType($m[$key]) &' : '& $m[$key] & @CRLF)
next

Attachments (0)

Change History (2)

comment:1 Changed 9 months ago by Jpm

Map setting is only valid for String and positive integer
It is true that any key will convert by the internal AutoIt concersion to Integer when needed
So be carefull to the key value used. the doc is almost coherent not refering perhaps to negative values, so Handle and Ptr can have pb, float can match too the same entry
here is a test script which show what can happen

Global $m[]
SetMap(3.14158, 1.7, "Float")
SetMap(3.1418, 1.8, "Float") ; will overide previous setting
SetMap(Ptr(0xCAFE), 2357, "Ptr")
SetMap(Ptr(0xCAFEBABE), 777, "Ptr") ; cannot  be set
SetMap(Ptr(Null), 777, "Ptr")
SetMap(0, 888, "Null") ; will overide previous setting
ConsoleWrite('-----now do the same, but via MapKeys in  For..in..next  loop---------' & @CRLF)
For $key In MapKeys($m)
	ConsoleWrite('keyname : ' & $key & ' (' & VarGetType($key) & ')')
	ConsoleWrite(' = ' & $m[$key] & ' (' & VarGetType($m[$key]) & ')' & @CRLF)
Next

Func SetMap($vEntry, $vValue, $sType, $ScriptLineNumber = @ScriptLineNumber)
	Local $vEntryOld = $vEntry

	If Not (IsString($vEntry) Or IsInt($vEntry)) Then
		$vEntry = Int($vEntry)
		If Int($vEntry) >= 0 Then
			$sType &= " -> Int"
		EndIf
	EndIf
	If $vEntry < 0 Then
		ConsoleWrite('Debug(' & $ScriptLineNumber & ') : CANNOT set ' & $sType & '(' & $vEntryOld & ') to ' & $vValue & @CRLF)
	Else
		ConsoleWrite('Debug(' & $ScriptLineNumber & ') : setting ' & $sType & '(' & $vEntryOld & ') to ' & $vValue)
		$m[$vEntry] = $vValue
		ConsoleWrite(' = ' & $m[$vEntry] & @CRLF)
	EndIf
EndFunc   ;==>SetMap

comment:2 Changed 8 months ago by Jpm

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

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

Action
as closed The ticket will remain with no owner.
Author


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

 
Note: See TracTickets for help on using tickets.