qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: Richard Henderson <richard.henderson@linaro.org>
Cc: qemu-devel@nongnu.org,
	jianchunfu <jianchunfu@cmss.chinamobile.com>,
	dgilbert@redhat.com, quintela@redhat.com
Subject: Re: [RFC] migration/dirtyrate: check malloc() return
Date: Mon, 11 Apr 2022 16:22:45 +0100	[thread overview]
Message-ID: <875ynf1w4m.fsf@linaro.org> (raw)
In-Reply-To: <c05d3c19-73d0-b16d-d1f1-dbb2fd817200@linaro.org>


Richard Henderson <richard.henderson@linaro.org> writes:

> On 4/9/22 20:58, jianchunfu wrote:
>> Handling potential memory allocation failures in dirtyrate.
>> Signed-off-by: jianchunfu <jianchunfu@cmss.chinamobile.com>
>> ---
>>   migration/dirtyrate.c | 8 ++++++++
>>   1 file changed, 8 insertions(+)
>> diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
>> index aace12a787..5dd40f32c8 100644
>> --- a/migration/dirtyrate.c
>> +++ b/migration/dirtyrate.c
>> @@ -523,9 +523,17 @@ static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config)
>>       }
>>         dirty_pages = malloc(sizeof(*dirty_pages) * nvcpu);
>> +    if (!dirty_pages) {
>> +        error_report("malloc dirty pages for vcpus failed.");
>> +        exit(1);
>> +    }
>>         DirtyStat.dirty_ring.nvcpu = nvcpu;
>>       DirtyStat.dirty_ring.rates = malloc(sizeof(DirtyRateVcpu) * nvcpu);
>> +    if (!DirtyStat.dirty_ring.rates) {
>> +        error_report("malloc dirty rates for vcpu ring failed.");
>> +        exit(1);
>> +    }
>
> You might as well use g_new(), which handles the sizeof and
> multiplication, and error reporting.

It will also assert if the alloc fails. If this is an allocation QEMU
can recover from then you need to use the try_new variants of the
g_malloc/new functions. However here we are exiting so no actual check
is needed as the g_malloc will exit for us.

-- 
Alex Bennée


      reply	other threads:[~2022-04-11 15:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-10  3:58 [RFC] migration/dirtyrate: check malloc() return jianchunfu
2022-04-10  6:00 ` Richard Henderson
2022-04-11 15:22   ` Alex Bennée [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875ynf1w4m.fsf@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=dgilbert@redhat.com \
    --cc=jianchunfu@cmss.chinamobile.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).