From: Thomas Huth <thuth@redhat.com>
To: Milica Lazarevic <milica.lazarevic@syrmia.com>
Cc: qemu-devel@nongnu.org, cfontana@suse.de, berrange@redhat.com,
pbonzini@redhat.com, vince.delvecchio@mediatek.com,
richard.henderson@linaro.org, peter.maydell@linaro.org,
djordje.todorovic@syrmia.com, mips32r2@gmail.com,
dragan.mladjenovic@syrmia.com
Subject: Re: [PATCH 13/20] disas/nanomips: Add free() calls
Date: Sat, 27 Aug 2022 09:45:54 +0200 [thread overview]
Message-ID: <974e6fb1-6873-9f9c-71ec-788324c3091c@redhat.com> (raw)
In-Reply-To: <20220815072629.12865-14-milica.lazarevic@syrmia.com>
On 15/08/2022 09.26, Milica Lazarevic wrote:
> The free() function is called for every string allocated using the
> strdup() function to prevent memory leaking.
>
> The implementation of the several functions working with dynamically
> allocated strings is slightly changed so we can free those strings.
>
> Almost every disassembly_function returns the result of the img_format()
> function, which returns a dynamically allocated string. To be able to
> free that string for every disassembly_function, a strdup() call is
> added for returning value of some disassembly functions like TLBGINV,
> TLBGINVF, TLBGP, etc.
>
> Signed-off-by: Milica Lazarevic <milica.lazarevic@syrmia.com>
> ---
> disas/nanomips.cpp | 1117 +++++++++++++++++++++++++++++++++-----------
> 1 file changed, 841 insertions(+), 276 deletions(-)
>
> diff --git a/disas/nanomips.cpp b/disas/nanomips.cpp
> index 561e4ff095..551bcb3164 100644
> --- a/disas/nanomips.cpp
> +++ b/disas/nanomips.cpp
> @@ -526,7 +526,9 @@ static const char *save_restore_list(uint64 rt, uint64 count, uint64 gp)
> for (uint64 counter = 0; counter != count; counter++) {
> bool use_gp = gp && (counter == count - 1);
> uint64 this_rt = use_gp ? 28 : ((rt & 0x10) | (rt + counter)) & 0x1f;
> - strcat(str, img_format(",%s", GPR(this_rt)));
> + const char *dis_str = img_format(",%s", GPR(this_rt));
> + strcat(str, dis_str);
> + free((char *)dis_str);
When using glib, you could get rid of the free() here by declaring dis_str
with g_autofree.
> }
>
> return strdup(str);
> @@ -663,7 +665,9 @@ static int Disassemble(const uint16 *data, char *dis,
> return -6;
> }
> type = table[i].type;
> - strcpy(dis, dis_fn(op_code));
> + const char *dis_str = dis_fn(op_code);
> + strcpy(dis, dis_str);
> + free((char *)dis_str);
dito
> return table[i].instructions_size;
> } else {
> strcpy(dis, "reserved instruction");
> @@ -1737,7 +1741,10 @@ static const char *ACLR(uint64 instruction)
> const char *s = IMMEDIATE(copy(s_value));
> const char *rs = GPR(copy(rs_value));
>
> - return img_format("ACLR %s, %s(%s)", bit, s, rs);
> + const char *ret = img_format("ACLR %s, %s(%s)", bit, s, rs);
> + free((char *)bit);
> + free((char *)s);
> + return ret;
> }
>
>
> @@ -1833,7 +1840,9 @@ static const char *ADDIU_32_(uint64 instruction)
> const char *rs = GPR(copy(rs_value));
> const char *u = IMMEDIATE(copy(u_value));
>
> - return img_format("ADDIU %s, %s, %s", rt, rs, u);
> + const char *ret = img_format("ADDIU %s, %s, %s", rt, rs, u);
> + free((char *)u);
I really dislike the need of having these "(char *)" casts in the calls to
free() everywhere.
IMHO, if a function allocated memory and the caller is required to free it,
the memory belongs to the caller after the function has finished, so it
would be better to return "char *" instead of "const char *" in these cases.
"const char *" should be used in the cases where the return value points to
static strings that the caller must not free.
(Apart from that, please consider to use g_autofree for the variables
declaration everywhere you added a free() here ... that would make this
patch way easier).
Thomas
next prev parent reply other threads:[~2022-08-27 7:48 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-15 7:26 [PATCH 0/20] Convert nanoMIPS disassembler from C++ to C Milica Lazarevic
2022-08-15 7:26 ` [PATCH 01/20] disas/nanomips: Remove namespace img Milica Lazarevic
2022-08-15 13:55 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 02/20] disas/nanomips: Extract enums out of the NMD class Milica Lazarevic
2022-08-15 13:59 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 03/20] disas/nanomips: Delete NMD class fields Milica Lazarevic
2022-08-15 10:59 ` Philippe Mathieu-Daudé via
2022-08-15 15:02 ` Richard Henderson
2022-08-15 7:26 ` [PATCH 04/20] disas/nanomips: Remove helper methods from class Milica Lazarevic
2022-08-15 16:41 ` Thomas Huth
2022-08-16 0:19 ` Philippe Mathieu-Daudé via
2022-08-16 5:46 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 05/20] disas/nanomips: Remove __cond " Milica Lazarevic
2022-08-15 11:07 ` Philippe Mathieu-Daudé via
2022-08-15 14:20 ` Thomas Huth
2022-08-16 0:14 ` Philippe Mathieu-Daudé via
2022-08-15 7:26 ` [PATCH 06/20] disas/nanomips: Remove disasm " Milica Lazarevic
2022-08-15 11:45 ` Philippe Mathieu-Daudé via
2022-08-15 7:26 ` [PATCH 07/20] disas/nanomips: Remove Pool tables from the class Milica Lazarevic
2022-08-16 0:12 ` Philippe Mathieu-Daudé via
2022-08-15 7:26 ` [PATCH 08/20] disas/nanomips: Remove NMD class Milica Lazarevic
2022-08-15 7:26 ` [PATCH 09/20] disas/nanomips: Move typedefs etc to nanomips.cpp Milica Lazarevic
2022-08-16 0:13 ` Philippe Mathieu-Daudé via
2022-08-15 7:26 ` [PATCH 10/20] disas/nanomips: Delete nanomips.h Milica Lazarevic
2022-08-16 0:14 ` Philippe Mathieu-Daudé via
2022-08-15 7:26 ` [PATCH 11/20] disas/nanomips: Remove #inlcude <sstream> Milica Lazarevic
2022-08-15 14:05 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 12/20] disas/nanomips: Replace std::string type Milica Lazarevic
2022-08-26 7:58 ` Milica Lazarevic
2022-08-27 7:38 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 13/20] disas/nanomips: Add free() calls Milica Lazarevic
2022-08-26 7:59 ` Milica Lazarevic
2022-08-27 7:45 ` Thomas Huth [this message]
2022-08-15 7:26 ` [PATCH 14/20] disas/nanomips: Remove function overloading Milica Lazarevic
2022-08-27 7:51 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 15/20] disas/nanomips: Replace exception handling Milica Lazarevic
2022-08-26 7:59 ` Milica Lazarevic
2022-08-27 8:34 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 16/20] disas/nanomips: Replace Cpp enums for C enums Milica Lazarevic
2022-08-27 8:35 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 17/20] disas/nanomips: Remove argument passing by ref Milica Lazarevic
2022-08-27 8:36 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 18/20] disas/nanomips: Add struct keyword Milica Lazarevic
2022-08-27 8:37 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 19/20] disas/nanomips: Add modifier static Milica Lazarevic
2022-08-16 0:22 ` Philippe Mathieu-Daudé via
2022-08-18 7:58 ` Milica Lazarevic
2022-08-27 8:38 ` Thomas Huth
2022-08-15 7:26 ` [PATCH 20/20] disas/nanomips: Rename nanomips.cpp to nanomips.c Milica Lazarevic
2022-08-27 8:39 ` Thomas Huth
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=974e6fb1-6873-9f9c-71ec-788324c3091c@redhat.com \
--to=thuth@redhat.com \
--cc=berrange@redhat.com \
--cc=cfontana@suse.de \
--cc=djordje.todorovic@syrmia.com \
--cc=dragan.mladjenovic@syrmia.com \
--cc=milica.lazarevic@syrmia.com \
--cc=mips32r2@gmail.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=vince.delvecchio@mediatek.com \
/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).