misc: Document qemu_find_file and fix memory leak in avr_load_firmware
cover misc: Document qemu_find_file and fix memory leak in avr_load_firmware
Fix the memory leak reported by Coverity (CID 1430449).
Philippe Mathieu-Daudé (4):
qemu/osdep: Document os_find_datadir() return value qemu/osdep: Reword qemu_get_exec_dir() documentation
qemu-common: Document qemu_find_file() hw/avr/boot: Fix memory leak in avr_load_firmware()
include/qemu-common.h | 14 ++++++++++++++ include/qemu/osdep.h | 5 ++++- hw/avr/boot.c | 2 +- os-posix.c | 3 +++ os-win32.c | 7 ++++++- 5 files changed, 28 insertions(+), 3 deletions(-)
Signature · 1 line
2.21.3
PATCH 1/4 qemu/osdep: Document os_find_datadir() return value
Document os_find_datadir() returned data must be freed.
--- os-posix.c | 3 +++ os-win32.c | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-)
os-posix.c +3 -0
@@ -84,6 +84,9 @@ void os_setup_signal_handling(void)* Find a likely location for support files using the location of the binary. * When running from the build tree this will be "$bindir/../pc-bios". * Otherwise, this is CONFIG_QEMU_DATADIR. +* +* The caller must use g_free() to free the returned data when it is +* no longer required. */ char *os_find_datadir (void ) {
os-win32.c +6 -1
@@ -57,7 +57,12 @@ void os_setup_early_signal_handling(void)atexit (os_undo_timer_resolution); } -/* Look for support files in the same directory as the executable. */ +/* +* Look for support files in the same directory as the executable. +* +* The caller must use g_free() to free the returned data when it is +* no longer required. +*/ char *os_find_datadir (void ) {return qemu_get_exec_dir ();
Signature · 1 line
2.21.3
Re: [PATCH-for-5.1 1/4] qemu/osdep: Document os_find_datadir() return value
Quoted 7 lines · Philippe Mathieu-Daudé
Document
os_find_datadir()returned data must be freed.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- os-posix.c | 3 +++ os-win32.c | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH 2/4 qemu/osdep: Reword qemu_get_exec_dir() documentation
In reply to Philippe Mathieu-DaudéThis comment is confuse, reword it a bit.
--- include/qemu/osdep.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
include/qemu/osdep.h +4 -1
@@ -588,7 +588,10 @@ char *qemu_get_local_state_pathname(const char *relative_pathname);void qemu_init_exec_dir (const char *argv0);/* Get the saved exec dir. -* Caller needs to release the returned string by g_free() */ +* +* The caller is responsible for releasing the value returned with g_free() +* after use. +*/ char *qemu_get_exec_dir (void );/**
Signature · 1 line
2.21.3
Re: [PATCH-for-5.1 2/4] qemu/osdep: Reword qemu_get_exec_dir() documentation
Quoted 6 lines · Philippe Mathieu-Daudé
This comment is confuse, reword it a bit.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/qemu/osdep.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH 3/4 qemu-common: Document qemu_find_file()
In reply to Philippe Mathieu-DaudéDocument qemu_find_file(), in particular the returned value which must be freed.
--- include/qemu-common.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/qemu-common.h +14 -0
@@ -110,6 +110,20 @@ const char *qemu_get_vm_name(void);#define QEMU_FILE_TYPE_BIOS 0#define QEMU_FILE_TYPE_KEYMAP 1 +/** +* qemu_find_file: +* @type: QEMU_FILE_TYPE_BIOS (for BIOS, VGA BIOS) +* or QEMU_FILE_TYPE_KEYMAP (for keymaps). +* @name: File name +* +* Search for @name file in the data directories, either configured at +* build time (DATADIR) or registered with the -L command line option. +* +* The caller must use g_free() to free the returned data when it is +* no longer required. +* +* Returns: absolute path to the file or NULL on error. +*/ char *qemu_find_file (int type,const char *name);/* OS specific functions */
Signature · 1 line
2.21.3
Re: [PATCH-for-5.1 3/4] qemu-common: Document qemu_find_file()
Quoted 22 lines · Philippe Mathieu-Daudé
Document
qemu_find_file(), in particular the returned value which must be freed.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/qemu-common.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)include/qemu-common.h +5
-0@@ -110,6 +110,20 @@ const char *qemu_get_vm_name(void);#define QEMU_FILE_TYPE_BIOS 0#define QEMU_FILE_TYPE_KEYMAP 1 +/** +* qemu_find_file: +* @type: QEMU_FILE_TYPE_BIOS (for BIOS, VGA BIOS) +* or QEMU_FILE_TYPE_KEYMAP (for keymaps). +* @name: File name
include/qemu-common.h const char *qemu_get_vm_name(void); · line 127
Quoted 10 lines
include/qemu-common.h +9
-0+* +* Search for @name file in the data directories, either configured at +* build time (DATADIR) or registered with the -L command line option. +* +* The caller must use g_free() to free the returned data when it is +* no longer required. +* +* Returns: absolute path to the file or NULL on error. +*/ char *qemu_find_file (int type,const char *name);
thanks
-- PMM
Re: [PATCH-for-5.1 3/4] qemu-common: Document qemu_find_file()
In reply to Philippe Mathieu-DaudéQuoted 5 lines · Philippe Mathieu-Daudé <[email protected]> 于2020年7月15日周三 上午12:48写道:
Document
qemu_find_file(), in particular the returned value which must be freed.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 33 lines
--- include/qemu-common.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)include/qemu-common.h +14
-1@@ -110,6 +110,20 @@ const char *qemu_get_vm_name(void);#define QEMU_FILE_TYPE_BIOS 0#define QEMU_FILE_TYPE_KEYMAP 1 +/** +* qemu_find_file: +* @type: QEMU_FILE_TYPE_BIOS (for BIOS, VGA BIOS) +* or QEMU_FILE_TYPE_KEYMAP (for keymaps). +* @name: File name +* +* Search for @name file in the data directories, either configured at +* build time (DATADIR) or registered with the -L command line option. +* +* The caller must use g_free() to free the returned data when it is +* no longer required. +* +* Returns: absolute path to the file or NULL on error. +*/ char *qemu_find_file (int type,const char *name);/* OS specific functions */ --2.21.3
Re: [PATCH-for-5.1 3/4] qemu-common: Document qemu_find_file()
In reply to Philippe Mathieu-DaudéQuoted 7 lines · Philippe Mathieu-Daudé
Document
qemu_find_file(), in particular the returned value which must be freed.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/qemu-common.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
Quoted 6 lines
include/qemu-common.h +0
-0
include/qemu-common.h const char *qemu_get_vm_name(void); · line 117
Quoted 8 lines
include/qemu-common.h +5
-0@@ -110,6 +110,20 @@ const char *qemu_get_vm_name(void);#define QEMU_FILE_TYPE_BIOS 0#define QEMU_FILE_TYPE_KEYMAP 1 +/** +* qemu_find_file: +* @type: QEMU_FILE_TYPE_BIOS (for BIOS, VGA BIOS) +* or QEMU_FILE_TYPE_KEYMAP (for keymaps). +* @name: File name
s/File name/relative or absolute file name/
include/qemu-common.h const char *qemu_get_vm_name(void); · line 118
Quoted 1 line
include/qemu-common.h +1
-0+*
Insert:
"If @name exists on disk as an absolute path, or a path relative
to the current directory, then returns @name unchanged.
include/qemu-common.h const char *qemu_get_vm_name(void); · line 120
Quoted 2 lines
include/qemu-common.h +2
-0+* Search for @name file in the data directories, either configured at +* build time (DATADIR) or registered with the -L command line option.
s/Search/Otherwise searches/
include/qemu-common.h const char *qemu_get_vm_name(void); · line 125
Quoted 5 lines
include/qemu-common.h +5
-0+* +* The caller must use g_free() to free the returned data when it is +* no longer required. +* +* Returns: absolute path to the file or NULL on error.
It doesn't always return an absolute path. It can return a path relative to the currently working directory via the initial short-circuit access() check.
Returns: a path that can access @name, or NULL if no matching
file exists.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH-for-5.1 3/4] qemu-common: Document qemu_find_file()
Quoted 48 lines · Daniel P. Berrangé
Quoted 7 lines · Philippe Mathieu-Daudé
Document
qemu_find_file(), in particular the returned value which must be freed.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/qemu-common.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)Reviewed-by Daniel P. Berrangé <[email protected]>Quoted 6 lines
include/qemu-common.h +0
-0include/qemu-common.h const char *qemu_get_vm_name(void); · line 117
Quoted 8 lines
include/qemu-common.h +5
-0@@ -110,6 +110,20 @@ const char *qemu_get_vm_name(void);#define QEMU_FILE_TYPE_BIOS 0#define QEMU_FILE_TYPE_KEYMAP 1 +/** +* qemu_find_file: +* @type: QEMU_FILE_TYPE_BIOS (for BIOS, VGA BIOS) +* or QEMU_FILE_TYPE_KEYMAP (for keymaps). +* @name: File name s/File name/relative or absolute file name/
include/qemu-common.h const char *qemu_get_vm_name(void); · line 118
Quoted 1 line
include/qemu-common.h +1
-0+* Insert:
"If @name exists on disk as an absolute path, or a path relative
to the current directory, then returns @name unchanged.include/qemu-common.h const char *qemu_get_vm_name(void); · line 120
Quoted 2 lines
include/qemu-common.h +2
-0+* Search for @name file in the data directories, either configured at +* build time (DATADIR) or registered with the -L command line option. s/Search/Otherwise searches/
include/qemu-common.h const char *qemu_get_vm_name(void); · line 125
Quoted 5 lines
include/qemu-common.h +5
-0+* +* The caller must use g_free() to free the returned data when it is +* no longer required. +* +* Returns: absolute path to the file or NULL on error.
Quoted 7 lines
It doesn't always return an absolute path. It can return a path relative to the currently working directory via the initial short-circuit
access()check.Returns: a path that can access @name, or NULL if no matching
file exists.
Thanks for the corrections!
Quoted 4 lines
Regards,
Daniel
PATCH 4/4 hw/avr/boot: Fix memory leak in avr_load_firmware()
In reply to Philippe Mathieu-DaudéThe value returned by qemu_find_file() must be freed.
This fixes Coverity issue CID 1430449, which points out that the memory returned by qemu_find_file() is leaked.
--- hw/avr/boot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/avr/boot.c +1 -1
@@ -60,7 +60,7 @@ static const char *avr_elf_e_flags_to_cpu_type(uint32_t flags)bool avr_load_firmware (AVRCPU *cpu,MachineState *ms,MemoryRegion *program_mr,const char *firmware) { -const char *filename; + g_autofree char *filename;int bytes_loaded;uint64_t entry;uint32_t e_flags;
Signature · 1 line
2.21.3
Re: [PATCH-for-5.1 4/4] hw/avr/boot: Fix memory leak in avr_load_firmware()
ping?
Quoted 26 lines · Philippe Mathieu-Daudé
The value returned by
qemu_find_file()must be freed.This fixes Coverity issue CID 1430449, which points out that the memory returned by
qemu_find_file()is leaked.Fixes Coverity CID 1430449 (RESOURCE_LEAK)Fixes 7dd8f6fde4 ('hw/avr: Add support for loading ELF/raw binaries')Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/avr/boot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/avr/boot.c +1
-1@@ -60,7 +60,7 @@ static const char *avr_elf_e_flags_to_cpu_type(uint32_t flags)bool avr_load_firmware (AVRCPU *cpu,MachineState *ms,MemoryRegion *program_mr,const char *firmware) { -const char *filename; + g_autofree char *filename;int bytes_loaded;uint64_t entry;uint32_t e_flags;
Re: [PATCH-for-5.1 4/4] hw/avr/boot: Fix memory leak in avr_load_firmware()
In reply to Philippe Mathieu-DaudéQuoted 17 lines · Philippe Mathieu-Daudé
The value returned by
qemu_find_file()must be freed.This fixes Coverity issue CID 1430449, which points out that the memory returned by
qemu_find_file()is leaked.Fixes Coverity CID 1430449 (RESOURCE_LEAK)Fixes 7dd8f6fde4 ('hw/avr: Add support for loading ELF/raw binaries')Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/avr/boot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/avr/boot.c +0
-0
hw/avr/boot.c static const char *avr_elf_e_flags_to_cpu_type(uint32_t flags) · line 63
Quoted 5 lines
hw/avr/boot.c +1
-1@@ -60,7 +60,7 @@ static const char *avr_elf_e_flags_to_cpu_type(uint32_t flags)bool avr_load_firmware (AVRCPU *cpu,MachineState *ms,MemoryRegion *program_mr,const char *firmware) { -const char *filename; + g_autofree char *filename;
Any variable marked g_autofree or g_auto must always be initialized to NULL otherwise there's risk of free'ing uninitialized data. Even if currently safe, any later refactoring could turn it into a bug.
So iff "= NULL" is added:
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH-for-5.1 4/4] hw/avr/boot: Fix memory leak in avr_load_firmware()
Quoted 13 lines · Daniel P. Berrangé
Quoted 12 lines · Philippe Mathieu-Daudé
The value returned by
qemu_find_file()must be freed.This fixes Coverity issue CID 1430449, which points out that the memory returned by
qemu_find_file()is leaked.Fixes Coverity CID 1430449 (RESOURCE_LEAK)Fixes 7dd8f6fde4 ('hw/avr: Add support for loading ELF/raw binaries')Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/avr/boot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
hw/avr/boot.c static const char *avr_elf_e_flags_to_cpu_type(uint32_t flags) · line 63
Quoted 10 lines
hw/avr/boot.c +1
-1@@ -60,7 +60,7 @@ static const char *avr_elf_e_flags_to_cpu_type(uint32_t flags)bool avr_load_firmware (AVRCPU *cpu,MachineState *ms,MemoryRegion *program_mr,const char *firmware) { -const char *filename; + g_autofree char *filename;Any variable marked
g_autofreeorg_automust always be initialized to NULL otherwise there's risk of free'ing uninitialized data. Even if currently safe, any later refactoring could turn it into a bug.
TIL, thanks :)
Quoted 9 lines
So iff "= NULL" is added:
Reviewed-by Daniel P. Berrangé <[email protected]>Regards,
Daniel
Quoted 19 lines · Philippe Mathieu-Daudé
Fix the memory leak reported by Coverity (CID 1430449).
Philippe Mathieu-Daudé (4):
qemu/osdep: Documentos_find_datadir()return value qemu/osdep: Rewordqemu_get_exec_dir()documentation
qemu-common: Documentqemu_find_file()hw/avr/boot: Fix memory leak inavr_load_firmware()include/qemu-common.h | 14 ++++++++++++++ include/qemu/osdep.h | 5 ++++- hw/avr/boot.c | 2 +- os-posix.c | 3 +++ os-win32.c | 7 ++++++- 5 files changed, 28 insertions(+), 3 deletions(-)--
2.21.3
Signature · 2 lines
Best Regards, Michael Rolnik
Michael Rolnik
Tested-byI mean I got the patch, built and then ran *make check-qtest-avr* and *make check-acceptance*
Quoted 30 lines · Michael Rolnik
Reviewed-by Michael Rolnik <[email protected]>Quoted 15 lines · Philippe Mathieu-Daudé
Fix the memory leak reported by Coverity (CID 1430449).
Philippe Mathieu-Daudé (4):
qemu/osdep: Documentos_find_datadir()return value qemu/osdep: Rewordqemu_get_exec_dir()documentation
qemu-common: Documentqemu_find_file()hw/avr/boot: Fix memory leak inavr_load_firmware()include/qemu-common.h | 14 ++++++++++++++ include/qemu/osdep.h | 5 ++++- hw/avr/boot.c | 2 +- os-posix.c | 3 +++ os-win32.c | 7 ++++++- 5 files changed, 28 insertions(+), 3 deletions(-)Quoted 4 lines
--
2.21.3--
Best Regards,
Michael Rolnik
Signature · 2 lines
Best Regards, Michael Rolnik
Quoted 8 lines · Philippe Mathieu-Daudé
Fix the memory leak reported by Coverity (CID 1430449).
Philippe Mathieu-Daudé (4):
qemu/osdep: Documentos_find_datadir()return value qemu/osdep: Rewordqemu_get_exec_dir()documentation
qemu-common: Documentqemu_find_file()hw/avr/boot: Fix memory leak inavr_load_firmware()
Quoted 7 lines
include/qemu-common.h | 14 ++++++++++++++ include/qemu/osdep.h | 5 ++++- hw/avr/boot.c | 2 +- os-posix.c | 3 +++ os-win32.c | 7 ++++++- 5 files changed, 28 insertions(+), 3 deletions(-)
Thanks to the reviewers.
I addressed Daniel's review comments and will send a pull request once the testing succeeds.
Phil.