- Fix memory leaks when reading strings and binary values from the registry
[reactos.git] / reactos / drivers / network / ndis / ndis / config.c
index 02eabd3..b95f1e0 100644 (file)
@@ -33,7 +33,6 @@
 
 #include "ndissys.h"
 
-#define NDIS_VERSION 0x00050001          /* the version of NDIS we claim to be to miniport drivers */
 #define PARAMETERS_KEY L"Parameters"     /* The parameters subkey under the device-specific key */
 
 /*
@@ -66,7 +65,7 @@ NdisWriteConfiguration(
     ULONG ParameterType;
     ULONG DataSize;
     PVOID Data;
-    WCHAR Buff[25];
+    WCHAR Buff[11];
 
     NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
 
@@ -87,6 +86,7 @@ NdisWriteConfiguration(
                       ParameterValue->ParameterData.IntegerData,
                       (ParameterValue->ParameterType == NdisParameterInteger) ? 10 : 16, &Str)))
                  {
+                      NDIS_DbgPrint(MIN_TRACE, ("RtlIntegerToUnicodeString failed (%x)\n", *Status));
                       *Status = NDIS_STATUS_FAILURE;
                       return;
                  }
@@ -139,14 +139,43 @@ NdisCloseConfiguration(
 {
     PMINIPORT_CONFIGURATION_CONTEXT ConfigurationContext = (PMINIPORT_CONFIGURATION_CONTEXT)ConfigurationHandle;
     PMINIPORT_RESOURCE Resource;
+    PNDIS_CONFIGURATION_PARAMETER ParameterValue;
+    PLIST_ENTRY CurrentEntry;
 
-    while(!IsListEmpty(&ConfigurationContext->ResourceListHead))
+    while((CurrentEntry = ExInterlockedRemoveHeadList(&ConfigurationContext->ResourceListHead,
+                                                      &ConfigurationContext->ResourceLock)) != NULL)
     {
-        Resource = (PMINIPORT_RESOURCE)ExInterlockedRemoveHeadList(&ConfigurationContext->ResourceListHead, &ConfigurationContext->ResourceLock);
-        if(Resource->ResourceType == MINIPORT_RESOURCE_TYPE_MEMORY)
+        Resource = CONTAINING_RECORD(CurrentEntry, MINIPORT_RESOURCE, ListEntry);
+        switch(Resource->ResourceType)
         {
+          case MINIPORT_RESOURCE_TYPE_REGISTRY_DATA:
+            ParameterValue = Resource->Resource;
+
+            switch (ParameterValue->ParameterType)
+            {
+               case NdisParameterBinary:
+                 ExFreePool(ParameterValue->ParameterData.BinaryData.Buffer);
+                 break;
+
+               case NdisParameterString:
+               case NdisParameterMultiString:
+                 ExFreePool(ParameterValue->ParameterData.StringData.Buffer);
+                 break;
+
+               default:
+                 break;
+            }
+
+          /* Fall through to free NDIS_CONFIGURATION_PARAMETER struct */
+
+          case MINIPORT_RESOURCE_TYPE_MEMORY:
             NDIS_DbgPrint(MAX_TRACE,("freeing 0x%x\n", Resource->Resource));
             ExFreePool(Resource->Resource);
+            break;
+
+          default:
+            NDIS_DbgPrint(MIN_TRACE,("Unknown resource type: %d\n", Resource->ResourceType));
+            break;
         }
 
         ExFreePool(Resource);
@@ -359,7 +388,7 @@ NdisReadConfiguration(
             return;
         }
 
-        MiniportResource->ResourceType = 0;
+        MiniportResource->ResourceType = MINIPORT_RESOURCE_TYPE_REGISTRY_DATA;
         MiniportResource->Resource = *ParameterValue;
 
         NDIS_DbgPrint(MID_TRACE,("inserting 0x%x into the resource list\n",
@@ -398,7 +427,7 @@ NdisReadConfiguration(
             return;
         }
 
-        MiniportResource->ResourceType = 0;
+        MiniportResource->ResourceType = MINIPORT_RESOURCE_TYPE_REGISTRY_DATA;
         MiniportResource->Resource = *ParameterValue;
         NDIS_DbgPrint(MID_TRACE,("inserting 0x%x into the resource list\n", MiniportResource->Resource));
         ExInterlockedInsertTailList(&ConfigurationContext->ResourceListHead,
@@ -434,7 +463,7 @@ NdisReadConfiguration(
             return;
         }
 
-        MiniportResource->ResourceType = 0;
+        MiniportResource->ResourceType = MINIPORT_RESOURCE_TYPE_REGISTRY_DATA;
         MiniportResource->Resource = *ParameterValue;
         NDIS_DbgPrint(MID_TRACE,("inserting 0x%x into the resource list\n", MiniportResource->Resource));
         ExInterlockedInsertTailList(&ConfigurationContext->ResourceListHead,
@@ -578,7 +607,7 @@ NdisReadConfiguration(
          }
     }
 
-    MiniportResource->ResourceType = MINIPORT_RESOURCE_TYPE_MEMORY;
+    MiniportResource->ResourceType = MINIPORT_RESOURCE_TYPE_REGISTRY_DATA;
     MiniportResource->Resource = *ParameterValue;
 
     ExInterlockedInsertTailList(&ConfigurationContext->ResourceListHead, &MiniportResource->ListEntry, &ConfigurationContext->ResourceLock);
@@ -662,6 +691,7 @@ NdisReadNetworkAddress(
     NDIS_STRING Keyword;
     UINT *IntArray = 0;
     UINT i,j = 0;
+    NDIS_STRING str;
 
     NdisInitUnicodeString(&Keyword, L"NetworkAddress");
     NdisReadConfiguration(Status, &ParameterValue, ConfigurationHandle, &Keyword, NdisParameterString);
@@ -672,11 +702,41 @@ NdisReadNetworkAddress(
         return;
     }
 
-    while (ParameterValue->ParameterData.StringData.Buffer[j] != '\0') j++;
+    if (ParameterValue->ParameterType == NdisParameterInteger)
+    {
+        WCHAR Buff[11];
+
+        NDIS_DbgPrint(MAX_TRACE, ("Read integer data %lx\n",
+                                  ParameterValue->ParameterData.IntegerData));
+
+        str.Buffer = Buff;
+        str.MaximumLength = (USHORT)sizeof(Buff);
+        str.Length = 0;
 
-    *NetworkAddressLength = (UINT)((j/2)+0.5);
+        *Status = RtlIntegerToUnicodeString(ParameterValue->ParameterData.IntegerData,
+                                            10,
+                                            &str);
 
-    if (j == 0)
+        if (*Status != NDIS_STATUS_SUCCESS)
+        {
+            NDIS_DbgPrint(MIN_TRACE, ("RtlIntegerToUnicodeString failed (%x)\n", *Status));
+            *Status = NDIS_STATUS_FAILURE;
+            return;
+        }
+
+        NDIS_DbgPrint(MAX_TRACE, ("Converted integer data into %wZ\n", &str));
+    }
+    else
+    {
+        ASSERT(ParameterValue->ParameterType == NdisParameterString);
+        str = ParameterValue->ParameterData.StringData;
+    }
+
+    while (j < str.Length && str.Buffer[j] != '\0') j++;
+    
+    *NetworkAddressLength = (j+1) >> 1;
+
+    if ((*NetworkAddressLength) == 0)
     {
         NDIS_DbgPrint(MIN_TRACE,("Empty NetworkAddress registry entry.\n"));
         *Status = NDIS_STATUS_FAILURE;
@@ -700,7 +760,7 @@ NdisReadNetworkAddress(
         return;
     }
 
-    MiniportResource->ResourceType = 0;
+    MiniportResource->ResourceType = MINIPORT_RESOURCE_TYPE_MEMORY;
     MiniportResource->Resource = IntArray;
     NDIS_DbgPrint(MID_TRACE,("inserting 0x%x into the resource list\n", MiniportResource->Resource));
     ExInterlockedInsertTailList(&ConfigurationContext->ResourceListHead, &MiniportResource->ListEntry, &ConfigurationContext->ResourceLock);
@@ -708,8 +768,8 @@ NdisReadNetworkAddress(
     /* convert from string to bytes */
     for(i=0; i<(*NetworkAddressLength); i++)
     {
-        IntArray[i] = (UnicodeToHexByte((ParameterValue->ParameterData.StringData.Buffer)[2*i]) << 4) +
-                UnicodeToHexByte((ParameterValue->ParameterData.StringData.Buffer)[2*i+1]);
+        IntArray[i] = (UnicodeToHexByte((str.Buffer)[2*i]) << 4) +
+                UnicodeToHexByte((str.Buffer)[2*i+1]);
     }
 
     *NetworkAddress = IntArray;