[PATCH] oledb32: Correct length calculation in DataConvert for DBTYPE_STR

Huw Davies huw at codeweavers.com
Mon Feb 27 07:21:58 CST 2017


On Fri, Feb 24, 2017 at 06:45:16AM +0000, Alistair Leslie-Hughes wrote:
> v2 - Correct failing test under wine
> 
> Signed-off-by: Alistair Leslie-Hughes <leslie_alistair at hotmail.com>
> ---
>  dlls/oledb32/convert.c | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/dlls/oledb32/convert.c b/dlls/oledb32/convert.c
> index ac462f2..20ef917 100644
> --- a/dlls/oledb32/convert.c
> +++ b/dlls/oledb32/convert.c
> @@ -816,23 +816,23 @@ static HRESULT WINAPI convert_DataConvert(IDataConvert* iface,
>      case DBTYPE_STR:
>      {
>          BSTR b;
> -        DBLENGTH bstr_len;
> +        DBLENGTH length;
>          INT bytes_to_copy;
> -        hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
> +        hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &length,
>                                        src, &b, sizeof(BSTR), src_status, dst_status,
>                                        precision, scale, flags);
>          if(hr != S_OK) return hr;
> -        bstr_len = SysStringLen(b);
> -        *dst_len = bstr_len * sizeof(char); /* Doesn't include size for '\0' */
> +        length = WideCharToMultiByte(CP_ACP, 0, b, -1, NULL, 0, NULL, NULL);
> +        *dst_len =  SysStringLen(b); /* Doesn't include size for '\0' */

This obviously can't be right.  As I said, you want to pass SysStringLen(b) as
the wstr_len to WideCharToMultiByte().  Then whatever that returns will be
the length of the ansi string excluding the '\0'.  That should go into *dst_len.

>          *dst_status = DBSTATUS_S_OK;
> -        bytes_to_copy = min(*dst_len + sizeof(char), dst_max_len);
> +        bytes_to_copy = min(length, dst_max_len);

bytes_to_copy should be min(length + 1, dst_max_len), so unchanged,
but let's get rid of sizeof(char).

>          if(dst)
>          {
>              if(bytes_to_copy >= sizeof(char))
>              {
> -                WideCharToMultiByte(CP_ACP, 0, b, bytes_to_copy - sizeof(char), dst, dst_max_len, NULL, NULL);
> +                WideCharToMultiByte(CP_ACP, 0, b, -1, dst, dst_max_len, NULL, NULL);

Again SysStringLen(b) here for the wstr_len.

>                  *((char *)dst + bytes_to_copy / sizeof(char) - 1) = 0;

This is correct, but again let's get rid of the sizeof(char).

> -                if(bytes_to_copy < *dst_len + sizeof(char))
> +                if(bytes_to_copy < length)

should be length + 1.

You probably want to add a test for a STR -> STR conversion of "test\0ed" and pass
in interesting src lengths like 6, 7 and 8, then check the returned dst (using
memcmp, not strcmp).

Huw.



More information about the wine-devel mailing list