From 3b8266d20b9242450c45a27b4e75304391116c2a Mon Sep 17 00:00:00 2001 From: Hans Leidekker Date: Fri, 12 Oct 2012 14:25:13 +0200 Subject: [PATCH] wbemprox: Always convert from BSTR. --- dlls/wbemprox/class.c | 21 ++++++++++++++++----- dlls/wbemprox/query.c | 30 +++++++++++++++++++++--------- dlls/wbemprox/wbemprox_private.h | 3 +++ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/dlls/wbemprox/class.c b/dlls/wbemprox/class.c index b1fc364c473..c17e1b30f04 100644 --- a/dlls/wbemprox/class.c +++ b/dlls/wbemprox/class.c @@ -232,6 +232,20 @@ static struct record *create_record( const struct column *columns, UINT num_cols return record; } +void destroy_array( struct array *array, CIMTYPE type ) +{ + UINT i, size; + + if (!array) return; + if (type == CIM_STRING || type == CIM_DATETIME) + { + size = get_type_size( type ); + for (i = 0; i < array->count; i++) heap_free( *(WCHAR **)((char *)array->ptr + i * size) ); + } + heap_free( array->ptr ); + heap_free( array ); +} + static void destroy_record( struct record *record ) { UINT i; @@ -241,11 +255,8 @@ static void destroy_record( struct record *record ) { if (record->fields[i].type == CIM_STRING || record->fields[i].type == CIM_DATETIME) heap_free( record->fields[i].u.sval ); - else if ((record->fields[i].type & CIM_FLAG_ARRAY) && record->fields[i].u.aval) - { - heap_free( record->fields[i].u.aval->ptr ); - heap_free( record->fields[i].u.aval ); - } + else if (record->fields[i].type & CIM_FLAG_ARRAY) + destroy_array( record->fields[i].u.aval, record->fields[i].type & CIM_TYPE_MASK ); } heap_free( record->fields ); heap_free( record ); diff --git a/dlls/wbemprox/query.c b/dlls/wbemprox/query.c index f2c1782081d..b48d75dc359 100644 --- a/dlls/wbemprox/query.c +++ b/dlls/wbemprox/query.c @@ -639,8 +639,6 @@ static void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret V_VT( ret ) = type; } -#define CIM_TYPE_MASK 0xfff - HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret, CIMTYPE *type, LONG *flavor ) { @@ -750,19 +748,33 @@ static struct array *to_array( VARIANT *var, CIMTYPE *type ) ret->count = bound + 1; size = get_type_size( basetype ); - if (!(ret->ptr = heap_alloc( ret->count * size ))) + if (!(ret->ptr = heap_alloc_zero( ret->count * size ))) { heap_free( ret ); return NULL; } for (i = 0; i < ret->count; i++) { - if (SafeArrayGetElement( V_ARRAY( var ), &i, (char *)ret->ptr + i * size ) != S_OK) + void *ptr = (char *)ret->ptr + i * size; + if (vartype == VT_BSTR) { - if (vartype == VT_BSTR) - for (i--; i >= 0; i--) SysFreeString( *(BSTR *)(char *)ret->ptr + i * size ); - heap_free( ret->ptr ); - heap_free( ret ); + BSTR str; + if (SafeArrayGetElement( V_ARRAY( var ), &i, &str ) != S_OK) + { + destroy_array( ret, basetype ); + return NULL; + } + *(WCHAR **)ptr = heap_strdupW( str ); + SysFreeString( str ); + if (!*(WCHAR **)ptr) + { + destroy_array( ret, basetype ); + return NULL; + } + } + else if (SafeArrayGetElement( V_ARRAY( var ), &i, ptr ) != S_OK) + { + destroy_array( ret, basetype ); return NULL; } } @@ -790,7 +802,7 @@ HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type ) *type = CIM_BOOLEAN; break; case VT_BSTR: - *val = (INT_PTR)SysAllocString( V_BSTR( var ) ); + *val = (INT_PTR)heap_strdupW( V_BSTR( var ) ); if (!*val) return E_OUTOFMEMORY; *type = CIM_STRING; break; diff --git a/dlls/wbemprox/wbemprox_private.h b/dlls/wbemprox/wbemprox_private.h index 0f1305e3860..8de3a0c6ad6 100644 --- a/dlls/wbemprox/wbemprox_private.h +++ b/dlls/wbemprox/wbemprox_private.h @@ -31,6 +31,8 @@ enum param_direction PARAM_IN = 1 }; +#define CIM_TYPE_MASK 0x00000fff + #define COL_TYPE_MASK 0x0000ffff #define COL_FLAG_DYNAMIC 0x00010000 #define COL_FLAG_KEY 0x00020000 @@ -177,6 +179,7 @@ HRESULT get_propval( const struct view *, UINT, const WCHAR *, VARIANT *, HRESULT put_propval( const struct view *, UINT, const WCHAR *, VARIANT *, CIMTYPE ) DECLSPEC_HIDDEN; HRESULT to_longlong( VARIANT *, LONGLONG *, CIMTYPE * ) DECLSPEC_HIDDEN; SAFEARRAY *to_safearray( const struct array *, CIMTYPE ) DECLSPEC_HIDDEN; +void destroy_array( struct array *, CIMTYPE ) DECLSPEC_HIDDEN; HRESULT get_properties( const struct view *, SAFEARRAY ** ) DECLSPEC_HIDDEN; HRESULT get_object( const WCHAR *, IWbemClassObject ** ) DECLSPEC_HIDDEN; BSTR get_method_name( const WCHAR *, UINT ) DECLSPEC_HIDDEN;