nameoffnv / pywinauto

Automatically exported from code.google.com/p/pywinauto
Other
0 stars 0 forks source link

handleprops.text() can cause various issues, exceptions, freezes... #8

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. for each window, call .text() on it
2. get lucky

-----------------------------------------
There are a few issues in handleprops.text() as it is written:

1. The return value of SendMessageTimeout is not checked.

If it returns 0, the function failed or the timeout occurred. The function 
should return '', or raise an exception. Currently, we ignore this possibility.

2. We try to allocate a unicode buffer of a length filled in by 
SendMessageTimeout. When SendMessageTimeout fails, it sometimes puts anything 
it wants in our variable. This has led to various exceptions, such as the 
length being negative, and various (temporary) system freezes, due to 
allocating what I imagine to be a few GB of RAM. Sometimes it directly bails 
with a MemoryError or an Overflow error.

-------------
Fix:

change:
win32functions.SendMessageTimeout(
to:
ret = win32functions.SendMessageTimeout(

add:
    if ret == 0:
        return '' #failed, probably timeout
after the function call

change:
    if length:
to:
    if length > 0:

Original issue reported on code.google.com by cbwhiz@gmail.com on 8 Feb 2012 at 7:42

GoogleCodeExporter commented 9 years ago
Yes, I've come across this same issue when using 
pywinauto.findwindows.find_windows with a title regexp (title_re). I'm not sure 
if returning an empty string in case of a timeout is the best way to fix this, 
though.

Original comment by xof...@gmail.com on 23 Apr 2012 at 2:12

GoogleCodeExporter commented 9 years ago
I agree with empty string not being preferable. Why can't it just raise an 
exception? If some kind of a return code is retrievable from 
SendMessageTimeout, imo it should be carried as part of the exception so it's 
possible to determine what happened afterwards (and possibly retry).

Original comment by seppo.yl...@gmail.com on 16 Jan 2013 at 2:40

GoogleCodeExporter commented 9 years ago
FWIW re-reading the code: return value of win32functions.SendMessageTimeout is 
not checked. If it is zero, the call failed or timed out as per 
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644952%28v=vs.85%29.a
spx. If this happens, you get weird lengths. Is there any maintainer for this 
project so we can get this finally fixed or do we need a fork? Docs also 
suggest calling GetLastError. Imo the function should see if retval is 0, get 
last error, translate to something sensible, raise exception.

Original comment by seppo.yl...@gmail.com on 3 Feb 2015 at 5:38