gas: ginsn: remove unnecessary buffer allocation and free

A previous commit 80ec235 fixed the memory leaks, but brought to light
that the code should ideally make consistent use of snprintf and not
allocate/free more buffers than necessary.

gas/
	* ginsn.c (ginsn_dst_print): Use snprintf consistently.
This commit is contained in:
Indu Bhagat
2024-05-21 12:59:55 -07:00
parent e63b1d5950
commit 002ccda0ef

View File

@@ -497,28 +497,25 @@ ginsn_src_print (struct ginsn_src *src)
static char* static char*
ginsn_dst_print (struct ginsn_dst *dst) ginsn_dst_print (struct ginsn_dst *dst)
{ {
int str_size = 0;
size_t len = GINSN_LISTING_OPND_LEN; size_t len = GINSN_LISTING_OPND_LEN;
char *dst_str = XNEWVEC (char, len); char *dst_str = XNEWVEC (char, len);
memset (dst_str, 0, len); memset (dst_str, 0, len);
if (dst->type == GINSN_DST_REG) if (dst->type == GINSN_DST_REG)
{ str_size = snprintf (dst_str, GINSN_LISTING_OPND_LEN,
char *buf = XNEWVEC (char, 32); "%%r%d", ginsn_get_dst_reg (dst));
sprintf (buf, "%%r%d", ginsn_get_dst_reg (dst));
strcat (dst_str, buf);
free (buf);
}
else if (dst->type == GINSN_DST_INDIRECT) else if (dst->type == GINSN_DST_INDIRECT)
{ str_size = snprintf (dst_str, GINSN_LISTING_OPND_LEN,
char *buf = XNEWVEC (char, 32); "[%%r%d+%lld]", ginsn_get_dst_reg (dst),
sprintf (buf, "[%%r%d+%lld]", ginsn_get_dst_reg (dst), (long long int) ginsn_get_dst_disp (dst));
(long long int) ginsn_get_dst_disp (dst)); else if (dst->type != GINSN_DST_UNKNOWN)
strcat (dst_str, buf); /* Other dst types are unexpected. */
free (buf); gas_assert (false);
}
gas_assert (strlen (dst_str) < GINSN_LISTING_OPND_LEN); /* str_size will remain 0 when GINSN_DST_UNKNOWN. */
gas_assert (str_size >= 0 && str_size < GINSN_LISTING_OPND_LEN);
return dst_str; return dst_str;
} }