Skip to contentSkip to conversation

misc: Document qemu_find_file and fix memory leak in avr_load_firmware

PATCH In review Philippe Mathieu-Daudé 5 participants 17 messages Jul 14, 2020 – Jul 20, 2020

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index b674b20b1b..3572db3f44 100644
@@ -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
index 6b86e022f0..c9c3afe648 100644
@@ -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
Reviewed-by on v1 1/4
#2 lore raw

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.

---
 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 :|
#3 lore raw

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
index 979a403984..a96849dd90 100644
@@ -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
Reviewed-by on v1 2/4
#4 lore raw

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.

---
 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 :|
#5 lore raw

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
index d0142f29ac..d6a08259d3 100644
@@ -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
Reviewed-by on v1 3/4
#6 lore raw

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.

---
 include/qemu-common.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/qemu-common.h +5 -0
index d0142f29ac..d6a08259d3 100644
@@ -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

Reviewed-by on v1 3/4
#7 lore raw

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.

Quoted 33 lines
---
 include/qemu-common.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/qemu-common.h +14 -1
index d0142f29ac..d6a08259d3 100644
@@ -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

Reviewed-by on v1 3/4
#8 lore raw

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.

---
 include/qemu-common.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Quoted 6 lines
include/qemu-common.h +0 -0
index d0142f29ac..d6a08259d3 100644

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 :|
#9 lore raw

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.

---
 include/qemu-common.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Quoted 6 lines
include/qemu-common.h +0 -0
index d0142f29ac..d6a08259d3 100644

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.
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

#10 lore raw

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.

Fixes Coverity CID 1430449 (RESOURCE_LEAK)
Fixes 7dd8f6fde4 ('hw/avr: Add support for loading ELF/raw binaries')
---
 hw/avr/boot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/avr/boot.c +1 -1
index 6fbcde4061..151734f82d 100644
@@ -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
#11 lore raw

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')
---
 hw/avr/boot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/avr/boot.c +1 -1
index 6fbcde4061..151734f82d 100644
@@ -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;
Reviewed-by on v1 4/4
#12 lore raw

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')
---
 hw/avr/boot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/avr/boot.c +0 -0
index 6fbcde4061..151734f82d 100644

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 :|
#13 lore raw

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')
---
 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
index 6fbcde4061..151734f82d 100644
@@ -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.

TIL, thanks :)

Quoted 9 lines

So iff "= NULL" is added:

Regards,
Daniel

Reviewed-by on the v1 series
#14 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 19 lines · Philippe Mathieu-Daudé

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(-)

--
2.21.3

Signature · 2 lines
Best Regards,
Michael Rolnik
Tested-by
#15 lore raw

I mean I got the patch, built and then ran *make check-qtest-avr* and *make check-acceptance*

Quoted 30 lines · Michael Rolnik
Quoted 15 lines · Philippe Mathieu-Daudé

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(-)
Quoted 4 lines

--
2.21.3

--
Best Regards,
Michael Rolnik

Signature · 2 lines
Best Regards,
Michael Rolnik
#16 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 8 lines · Philippe Mathieu-Daudé

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()

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.