kuza55 / csexwb2

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

Crash when finding only cache entries #71

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Call twice FinUrlCacheEntries with the pattern "Visited:.*"

What is the expected output? What do you see instead?
The second call should return an empty arraylist, but it crashes instead

What are the OS and IE versions?
Windows XP SP3, IE 7

What version of the product are you using?
Version 2.0.0.1

Please provide any additional information below.

The problem is in FindUrlCacheEntries in WinApis.cs
I tried this code, and it seems to solve the problem : 

int lastError = Marshal.GetLastWin32Error();
if (lastError == Hresults.ERROR_INSUFFICIENT_BUFFER)
{
  //Allocate buffer
  buffer = Marshal.AllocHGlobal((int)structSize);
  //Call again, this time it should succeed
  hEnum = FindFirstUrlCacheEntry(urlPattern, buffer, out structSize);
  // get last error so we can detect there are no more items
  lastError = Marshal.GetLastWin32Error();
}
if (lastError == Hresults.ERROR_NO_MORE_ITEMS)
{
  return results;
}

I also noticed that the "Empty cache" and "Empty cookies" tools from the
DemoApp don't work (they both empty all cookies and cache entries).

Regards,

Claire

Original issue reported on code.google.com by claire.a...@gmail.com on 10 Feb 2009 at 5:43

GoogleCodeExporter commented 8 years ago
Hi MH,

Here is a new proposal to correct the bug in FindUrlCacheEntries :

       public static ArrayList FindUrlCacheEntries(string urlPattern)
        {
            ArrayList results = new ArrayList();

            IntPtr buffer = IntPtr.Zero;
            UInt32 structSize;

            //This call will fail but returns the size required in structSize
            //to allocate necessary buffer
            IntPtr hEnum = FindFirstUrlCacheEntry(urlPattern, buffer, out structSize);
            try
            {
                int lastErrorCacheEntry = 0;
                if (hEnum == IntPtr.Zero)
                {
                    lastErrorCacheEntry = Marshal.GetLastWin32Error();
                    if (lastErrorCacheEntry == Hresults.ERROR_INSUFFICIENT_BUFFER)
                    {
                        //Allocate buffer
                        buffer = Marshal.AllocHGlobal((int)structSize);
                        //Call again, this time it should succeed
                        hEnum = FindFirstUrlCacheEntry(urlPattern, buffer, out
structSize);
                        // get last error so we can detect there are no more items
                        lastErrorCacheEntry = Marshal.GetLastWin32Error();
                    }
                    if (lastErrorCacheEntry == Hresults.ERROR_NO_MORE_ITEMS)
                    {
                        return results;
                    }
                }

                // To avoid crash
                if (buffer == IntPtr.Zero)
                {
                    throw new ApplicationException("Unable to find first url cache
entry, error=" + lastErrorCacheEntry);
                }

                INTERNET_CACHE_ENTRY_INFO result =
(INTERNET_CACHE_ENTRY_INFO)Marshal.PtrToStructure(buffer,
typeof(INTERNET_CACHE_ENTRY_INFO));
                try
                {
                    if (Regex.IsMatch(result.lpszSourceUrlName, urlPattern,
RegexOptions.IgnoreCase))
                    {
                        results.Add(result);
                    }
                }
                catch (ArgumentException ae)
                {
                    throw new ApplicationException("Invalid regular expression,
details=" + ae.Message);
                }

                if (buffer != IntPtr.Zero)
                {
                    try { Marshal.FreeHGlobal(buffer); }
                    catch { }
                    buffer = IntPtr.Zero;
                    structSize = 0;
                }

                //Loop through all entries, attempt to find matches
                while (true)
                {
                    long nextResult = FindNextUrlCacheEntry(hEnum, buffer, out
structSize);
                    if (nextResult != 1) //TRUE
                    {
                        int lastError = Marshal.GetLastWin32Error();
                        if (lastError == Hresults.ERROR_INSUFFICIENT_BUFFER)
                        {
                            buffer = Marshal.AllocHGlobal((int)structSize);
                            nextResult = FindNextUrlCacheEntry(hEnum, buffer, out
structSize);
                        }
                        else if (lastError == Hresults.ERROR_NO_MORE_ITEMS)
                        {
                            break;
                        }
                    }

                    result =
(INTERNET_CACHE_ENTRY_INFO)Marshal.PtrToStructure(buffer,
typeof(INTERNET_CACHE_ENTRY_INFO));
                    if (Regex.IsMatch(result.lpszSourceUrlName, urlPattern,
RegexOptions.IgnoreCase))
                    {
                        results.Add(result);
                    }

                    if (buffer != IntPtr.Zero)
                    {
                        try { Marshal.FreeHGlobal(buffer); }
                        catch { }
                        buffer = IntPtr.Zero;
                        structSize = 0;
                    }
                }
            }
            finally
            {
                if (hEnum != IntPtr.Zero)
                {
                    FindCloseUrlCache(hEnum);
                }
                if (buffer != IntPtr.Zero)
                {
                    try { Marshal.FreeHGlobal(buffer); }
                    catch { }
                }
            }

            return results;
        }

Regards,

Claire

Original comment by claire.a...@gmail.com on 11 Feb 2009 at 11:06

GoogleCodeExporter commented 8 years ago
Claire, lets make a baby!!! :D
This problem was happening on random windows 7 machines. I was just starting to 
get desperate trying to figure out why.

You just saved my life.... tks so much!!

Nuno Araújo

Original comment by nemesis....@gmail.com on 5 Sep 2013 at 6:11