Skip to contentSkip to conversation

Fixes for realloc() in vhost

PATCH Applied Tiwei Bie 2 participants 4 messages Jan 15, 2019 – Jan 17, 2019

lore
Share & tools
lore

Tiwei Bie

author
#0 lore raw

v2 cover Fixes for realloc() in vhost

Tiwei Bie (2):
vhost: fix memory leak on realloc failure examples/vhost: fix path allocation failure handling

 examples/vhost/main.c         | 8 ++++++++
 lib/librte_vhost/vhost_user.c | 3 +++
 2 files changed, 11 insertions(+)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v2 1/2 vhost: fix memory leak on realloc failure

When realloc() fails, the original block isn't freed.

Fixes e246896178e6 ("vhost: get guest/host physical address mappings")
---
 lib/librte_vhost/vhost_user.c | 3 +++
 1 file changed, 3 insertions(+)
lib/librte_vhost/vhost_user.c +3 -0
index 1843e032f..4a968ef70 100644
@@ -733,13 +733,16 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 		   uint64_t host_phys_addr, uint64_t size)
 {
 	struct guest_page *page, *last_page;
+	struct guest_page *old_pages;
 
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
+		old_pages = dev->guest_pages;
 		dev->guest_pages = realloc(dev->guest_pages,
 					dev->max_guest_pages * sizeof(*page));
 		if (!dev->guest_pages) {
 			RTE_LOG(ERR, VHOST_CONFIG, "cannot realloc guest_pages\n");
+			free(old_pages);
 			return -1;
 		}
 	}
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH v2 2/2 examples/vhost: fix path allocation failure handling

In reply to Tiwei Bie

Add the missing failure handling for path allocation, as realloc() may fail.

Fixes ad0eef4d2203 ("examples/vhost: support multiple socket files")

---
v2:
- Improve the title and add some explanations (Thomas);

 examples/vhost/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
examples/vhost/main.c +8 -0
index 645cf51e9..5e914f58e 100644
@@ -353,11 +353,19 @@ port_init(uint16_t port)
 static int
 us_vhost_parse_socket_path(const char *q_arg)
 {
+	char *old;
+
 	/* parse number string */
 	if (strnlen(q_arg, PATH_MAX) == PATH_MAX)
 		return -1;
 
+	old = socket_files;
 	socket_files = realloc(socket_files, PATH_MAX * (nb_sockets + 1));
+	if (socket_files == NULL) {
+		free(old);
+		return -1;
+	}
+
 	snprintf(socket_files + nb_sockets * PATH_MAX, PATH_MAX, "%s", q_arg);
 	nb_sockets++;
 
Signature · 1 line
2.17.1
#3 lore raw
In reply to Tiwei Bie
Quoted 8 lines · Tiwei Bie

Tiwei Bie (2):
vhost: fix memory leak on realloc failure examples/vhost: fix path allocation failure handling

  examples/vhost/main.c         | 8 ++++++++
  lib/librte_vhost/vhost_user.c | 3 +++
  2 files changed, 11 insertions(+)

Applied to dpdk-next-virtio/master.

Thanks,
Maxime