* [Qemu-devel] [PATCH] xtensa: use g_malloc/g_new/g_free
@ 2017-05-08 7:35 Paolo Bonzini
2017-05-08 11:46 ` Philippe Mathieu-Daudé
2017-05-08 13:32 ` Alex Bennée
0 siblings, 2 replies; 4+ messages in thread
From: Paolo Bonzini @ 2017-05-08 7:35 UTC (permalink / raw)
To: qemu-devel; +Cc: Max Filippov, qemu-trivial
Replace malloc/free with g_malloc/g_free to get a program exit on
out of memory.
Replace g_malloc with g_new when allocating the MemoryRegion to
get more type safety.
Reported by Coverity.
Cc: Max Filippov <jcmvbkbc@gmail.com>
Cc: qemu-trivial@nongnu.org
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
hw/xtensa/sim.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/hw/xtensa/sim.c b/hw/xtensa/sim.c
index d2d1d3a6fd..97307728f7 100644
--- a/hw/xtensa/sim.c
+++ b/hw/xtensa/sim.c
@@ -41,13 +41,13 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
const char *name)
{
unsigned i;
- char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
+ char *num_name = g_malloc(strlen(name) + sizeof(i) * 3 + 1);
for (i = 0; i < memory->num; ++i) {
MemoryRegion *m;
sprintf(num_name, "%s%u", name, i);
- m = g_malloc(sizeof(*m));
+ m = g_new(MemoryRegion, 1);
memory_region_init_ram(m, NULL, num_name,
memory->location[i].size,
&error_fatal);
@@ -55,7 +55,7 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
memory_region_add_subregion(get_system_memory(),
memory->location[i].addr, m);
}
- free(num_name);
+ g_free(num_name);
}
static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
--
2.12.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] xtensa: use g_malloc/g_new/g_free
2017-05-08 7:35 [Qemu-devel] [PATCH] xtensa: use g_malloc/g_new/g_free Paolo Bonzini
@ 2017-05-08 11:46 ` Philippe Mathieu-Daudé
2017-05-08 13:32 ` Alex Bennée
1 sibling, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2017-05-08 11:46 UTC (permalink / raw)
To: Paolo Bonzini, qemu-devel; +Cc: qemu-trivial, Max Filippov
On 05/08/2017 04:35 AM, Paolo Bonzini wrote:
> Replace malloc/free with g_malloc/g_free to get a program exit on
> out of memory.
>
> Replace g_malloc with g_new when allocating the MemoryRegion to
> get more type safety.
>
> Reported by Coverity.
>
> Cc: Max Filippov <jcmvbkbc@gmail.com>
> Cc: qemu-trivial@nongnu.org
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
> hw/xtensa/sim.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/hw/xtensa/sim.c b/hw/xtensa/sim.c
> index d2d1d3a6fd..97307728f7 100644
> --- a/hw/xtensa/sim.c
> +++ b/hw/xtensa/sim.c
> @@ -41,13 +41,13 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
> const char *name)
> {
> unsigned i;
> - char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
> + char *num_name = g_malloc(strlen(name) + sizeof(i) * 3 + 1);
>
> for (i = 0; i < memory->num; ++i) {
> MemoryRegion *m;
>
> sprintf(num_name, "%s%u", name, i);
> - m = g_malloc(sizeof(*m));
> + m = g_new(MemoryRegion, 1);
> memory_region_init_ram(m, NULL, num_name,
> memory->location[i].size,
> &error_fatal);
> @@ -55,7 +55,7 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
> memory_region_add_subregion(get_system_memory(),
> memory->location[i].addr, m);
> }
> - free(num_name);
> + g_free(num_name);
> }
>
> static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] xtensa: use g_malloc/g_new/g_free
2017-05-08 7:35 [Qemu-devel] [PATCH] xtensa: use g_malloc/g_new/g_free Paolo Bonzini
2017-05-08 11:46 ` Philippe Mathieu-Daudé
@ 2017-05-08 13:32 ` Alex Bennée
2017-05-08 17:05 ` Max Filippov
1 sibling, 1 reply; 4+ messages in thread
From: Alex Bennée @ 2017-05-08 13:32 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: qemu-devel, qemu-trivial, Max Filippov
Paolo Bonzini <pbonzini@redhat.com> writes:
> Replace malloc/free with g_malloc/g_free to get a program exit on
> out of memory.
>
> Replace g_malloc with g_new when allocating the MemoryRegion to
> get more type safety.
>
> Reported by Coverity.
>
> Cc: Max Filippov <jcmvbkbc@gmail.com>
> Cc: qemu-trivial@nongnu.org
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> hw/xtensa/sim.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/hw/xtensa/sim.c b/hw/xtensa/sim.c
> index d2d1d3a6fd..97307728f7 100644
> --- a/hw/xtensa/sim.c
> +++ b/hw/xtensa/sim.c
> @@ -41,13 +41,13 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
> const char *name)
> {
> unsigned i;
> - char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
> + char *num_name = g_malloc(strlen(name) + sizeof(i) * 3 + 1);
It seems a little sketchy using the size of the storage type as a proxy
for the number of bytes used by the format specifier in a format string.
In this sort of situation I'd just use Glib and stop worrying about it:
GString num_name = g_string_new(NULL);
...
g_string_printf(num_name, "%s%u", name, i);
m = g_new(MemoryRegion, 1);
memory_region_init_ram(m, NULL, num_name->str,
memory->location[i].size,
&error_fatal);
...
g_string_free(name, TRUE);
>
> for (i = 0; i < memory->num; ++i) {
> MemoryRegion *m;
>
> sprintf(num_name, "%s%u", name, i);
> - m = g_malloc(sizeof(*m));
> + m = g_new(MemoryRegion, 1);
> memory_region_init_ram(m, NULL, num_name,
> memory->location[i].size,
> &error_fatal);
> @@ -55,7 +55,7 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
> memory_region_add_subregion(get_system_memory(),
> memory->location[i].addr, m);
> }
> - free(num_name);
> + g_free(num_name);
> }
>
> static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
--
Alex Bennée
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] xtensa: use g_malloc/g_new/g_free
2017-05-08 13:32 ` Alex Bennée
@ 2017-05-08 17:05 ` Max Filippov
0 siblings, 0 replies; 4+ messages in thread
From: Max Filippov @ 2017-05-08 17:05 UTC (permalink / raw)
To: Alex Bennée; +Cc: Paolo Bonzini, qemu-devel, QEMU Trivial
On Mon, May 8, 2017 at 6:32 AM, Alex Bennée <alex.bennee@linaro.org> wrote:
>> diff --git a/hw/xtensa/sim.c b/hw/xtensa/sim.c
>> index d2d1d3a6fd..97307728f7 100644
>> --- a/hw/xtensa/sim.c
>> +++ b/hw/xtensa/sim.c
>> @@ -41,13 +41,13 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
>> const char *name)
>> {
>> unsigned i;
>> - char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
>> + char *num_name = g_malloc(strlen(name) + sizeof(i) * 3 + 1);
>
> It seems a little sketchy using the size of the storage type as a proxy
> for the number of bytes used by the format specifier in a format string.
> In this sort of situation I'd just use Glib and stop worrying about it:
Ok, I'll send a patch doing it that way.
--
Thanks.
-- Max
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-05-08 17:06 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-05-08 7:35 [Qemu-devel] [PATCH] xtensa: use g_malloc/g_new/g_free Paolo Bonzini
2017-05-08 11:46 ` Philippe Mathieu-Daudé
2017-05-08 13:32 ` Alex Bennée
2017-05-08 17:05 ` Max Filippov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).