libs/guest: Fix leak on realloc failure in backup_ptes()

From `man 2 realloc`:

  If realloc() fails, the original block is left untouched; it is not freed or moved.

Found using GCC -fanalyzer:

  |  184 |         backup->entries = realloc(backup->entries,
  |      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  |      |         |               | |
  |      |         |               | (91) when ‘realloc’ fails
  |      |         |               (92) ‘old_ptes.entries’ leaks here; was allocated at (44)
  |      |         (90) ...to here

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>
This commit is contained in:
Edwin Török authored and Andrew Cooper committed 2023-02-27 15:51:23 +00:00
1 parent 1868d7f226
commit 275d13184c
1 file changed
+8 -2
+8 -2
View File
@@ -181,10 +181,16 @@ static int backup_ptes(xen_pfn_t table_mfn, int offset,
if (backup->max == backup->cur)
{
backup->entries = realloc(backup->entries,
backup->max * 2 * sizeof(struct pte_backup_entry));
void *orig = backup->entries;
backup->entries = realloc(
orig, backup->max * 2 * sizeof(struct pte_backup_entry));
if (backup->entries == NULL)
{
free(orig);
return -1;
}
else
backup->max *= 2;
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.