diff mbox

[ovs-dev] lib Windows: Use xmalloc instead of malloc

Message ID 20170519213843.16304-1-rams@vmware.com
State Superseded
Headers show

Commit Message

Shashank Ram May 19, 2017, 9:38 p.m. UTC
xmalloc checks if the size is valid before allocating
memory.

Signed-off-by: Shashank Ram <rams@vmware.com>
---
 lib/netdev-windows.c | 4 ++--
 lib/wmi.c            | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

--
2.9.3.windows.2

Comments

Sairam Venugopal May 19, 2017, 9:48 p.m. UTC | #1
Acked-by: Sairam Venugopal <vsairam@vmware.com>





On 5/19/17, 2:38 PM, "ovs-dev-bounces@openvswitch.org on behalf of Shashank Ram" <ovs-dev-bounces@openvswitch.org on behalf of rams@vmware.com> wrote:

>xmalloc checks if the size is valid before allocating
>memory.
>
>Signed-off-by: Shashank Ram <rams@vmware.com>
>---
> lib/netdev-windows.c | 4 ++--
> lib/wmi.c            | 4 ++--
> 2 files changed, 4 insertions(+), 4 deletions(-)
>
>diff --git a/lib/netdev-windows.c b/lib/netdev-windows.c
>index 375cb32..b4b39e9 100644
>--- a/lib/netdev-windows.c
>+++ b/lib/netdev-windows.c
>@@ -394,7 +394,7 @@ netdev_windows_arp_lookup(const struct netdev *netdev,
>         return ENXIO;
>     }
>
>-    arp_table = (MIB_IPNETTABLE *) malloc(buffer_length);
>+    arp_table = (MIB_IPNETTABLE *) xmalloc(buffer_length);
>
>     if (arp_table == NULL) {
>         VLOG_ERR("Could not allocate memory for all the interfaces");
>@@ -443,7 +443,7 @@ netdev_windows_get_next_hop(const struct in_addr *host,
>         return ENXIO;
>     }
>
>-    all_addr = (IP_ADAPTER_ADDRESSES *) malloc(buffer_length);
>+    all_addr = (IP_ADAPTER_ADDRESSES *) xmalloc(buffer_length);
>
>     if (all_addr == NULL) {
>         VLOG_ERR("Could not allocate memory for all the interfaces");
>diff --git a/lib/wmi.c b/lib/wmi.c
>index dba8022..b560a7e 100644
>--- a/lib/wmi.c
>+++ b/lib/wmi.c
>@@ -406,7 +406,7 @@ delete_wmi_port(char *name)
>     wchar_t internal_port_query[WMI_QUERY_COUNT] = L"SELECT * from "
>         L"Msvm_EthernetPortAllocationSettingData  WHERE ElementName = \"" ;
>
>-    wide_name = malloc((strlen(name) + 1) * sizeof(wchar_t));
>+    wide_name = xmalloc((strlen(name) + 1) * sizeof(wchar_t));
>     if (wide_name == NULL) {
>         VLOG_WARN("Could not allocate memory for wide string");
>         retval = false;
>@@ -693,7 +693,7 @@ create_wmi_port(char *name) {
>     wchar_t internal_port_query[WMI_QUERY_COUNT] = L"SELECT * FROM "
>     L"Msvm_InternalEthernetPort WHERE ElementName = \"";
>
>-    wide_name = malloc((strlen(name) + 1) * sizeof(wchar_t));
>+    wide_name = xmalloc((strlen(name) + 1) * sizeof(wchar_t));
>     if (wide_name == NULL) {
>         VLOG_WARN("Could not allocate memory for wide string");
>         retval = false;
>--
>2.9.3.windows.2
>
>_______________________________________________
>dev mailing list
>dev@openvswitch.org
>https://urldefense.proofpoint.com/v2/url?u=https-3A__mail.openvswitch.org_mailman_listinfo_ovs-2Ddev&d=DwICAg&c=uilaK90D4TOVoH58JNXRgQ&r=Z6vowHUOjP5ysP_g372c49Nqc1vEKqHKNBkR5Q5Z7uo&m=A_SrOn7pygG6mPCwmqq6YGTrmTn8yCfvuSryqv25tqY&s=x4TwpFwUmcevmXKYYFaWo8XUR-0DNGsADZbfW6I_DU4&e=
Alin Serdean May 19, 2017, 9:51 p.m. UTC | #2
Thanks for the patch!

Xmalloc also check if the allocation was successful.

Mind also removing the checks for null afterwards?

Thanks,
Alin.

> -----Original Message-----
> From: ovs-dev-bounces@openvswitch.org [mailto:ovs-dev-
> bounces@openvswitch.org] On Behalf Of Shashank Ram
> Sent: Saturday, May 20, 2017 12:39 AM
> To: dev@openvswitch.org
> Subject: [ovs-dev] [PATCH] lib Windows: Use xmalloc instead of malloc
> 
> xmalloc checks if the size is valid before allocating memory.
> 
> Signed-off-by: Shashank Ram <rams@vmware.com>
> ---
>  lib/netdev-windows.c | 4 ++--
>  lib/wmi.c            | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/netdev-windows.c b/lib/netdev-windows.c index
> 375cb32..b4b39e9 100644
> --- a/lib/netdev-windows.c
> +++ b/lib/netdev-windows.c
> @@ -394,7 +394,7 @@ netdev_windows_arp_lookup(const struct netdev
> *netdev,
>          return ENXIO;
>      }
> 
> -    arp_table = (MIB_IPNETTABLE *) malloc(buffer_length);
> +    arp_table = (MIB_IPNETTABLE *) xmalloc(buffer_length);
> 
>      if (arp_table == NULL) {
>          VLOG_ERR("Could not allocate memory for all the interfaces"); @@ -
> 443,7 +443,7 @@ netdev_windows_get_next_hop(const struct in_addr
> *host,
>          return ENXIO;
>      }
[Alin Serdean] remove if
> 
> -    all_addr = (IP_ADAPTER_ADDRESSES *) malloc(buffer_length);
> +    all_addr = (IP_ADAPTER_ADDRESSES *) xmalloc(buffer_length);
> 
>      if (all_addr == NULL) {
>          VLOG_ERR("Could not allocate memory for all the interfaces");
[Alin Serdean] remove if
 diff --git
> a/lib/wmi.c b/lib/wmi.c index dba8022..b560a7e 100644
> --- a/lib/wmi.c
> +++ b/lib/wmi.c
> @@ -406,7 +406,7 @@ delete_wmi_port(char *name)
>      wchar_t internal_port_query[WMI_QUERY_COUNT] = L"SELECT * from "
>          L"Msvm_EthernetPortAllocationSettingData  WHERE ElementName = \""
> ;
> 
> -    wide_name = malloc((strlen(name) + 1) * sizeof(wchar_t));
> +    wide_name = xmalloc((strlen(name) + 1) * sizeof(wchar_t));
>      if (wide_name == NULL) {
>          VLOG_WARN("Could not allocate memory for wide string");
>          retval = false;
[Alin Serdean] remove if
> @@ -693,7 +693,7 @@ create_wmi_port(char *name) {
>      wchar_t internal_port_query[WMI_QUERY_COUNT] = L"SELECT * FROM "
>      L"Msvm_InternalEthernetPort WHERE ElementName = \"";
> 
> -    wide_name = malloc((strlen(name) + 1) * sizeof(wchar_t));
> +    wide_name = xmalloc((strlen(name) + 1) * sizeof(wchar_t));
>      if (wide_name == NULL) {
>          VLOG_WARN("Could not allocate memory for wide string");
>          retval = false;
[Alin Serdean] remove if
> --
> 2.9.3.windows.2
> 
> _______________________________________________
> dev mailing list
> dev@openvswitch.org
> https://mail.openvswitch.org/mailman/listinfo/ovs-dev
Shashank Ram May 19, 2017, 9:53 p.m. UTC | #3
Sure, will send out a v2.


Thanks,

Shashank
diff mbox

Patch

diff --git a/lib/netdev-windows.c b/lib/netdev-windows.c
index 375cb32..b4b39e9 100644
--- a/lib/netdev-windows.c
+++ b/lib/netdev-windows.c
@@ -394,7 +394,7 @@  netdev_windows_arp_lookup(const struct netdev *netdev,
         return ENXIO;
     }

-    arp_table = (MIB_IPNETTABLE *) malloc(buffer_length);
+    arp_table = (MIB_IPNETTABLE *) xmalloc(buffer_length);

     if (arp_table == NULL) {
         VLOG_ERR("Could not allocate memory for all the interfaces");
@@ -443,7 +443,7 @@  netdev_windows_get_next_hop(const struct in_addr *host,
         return ENXIO;
     }

-    all_addr = (IP_ADAPTER_ADDRESSES *) malloc(buffer_length);
+    all_addr = (IP_ADAPTER_ADDRESSES *) xmalloc(buffer_length);

     if (all_addr == NULL) {
         VLOG_ERR("Could not allocate memory for all the interfaces");
diff --git a/lib/wmi.c b/lib/wmi.c
index dba8022..b560a7e 100644
--- a/lib/wmi.c
+++ b/lib/wmi.c
@@ -406,7 +406,7 @@  delete_wmi_port(char *name)
     wchar_t internal_port_query[WMI_QUERY_COUNT] = L"SELECT * from "
         L"Msvm_EthernetPortAllocationSettingData  WHERE ElementName = \"" ;

-    wide_name = malloc((strlen(name) + 1) * sizeof(wchar_t));
+    wide_name = xmalloc((strlen(name) + 1) * sizeof(wchar_t));
     if (wide_name == NULL) {
         VLOG_WARN("Could not allocate memory for wide string");
         retval = false;
@@ -693,7 +693,7 @@  create_wmi_port(char *name) {
     wchar_t internal_port_query[WMI_QUERY_COUNT] = L"SELECT * FROM "
     L"Msvm_InternalEthernetPort WHERE ElementName = \"";

-    wide_name = malloc((strlen(name) + 1) * sizeof(wchar_t));
+    wide_name = xmalloc((strlen(name) + 1) * sizeof(wchar_t));
     if (wide_name == NULL) {
         VLOG_WARN("Could not allocate memory for wide string");
         retval = false;