wcscat_s and wcscpy_s requires number of elements as argument. wchar_t
uses 2 bytes for storage and using sizeof(internal_port_query) causes
access violation error on Windows 2012 R2 (64 bit). This patch introduces
a count_of() function to return the count of the wchar_t array.

Signed-off-by: Sairam Venugopal <[email protected]>
Reported-by: Sairam Venugopal <[email protected]>
Reported-at: openvswitch/ovs-issues#121
---
 lib/wmi.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/lib/wmi.c b/lib/wmi.c
index e38b482..a9dfd27 100644
--- a/lib/wmi.c
+++ b/lib/wmi.c
@@ -350,6 +350,8 @@ tranform_wide(char *name, wchar_t *wide_name)
     return true;
 }
 
+#define count_of(array) (sizeof(array)/sizeof(array[0]))
+
 /* This function will delete a switch internal port with a given name as input
  * executing "RemoveResourceSettings" as per documentation:
  * https://msdn.microsoft.com/en-us/library/hh850277%28v=vs.85%29.aspx
@@ -414,9 +416,9 @@ delete_wmi_port(char *name)
         retval = false;
         goto error;
     }
-    wcscat_s(internal_port_query, sizeof(internal_port_query), wide_name);
+    wcscat_s(internal_port_query, count_of(internal_port_query), wide_name);
 
-    wcscat_s(internal_port_query, sizeof(internal_port_query), L"\"");
+    wcscat_s(internal_port_query, count_of(internal_port_query), L"\"");
 
     hres = psvc->lpVtbl->ExecQuery(psvc,
                                    L"WQL",
@@ -626,6 +628,7 @@ error:
     return retval;
 }
 
+
 /* This function will create an internal port on the switch given a given name
  * executing the method AddResourceSettings as per documentation:
  * https://msdn.microsoft.com/en-us/library/hh850019%28v=vs.85%29.aspx.
@@ -700,9 +703,10 @@ create_wmi_port(char *name) {
         retval = false;
         goto error;
     }
-    wcscat_s(internal_port_query, sizeof(internal_port_query), wide_name);
 
-    wcscat_s(internal_port_query, sizeof(internal_port_query), L"\"");
+    wcscat_s(internal_port_query, count_of(internal_port_query), wide_name);
+
+    wcscat_s(internal_port_query, count_of(internal_port_query), L"\"");
     hres = psvc->lpVtbl->ExecQuery(psvc,
                                    L"WQL",
                                    internal_port_query,
@@ -748,7 +752,7 @@ create_wmi_port(char *name) {
         retval = false;
         goto error;
     }
-    wcscpy_s(internal_port_query, sizeof(internal_port_query),
+    wcscpy_s(internal_port_query, count_of(internal_port_query),
              L"SELECT * FROM Msvm_VirtualEthernetSwitch WHERE Name = \"");
 
     hres = pcls_obj->lpVtbl->Get(pcls_obj, L"SystemName", 0,
@@ -758,7 +762,7 @@ create_wmi_port(char *name) {
         goto error;
     }
 
-    wcscat_s(internal_port_query, sizeof(internal_port_query),
+    wcscat_s(internal_port_query, count_of(internal_port_query),
              vt_prop.bstrVal);
 
     VariantClear(&vt_prop);
@@ -780,7 +784,7 @@ create_wmi_port(char *name) {
     }
 
     /* Get the switch object on which the extension is activated. */
-    wcscat_s(internal_port_query, sizeof(internal_port_query), L"\"");
+    wcscat_s(internal_port_query, count_of(internal_port_query), L"\"");
     hres = psvc->lpVtbl->ExecQuery(psvc,
                                    L"WQL",
                                    internal_port_query,
@@ -810,11 +814,11 @@ create_wmi_port(char *name) {
         goto error;
     }
 
-    wcscpy_s(internal_port_query, sizeof(internal_port_query),
+    wcscpy_s(internal_port_query, count_of(internal_port_query),
              L"SELECT * FROM Msvm_VirtualEthernetSwitchSettingData WHERE "
              L"ElementName = \"");
 
-    wcscat_s(internal_port_query, sizeof(internal_port_query),
+    wcscat_s(internal_port_query, count_of(internal_port_query),
              vt_prop.bstrVal);
     VariantClear(&vt_prop);
 
@@ -831,11 +835,11 @@ create_wmi_port(char *name) {
      * Uniquely identifies an instance of this class. This property is
      * inherited from CIM_SettingData and is always
      * set to "Microsoft:GUID\DeviceSpecificData". */
-    wcscat_s(internal_port_query, sizeof(internal_port_query),
+    wcscat_s(internal_port_query, count_of(internal_port_query),
              L"\" AND InstanceID  = \"Microsoft:");
-    wcscat_s(internal_port_query, sizeof(internal_port_query),
+    wcscat_s(internal_port_query, count_of(internal_port_query),
              vt_prop.bstrVal);
-    wcscat_s(internal_port_query, sizeof(internal_port_query),
+    wcscat_s(internal_port_query, count_of(internal_port_query),
              L"\"");
 
     VariantClear(&vt_prop);
@@ -1135,10 +1139,10 @@ create_wmi_port(char *name) {
         goto error;
     }
 
-    wcscpy_s(internal_port_query, sizeof(internal_port_query),
+    wcscpy_s(internal_port_query, count_of(internal_port_query),
              L"SELECT * FROM MSFT_NetAdapter WHERE Name LIKE '%%");
-    wcscat_s(internal_port_query, sizeof(internal_port_query), wide_name);
-    wcscat_s(internal_port_query, sizeof(internal_port_query), L"%%'");
+    wcscat_s(internal_port_query, count_of(internal_port_query), wide_name);
+    wcscat_s(internal_port_query, count_of(internal_port_query), L"%%'");
 
     /* Get the object with the port name equal to name on the CIM. */
     hres = psvc->lpVtbl->ExecQuery(psvc,
-- 
2.9.0.windows.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to