mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
daemon: fix cleanup of stringsbuf usages
Declare most of the stringsbuf as CLEANUP_FREE_STRINGSBUF, so they are freed completely on stack unwind: use take_stringsbuf() in return places to take away from the stringsbuf its content, and remove all the manual calls to free_stringslen (no more needed now). This requires to not use free_stringslen anymore on failure in the helper functions of stringsbuf, which now leave the content as-is (might be still useful even on error). This allows us to simplify the memory management of stringsbuf's, which are not properly fully freed, fixing memory leaks in some error paths (which were not calling free_stringslen).
This commit is contained in:
@@ -41,7 +41,7 @@ static char *read_whole_file (const char *filename);
|
||||
char **
|
||||
do_list_9p (void)
|
||||
{
|
||||
DECLARE_STRINGSBUF (r);
|
||||
CLEANUP_FREE_STRINGSBUF DECLARE_STRINGSBUF (r);
|
||||
|
||||
DIR *dir;
|
||||
|
||||
@@ -60,7 +60,7 @@ do_list_9p (void)
|
||||
if (end_stringsbuf (&r) == -1)
|
||||
return NULL;
|
||||
|
||||
return r.argv;
|
||||
return take_stringsbuf (&r);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
@@ -93,7 +93,6 @@ do_list_9p (void)
|
||||
/* Check readdir didn't fail */
|
||||
if (errno != 0) {
|
||||
reply_with_perror ("readdir: /sys/block");
|
||||
free_stringslen (r.argv, r.size);
|
||||
closedir (dir);
|
||||
return NULL;
|
||||
}
|
||||
@@ -101,7 +100,6 @@ do_list_9p (void)
|
||||
/* Close the directory handle */
|
||||
if (closedir (dir) == -1) {
|
||||
reply_with_perror ("closedir: /sys/block");
|
||||
free_stringslen (r.argv, r.size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -113,7 +111,7 @@ do_list_9p (void)
|
||||
if (end_stringsbuf (&r) == -1)
|
||||
return NULL;
|
||||
|
||||
return r.argv;
|
||||
return take_stringsbuf (&r);
|
||||
}
|
||||
|
||||
/* Read whole file into dynamically allocated array. If there is an
|
||||
|
||||
Reference in New Issue
Block a user