From 92a5149540c964ab1794e697d9ff4628eb555089 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Mon, 12 Sep 2005 11:13:05 +0000 Subject: [PATCH] Avoid non-portable va_list manipulations. --- dlls/msi/msiquery.c | 90 +++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 56 deletions(-) diff --git a/dlls/msi/msiquery.c b/dlls/msi/msiquery.c index 3cade1d95f8..eab6ef24f7c 100644 --- a/dlls/msi/msiquery.c +++ b/dlls/msi/msiquery.c @@ -139,62 +139,27 @@ UINT MSI_DatabaseOpenViewW(MSIDATABASE *db, return r; } -static UINT MSI_OpenQueryV( MSIDATABASE *db, MSIQUERY **view, - LPCWSTR fmt, va_list args ) -{ - LPWSTR szQuery; - LPCWSTR p; - UINT sz, rc; - va_list va; - - /* figure out how much space we need to allocate */ - va = args; - sz = lstrlenW(fmt) + 1; - p = fmt; - while (*p) - { - p = strchrW(p, '%'); - if (!p) - break; - p++; - switch (*p) - { - case 's': /* a string */ - sz += lstrlenW(va_arg(va,LPCWSTR)); - break; - case 'd': - case 'i': /* an integer -2147483648 seems to be longest */ - sz += 3*sizeof(int); - (void)va_arg(va,int); - break; - case '%': /* a single % - leave it alone */ - break; - default: - FIXME("Unhandled character type %c\n",*p); - } - p++; - } - - /* construct the string */ - szQuery = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR)); - va = args; - vsnprintfW(szQuery, sz, fmt, va); - - /* perform the query */ - rc = MSI_DatabaseOpenViewW(db, szQuery, view); - HeapFree(GetProcessHeap(), 0, szQuery); - return rc; -} - UINT MSI_OpenQuery( MSIDATABASE *db, MSIQUERY **view, LPCWSTR fmt, ... ) { UINT r; - va_list va; - - va_start(va, fmt); - r = MSI_OpenQueryV( db, view, fmt, va ); - va_end(va); + int size = 100, res; + LPWSTR query; + /* construct the string */ + for (;;) + { + va_list va; + query = HeapAlloc( GetProcessHeap(), 0, size*sizeof(WCHAR) ); + va_start(va, fmt); + res = vsnprintfW(query, size, fmt, va); + va_end(va); + if (res == -1) size *= 2; + else if (res >= size) size = res + 1; + else break; + } + /* perform the query */ + r = MSI_DatabaseOpenViewW(db, query, view); + HeapFree(GetProcessHeap(), 0, query); return r; } @@ -240,11 +205,24 @@ MSIRECORD *MSI_QueryGetRecord( MSIDATABASE *db, LPCWSTR fmt, ... ) MSIRECORD *rec = NULL; MSIQUERY *view = NULL; UINT r; - va_list va; + int size = 100, res; + LPWSTR query; - va_start(va, fmt); - r = MSI_OpenQueryV( db, &view, fmt, va ); - va_end(va); + /* construct the string */ + for (;;) + { + va_list va; + query = HeapAlloc( GetProcessHeap(), 0, size*sizeof(WCHAR) ); + va_start(va, fmt); + res = vsnprintfW(query, size, fmt, va); + va_end(va); + if (res == -1) size *= 2; + else if (res >= size) size = res + 1; + else break; + } + /* perform the query */ + r = MSI_DatabaseOpenViewW(db, query, &view); + HeapFree(GetProcessHeap(), 0, query); if( r == ERROR_SUCCESS ) {