[8/8] regedit: Modify getRegClass() to avoid dubious comparisons and casts between HKEYs and error codes.

Francois Gouget fgouget at codeweavers.com
Thu Jun 14 04:53:41 CDT 2007


---

Handles are usually relatively small integers (and HKEYs too I suspect), 
so expecting them to not collide with error codes which are quite dense 
in the range 0 to several hundreds is taking a pretty big risk. Besides 
the casts are ugly.


 programs/regedit/regproc.c |   18 ++++++++----------
 1 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/programs/regedit/regproc.c b/programs/regedit/regproc.c
index a973ad0..d1d757d 100644
--- a/programs/regedit/regproc.c
+++ b/programs/regedit/regproc.c
@@ -279,7 +279,7 @@ static LONG setValue(LPSTR val_name, LPSTR val_data)
  * Extracts from [HKEY\some\key\path] or HKEY\some\key\path types of line
  * the key class (what ends before the first '\')
  */
-static HKEY getRegClass(LPSTR lpClass)
+static BOOL getRegClass(LPSTR lpClass, HKEY* hkey)
 {
     LPSTR classNameEnd;
     LPSTR classNameBeg;
@@ -288,7 +288,7 @@ static HKEY getRegClass(LPSTR lpClass)
     char  lpClassCopy[KEY_MAX_LEN];
 
     if (lpClass == NULL)
-        return (HKEY)ERROR_INVALID_PARAMETER;
+        return FALSE;
 
     lstrcpynA(lpClassCopy, lpClass, KEY_MAX_LEN);
 
@@ -310,10 +310,11 @@ static HKEY getRegClass(LPSTR lpClass)
 
     for (i = 0; i < REG_CLASS_NUMBER; i++) {
         if (!strcmp(classNameBeg, reg_class_names[i])) {
-            return reg_class_keys[i];
+            *hkey = reg_class_keys[i];
+            return TRUE;
         }
     }
-    return (HKEY)ERROR_INVALID_PARAMETER;
+    return FALSE;
 }
 
 /******************************************************************************
@@ -365,8 +366,7 @@ static LONG openKey( LPSTR stdInput)
         return ERROR_INVALID_PARAMETER;
 
     /* Get the registry class */
-    currentKeyClass = getRegClass(stdInput); /* Sets global variable */
-    if (currentKeyClass == (HKEY)ERROR_INVALID_PARAMETER)
+    if (!getRegClass(stdInput, &currentKeyClass)) /* Sets global variable */
         return ERROR_INVALID_PARAMETER;
 
     /* Get the key name */
@@ -918,8 +918,7 @@ BOOL export_registry_key(CHAR *file_name, CHAR *reg_key_name)
         strcpy(reg_key_name_buf, reg_key_name);
 
         /* open the specified key */
-        reg_key_class = getRegClass(reg_key_name);
-        if (reg_key_class == (HKEY)ERROR_INVALID_PARAMETER) {
+        if (!getRegClass(reg_key_name, &reg_key_class)) {
             fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
                     getAppName(), reg_key_name);
             exit(1);
@@ -1052,8 +1051,7 @@ void delete_registry_key(CHAR *reg_key_name)
     if (!reg_key_name || !reg_key_name[0])
         return;
     /* open the specified key */
-    reg_key_class = getRegClass(reg_key_name);
-    if (reg_key_class == (HKEY)ERROR_INVALID_PARAMETER) {
+    if (!getRegClass(reg_key_name, &reg_key_class)) {
         fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
                 getAppName(), reg_key_name);
         exit(1);
-- 
1.4.4.4



More information about the wine-patches mailing list