Do not use LoadStringW for get strlen of resource string

James Hawkins truiken at gmail.com
Tue Jan 30 14:55:10 CST 2007


On 1/30/07, Vitaly Lipatov <lav at etersoft.ru> wrote:
> As LoadStringW(...NULL,0) returns length of resource string only in Wine,
> rewrote this part in independent manner.
>
> Changelog:
>  - do not use LoadStringW for get strlen of resource string
>

This is wrong:

> @@ -75,15 +75,16 @@ static LPWSTR HH_ANSIToUnicode(LPCSTR an
>  /* Loads a string from the resource file */
>  static LPWSTR HH_LoadString(DWORD dwID)
>  {
> -    LPWSTR string = NULL;
> +    WCHAR buf[1024];
> +    LPWSTR string;
>      int iSize;
>
> -    iSize = LoadStringW(hhctrl_hinstance, dwID, NULL, 0);
> +    iSize = LoadStringW(hhctrl_hinstance, dwID, buf, sizeof(buf)/sizeof(buf[0]));
>      iSize += 2; /* some strings (tab text) needs double-null termination */
>
>    string = HeapAlloc(GetProcessHeap(), 0, iSize * sizeof(WCHAR));
>     LoadStringW(hhctrl_hinstance, dwID, string, iSize);
> -
> +    string[iSize] = 0; /* add second null termination */
>      return string;
>  }

Why are you adding string[iSize] = 0...?  This is an internal wrapper
around LoadString that makes no promises about double-null
termination, etc.  Don't make assumptions.  Also, you need to keep the
style of the original code when making changes.  There was an empty
line where you added the aforementioned pieced of code, and that empty
line separates logically different pieces of the function.  1024 is a
magic number and it needs to go.  You're putting a limitation of 1024
length on HH_LoadString that didn't exist before.

-- 
James Hawkins



More information about the wine-devel mailing list