Jump to content











Photo
- - - - -

Something really strange in if X==Y


  • Please log in to reply
2 replies to this topic

#1 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 27 August 2021 - 08:48 AM

While writing a small grub4dos batch subroutine to limit the input (allowed key presses) in the batch I found by mere chance a strange quirk .

 

Some strings (composed of hex number characters) evaluate wrongly.

 

First occasion found was that I intercepted all keys but [ENTER], still a (and A) and s (and S) "went through".

 

the code used was

pause --test-key | set testkey=

if %testkey%.==1c0d. call :do_input

 

in theory you can press all the keys you want and none but [ENTER]  would "go through" the subroutine.

 

BUT in practice also a, A, s and S went through. :w00t:

 

Try this batch:

!BAT
setlocal
pager off

:loop
#press [End] key to exit
pause --test-key | set thiskey=
if "%thiskey%"=="4f00" goto :eof

set Enter=1c0d

if %thiskey%==%Enter% echo %thiskey%==%Enter% match 
if %thiskey%.==%Enter%. echo %thiskey%.==%Enter%. match
if not .%thiskey%==.%Enter% echo .%thiskey%==.%Enter% DO NOT match
if not "%thiskey%"=="%Enter%" echo "%thiskey%"=="%Enter%" DO NOT match
echo 

goto :loop

The number of possible collisions is nearly infinite

 

Try on command line:

if 1a4==1b5 echo Same

 

or 

 

if 2a==2b echo Same

 

but also:

 

if 2c==2f0 echo Same

 

And 

 

if 3r==3F0518CD echo Same

 

BUT

 

if 3k==3F0518CD echo Same 

 

behaves differently.

 

It seems that the first character on both sides of the == must be a number, then some (but not all) letters would do. :dubbio:

 

Tested on 0.4.6a 2021-05-16.

 

:duff:

Wonko

 

 

 

 

 

 



#2 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 27 August 2021 - 09:12 AM

== will compare the parameters as numbers if numbers are used in first character

 

when you compare a number (not a string), grub4dos recognises number suffixes (k,m,g,t or K,M.G,T)

k = 1024 m=1048576

 

grub4dos will ignore any non-numerical or non-number-suffix letter that follows a string beginning with a number

 

2a  is same as 2

2abcdf is same as 2

 

2k is same as 2048

2kabcd is 2048 when using ==, but invalid if using set /a or set /A (get 0)

 

2m=2097152

 

set /A A=2m

 

So use set /a testkey=%@retval%

 

P.S. I recommend 'Getting started with grub4dos' eBook by a bloke called Steve Si (which I think you were sent) ;-)


Edited by steve6375, 27 August 2021 - 09:51 AM.


#3 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 27 August 2021 - 11:24 AM

The generic issue is solved by using properly the comparison as string (i.e. using double quotes or a leading non-numerical character) OR (in this specific case) using a proper (hex) number comparison, i.e.:

pause --test-key | set thiskey=0x

if %thiskey%==0x1c0d ...

or as you suggest a proper (decimal) number comparison. 

 

The quirk is that the "wrong" way I used blocked *everything* but a, A, s, S (besides the intended [Enter]) because their scan codes happen to begin with 1 AND have a letter value as second. 

 

BTW also [] pass through for the same reasons.

 

And as you rightly pointed out :) , the k,m,g,t (and K,M,G,T) are "exceptions" (but don't of course apply to scancodes).

 

:duff:

Wonko






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users