From 687e612f9a56763d82c78ed75656896ce9cace17 Mon Sep 17 00:00:00 2001 From: George Melikov Date: Fri, 27 Jan 2017 22:10:10 +0300 Subject: [PATCH] Add realloc() success check in zpool_history_unpack() Correctly handle the unlikely case where the memory buffer cannot be resized. Reviewed-by: Brian Behlendorf Reviewed-by: Giuseppe Di Natale Signed-off-by: George Melikov Closes #5575 --- lib/libzfs/libzfs_pool.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/libzfs/libzfs_pool.c b/lib/libzfs/libzfs_pool.c index afc665f49..67db13401 100644 --- a/lib/libzfs/libzfs_pool.c +++ b/lib/libzfs/libzfs_pool.c @@ -3758,6 +3758,7 @@ zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, uint64_t reclen; nvlist_t *nv; int i; + void *tmp; while (bytes_read > sizeof (reclen)) { @@ -3777,8 +3778,14 @@ zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, /* add record to nvlist array */ (*numrecords)++; if (ISP2(*numrecords + 1)) { - *records = realloc(*records, + tmp = realloc(*records, *numrecords * 2 * sizeof (nvlist_t *)); + if (tmp == NULL) { + nvlist_free(nv); + (*numrecords)--; + return (ENOMEM); + } + *records = tmp; } (*records)[*numrecords - 1] = nv; } -- 2.40.0