win-pvdrivers

changeset 759:996f1c7d2cb3 0.11.0.206

Fixed a bug causing a hang in copyconfig when the buffer size is exceeded or the user has insufficient privileges
author James Harper <james.harper@bendigoit.com.au>
date Tue Jan 12 23:17:20 2010 +1100 (2010-01-12)
parents 6bed92b175e6
children 11a00d18ed6a
files copyconfig/copyconfig.c
line diff
     1.1 --- a/copyconfig/copyconfig.c	Tue Jan 12 23:15:40 2010 +1100
     1.2 +++ b/copyconfig/copyconfig.c	Tue Jan 12 23:17:20 2010 +1100
     1.3 @@ -44,8 +44,9 @@ main(
     1.4    CHAR value_name[256];
     1.5    DWORD value_name_len;
     1.6    DWORD value_type;
     1.7 -  CHAR value_data[1024];
     1.8 +  PCHAR value_data;
     1.9    DWORD value_data_len;
    1.10 +  DWORD value_data_max_len;
    1.11    HKEY src_key_handle;
    1.12    HKEY dst_key_handle;
    1.13    
    1.14 @@ -173,22 +174,58 @@ main(
    1.15          continue;
    1.16        }
    1.17        value_name_len = 256;
    1.18 -      value_data_len = 1024;
    1.19 +      value_data_max_len = 0;
    1.20 +      value_data = NULL;
    1.21 +      value_data_len = 0;
    1.22        while ((status = RegEnumValue(dst_key_handle, 0, value_name, &value_name_len, NULL, &value_type, (LPBYTE)value_data, &value_data_len)) != ERROR_NO_MORE_ITEMS)
    1.23        {
    1.24 -        RegDeleteValue(dst_key_handle, value_name);
    1.25 +        //printf("A status = %08x, value_data_max_len = %d, value_data_len = %d\n", status, value_data_max_len, value_data_len);
    1.26 +        if (value_data_len > value_data_max_len || status == ERROR_MORE_DATA)
    1.27 +        {
    1.28 +          //printf("A Buffer %d too small. Allocating %d bytes\n", value_data_max_len, value_data_len);
    1.29 +          if (value_data != NULL)
    1.30 +            free(value_data);
    1.31 +          value_data_max_len = value_data_len;
    1.32 +          value_data = malloc(value_data_max_len);
    1.33 +        }
    1.34 +        else if (status != ERROR_SUCCESS)
    1.35 +        {
    1.36 +          printf("Failed to read registry %08x\n", status);
    1.37 +          return 1;
    1.38 +        }
    1.39 +        else
    1.40 +        {
    1.41 +          RegDeleteValue(dst_key_handle, value_name);
    1.42 +        }
    1.43          value_name_len = 256;
    1.44 -        value_data_len = 1024;
    1.45 +        value_data_len = value_data_max_len;
    1.46        }
    1.47        i = 0;
    1.48        value_name_len = 256;
    1.49 -      value_data_len = 1024;
    1.50 +      value_data_len = value_data_max_len;
    1.51        while ((status = RegEnumValue(src_key_handle, i, value_name, &value_name_len, NULL, &value_type, (LPBYTE)value_data, &value_data_len)) != ERROR_NO_MORE_ITEMS)
    1.52        {
    1.53 -        RegSetValueEx(dst_key_handle, value_name, 0, value_type, (BYTE *)value_data, value_data_len);
    1.54 +        //printf("B status = %08x, value_data_max_len = %d, value_data_len = %d\n", status, value_data_max_len, value_data_len);
    1.55 +        if (value_data_len > value_data_max_len || status == ERROR_MORE_DATA)
    1.56 +        {
    1.57 +          //printf("B Buffer %d too small. Allocating %d bytes\n", value_data_max_len, value_data_len);
    1.58 +          if (value_data != NULL)
    1.59 +            free(value_data);
    1.60 +          value_data_max_len = value_data_len;
    1.61 +          value_data = malloc(value_data_max_len);
    1.62 +        }
    1.63 +        else if (status != ERROR_SUCCESS)
    1.64 +        {
    1.65 +          printf("Failed to read registry %08x\n", status);
    1.66 +          return 1;
    1.67 +        }
    1.68 +        else
    1.69 +        {
    1.70 +          RegSetValueEx(dst_key_handle, value_name, 0, value_type, (BYTE *)value_data, value_data_len);
    1.71 +          i++;
    1.72 +        }
    1.73          value_name_len = 256;
    1.74 -        value_data_len = 1024;
    1.75 -        i++;
    1.76 +        value_data_len = value_data_max_len;
    1.77        }
    1.78        printf(" Copied\n", buf);
    1.79      }