[dpdk-dev] net/virtio-user: check value returned from malloc

Message ID 1485399942-94705-1-git-send-email-jianfeng.tan@intel.com
State Accepted, archived
Delegated to: Yuanhan Liu
Headers show

Checks

Context Check Description
ci/Intel compilation success Compilation OK
ci/checkpatch success coding style OK

Commit Message

Jianfeng Tan Jan. 26, 2017, 3:05 a.m.
Value returned from malloc is not checked for errors before being used.
This patch fixes following coverity issue.

    static struct vhost_memory_kernel *
    prepare_vhost_memory_kernel(void)
    {
        ...
        vm = malloc(sizeof(struct vhost_memory_kernel) +
                    max_regions *
                    sizeof(struct vhost_memory_region));
        ...
    >>>     CID 140744:    (NULL_RETURNS)
    >>>     Dereferencing a null pointer "vm".
                mr = &vm->regions[k++];

Fixes: e3b434818bbb ("net/virtio-user: support kernel vhost")
Coverity issue: 140744

Signed-off-by: Jianfeng Tan <jianfeng.tan@intel.com>
---
 drivers/net/virtio/virtio_user/vhost_kernel.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Yuanhan Liu Jan. 28, 2017, 1:26 p.m. | #1
On Thu, Jan 26, 2017 at 03:05:42AM +0000, Jianfeng Tan wrote:
> Value returned from malloc is not checked for errors before being used.
> This patch fixes following coverity issue.
> 
>     static struct vhost_memory_kernel *
>     prepare_vhost_memory_kernel(void)
>     {
>         ...
>         vm = malloc(sizeof(struct vhost_memory_kernel) +
>                     max_regions *
>                     sizeof(struct vhost_memory_region));
>         ...
>     >>>     CID 140744:    (NULL_RETURNS)
>     >>>     Dereferencing a null pointer "vm".
>                 mr = &vm->regions[k++];
> 
> Fixes: e3b434818bbb ("net/virtio-user: support kernel vhost")
> Coverity issue: 140744
> 
> Signed-off-by: Jianfeng Tan <jianfeng.tan@intel.com>

Applied to dpdk-next-virtio.

	--yliu

Patch

diff --git a/drivers/net/virtio/virtio_user/vhost_kernel.c b/drivers/net/virtio/virtio_user/vhost_kernel.c
index 9b6637d..05aa6c6 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel.c
+++ b/drivers/net/virtio/virtio_user/vhost_kernel.c
@@ -114,6 +114,8 @@  prepare_vhost_memory_kernel(void)
 	vm = malloc(sizeof(struct vhost_memory_kernel) +
 		    max_regions *
 		    sizeof(struct vhost_memory_region));
+	if (!vm)
+		return NULL;
 
 	for (i = 0; i < RTE_MAX_MEMSEG; ++i) {
 		seg = &rte_eal_get_configuration()->mem_config->memseg[i];