From ebec0a969761f083259ffa7f7caa3b3a4efb889f Mon Sep 17 00:00:00 2001 From: Paul Vriens Date: Mon, 31 Mar 2008 19:51:21 +0200 Subject: [PATCH] setupapi: Fix for SetupGetIntField, with tests. --- dlls/setupapi/parser.c | 21 +++++++----- dlls/setupapi/tests/parser.c | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/dlls/setupapi/parser.c b/dlls/setupapi/parser.c index f6de4ed8550..a00e06862f3 100644 --- a/dlls/setupapi/parser.c +++ b/dlls/setupapi/parser.c @@ -1711,21 +1711,26 @@ BOOL WINAPI SetupGetIntField( PINFCONTEXT context, DWORD index, PINT result ) char *end, *buffer = localbuff; DWORD required; INT res; - BOOL ret = FALSE; + BOOL ret; - if (!SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required )) + if (!(ret = SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required ))) { if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE; if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE; - if (!SetupGetStringFieldA( context, index, buffer, required, NULL )) goto done; + if (!(ret = SetupGetStringFieldA( context, index, buffer, required, NULL ))) goto done; } - res = strtol( buffer, &end, 0 ); - if (end != buffer && !*end) + /* The call to SetupGetStringFieldA succeeded. If buffer is empty we have an optional field */ + if (!*buffer) *result = 0; + else { - *result = res; - ret = TRUE; + res = strtol( buffer, &end, 0 ); + if (end != buffer && !*end) *result = res; + else + { + SetLastError( ERROR_INVALID_DATA ); + ret = FALSE; + } } - else SetLastError( ERROR_INVALID_DATA ); done: if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer ); diff --git a/dlls/setupapi/tests/parser.c b/dlls/setupapi/tests/parser.c index d49c37ff412..6341222819e 100644 --- a/dlls/setupapi/tests/parser.c +++ b/dlls/setupapi/tests/parser.c @@ -475,6 +475,67 @@ static void test_pSetupGetField(void) SetupCloseInfFile( hinf ); } +static void test_SetupGetIntField(void) +{ + static const struct + { + const char *key; + const char *fields; + DWORD index; + INT value; + DWORD err; + } keys[] = + { + /* key fields index expected int errorcode */ + { "Key=", "48", 1, 48, ERROR_SUCCESS }, + { "Key=", "48", 0, -1, ERROR_INVALID_DATA }, + { "123=", "48", 0, 123, ERROR_SUCCESS }, + { "Key=", "0x4", 1, 4, ERROR_SUCCESS }, + { "Key=", "Field1", 1, -1, ERROR_INVALID_DATA }, + { "Key=", "Field1,34", 2, 34, ERROR_SUCCESS }, + { "Key=", "Field1,,Field3", 2, 0, ERROR_SUCCESS }, + { "Key=", "Field1,", 2, 0, ERROR_SUCCESS } + }; + unsigned int i; + + for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) + { + HINF hinf; + char buffer[MAX_INF_STRING_LENGTH]; + INFCONTEXT context; + UINT err; + BOOL retb; + INT intfield; + + strcpy( buffer, STD_HEADER "[TestSection]\n" ); + strcat( buffer, keys[i].key ); + strcat( buffer, keys[i].fields ); + hinf = test_file_contents( buffer, &err); + ok( hinf != NULL, "Expected valid INF file\n" ); + + SetupFindFirstLineA( hinf, "TestSection", "Key", &context ); + SetLastError( 0xdeadbeef ); + intfield = -1; + retb = SetupGetIntField( &context, keys[i].index, &intfield ); + if ( keys[i].err == ERROR_SUCCESS ) + { + ok( retb, "Expected success\n" ); + ok( GetLastError() == ERROR_SUCCESS || + GetLastError() == 0xdeadbeef /* win9x, NT4 */, + "Expected ERROR_SUCCESS or 0xdeadbeef, got %u\n", GetLastError() ); + } + else + { + ok( !retb, "Expected failure\n" ); + ok( GetLastError() == keys[i].err, + "Expected %d, got %u\n", keys[i].err, GetLastError() ); + } + ok( intfield == keys[i].value, "Expected %d, got %d\n", keys[i].value, intfield ); + + SetupCloseInfFile( hinf ); + } +} + static void test_GLE(void) { static const char *inf = @@ -597,6 +658,7 @@ START_TEST(parser) test_key_names(); test_close_inf_file(); test_pSetupGetField(); + test_SetupGetIntField(); test_GLE(); DeleteFileA( tmpfilename ); }