Skip to contentSkip to conversation

tools/libs/guest: assist gcc13's realloc analyzer

PATCH In review Olaf Hering 3 participants 3 messages Apr 19, 2023 – Apr 24, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/libs/guest: assist gcc13's realloc analyzer

gcc13 fails to track the allocated memory in backup_ptes:

xg_offline_page.c: In function 'backup_ptes':
xg_offline_page.c:191:13: error: pointer 'orig' may be used after 'realloc' [-Werror=use-after-free]
191 | free(orig);

Assist the analyzer by slightly rearranging the code:
In case realloc succeeds, the previous allocation is either extended or released internally. In case realloc fails, the previous allocation is left unchanged. Return an error in this case, the caller will release the currently allocated memory in its error path.

http://bugzilla.suse.com/show_bug.cgi?id=1210570

---
 tools/libs/guest/xg_offline_page.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
tools/libs/guest/xg_offline_page.c +6 -10
index ccd0299f0f..8f0a252417 100644
@@ -181,18 +181,14 @@ static int backup_ptes(xen_pfn_t table_mfn, int offset,
 
     if (backup->max == backup->cur)
     {
-        void *orig = backup->entries;
+        void *entries = realloc(backup->entries, backup->max * 2 *
+                                sizeof(struct pte_backup_entry));
 
-        backup->entries = realloc(
-            orig, backup->max * 2 * sizeof(struct pte_backup_entry));
-
-        if (backup->entries == NULL)
-        {
-            free(orig);
+        if (entries == NULL)
             return -1;
-        }
-        else
-            backup->max *= 2;
+
+        backup->entries = entries;
+        backup->max *= 2;
     }
 
     backup->entries[backup->cur].table_mfn = table_mfn;
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Olaf Hering

gcc13 fails to track the allocated memory in backup_ptes:

xg_offline_page.c: In function 'backup_ptes':
xg_offline_page.c:191:13: error: pointer 'orig' may be used after 'realloc' [-Werror=use-after-free]
191 | free(orig);

Assist the analyzer by slightly rearranging the code:
In case realloc succeeds, the previous allocation is either extended or released internally. In case realloc fails, the previous allocation is left unchanged. Return an error in this case, the caller will release the currently allocated memory in its error path.

Quoted 3 lines

http://bugzilla.suse.com/show_bug.cgi?id=1210570

Juergen

#2 lore raw
Quoted 14 lines · Juergen Gross
Quoted 12 lines · Olaf Hering

gcc13 fails to track the allocated memory in backup_ptes:

xg_offline_page.c: In function 'backup_ptes':
xg_offline_page.c:191:13: error: pointer 'orig' may be used after 'realloc' [-Werror=use-after-free]
191 | free(orig);

Assist the analyzer by slightly rearranging the code:
In case realloc succeeds, the previous allocation is either extended or released internally. In case realloc fails, the previous allocation is left unchanged. Return an error in this case, the caller will release the currently allocated memory in its error path.

Quoted 5 lines
Quoted 3 lines

http://bugzilla.suse.com/show_bug.cgi?id=1210570

Needed to build on Fedora 38.

Thanks,
Jason