aboutsummaryrefslogtreecommitdiff
path: root/gas/macro.c
diff options
context:
space:
mode:
authorAlan Modra <amodra@gmail.com>2023-01-27 10:31:56 +1030
committerAlan Modra <amodra@gmail.com>2023-01-27 15:38:52 +1030
commitc026360c7578b4599c289987981d9c9c80481e40 (patch)
tree695998bd2116183a34814b02cd44d44316f6674b /gas/macro.c
parent48afe8b710712bf131fadbfa3278e512a7034483 (diff)
downloadbinutils-c026360c7578b4599c289987981d9c9c80481e40.zip
binutils-c026360c7578b4599c289987981d9c9c80481e40.tar.gz
binutils-c026360c7578b4599c289987981d9c9c80481e40.tar.bz2
gas macro memory leaks
This tidies memory allocated for entries in macro_hash. Freeing the macro name requires a little restructuring of the define_macro interface due to the name being used in the error message, and exposed the fact that the name and other fields were not initialised by the iq2000 backend. There is also a fix for .macro .macro .endm .macro .macro .endm which prior to this patch reported mac.s:1: Warning: attempt to redefine pseudo-op `.macro' ignored mac.s:3: Error: Macro `.macro' was already defined rather than reporting the attempt to redefine twice. * macro.c (macro_del_f): New function. (macro_init): Use it when creating macro_hash. (free_macro): Free macro name too. (define_macro): Return the macro_entry, remove idx, file, line and namep params. Call as_where. Report errors here. Delete macro from macro_hash on attempt to redefined pseudo-op. (delete_macro): Don't call free_macro. * macro.h (define_macro): Update prototype. * read.c (s_macro): Adjust to suit. * config/tc-iq2000.c (iq2000_add_macro): Init all fields of macro_entry.
Diffstat (limited to 'gas/macro.c')
-rw-r--r--gas/macro.c46
1 files changed, 25 insertions, 21 deletions
diff --git a/gas/macro.c b/gas/macro.c
index ed1e9ef..763bafb 100644
--- a/gas/macro.c
+++ b/gas/macro.c
@@ -70,13 +70,23 @@ static size_t (*macro_expr) (const char *, size_t, sb *, offsetT *);
static int macro_number;
+static void free_macro (macro_entry *);
+
+static void
+macro_del_f (void *ent)
+{
+ string_tuple_t *tuple = ent;
+ free_macro ((macro_entry *) tuple->value);
+}
+
/* Initialize macro processing. */
void
macro_init (int alternate, int mri, int strip_at,
size_t (*exp) (const char *, size_t, sb *, offsetT *))
{
- macro_hash = str_htab_create ();
+ macro_hash = htab_create_alloc (16, hash_string_tuple, eq_string_tuple,
+ macro_del_f, notes_calloc, NULL);
macro_defined = 0;
macro_alternate = alternate;
macro_mri = mri;
@@ -664,34 +674,30 @@ free_macro (macro_entry *macro)
}
htab_delete (macro->formal_hash);
sb_kill (&macro->sub);
+ free ((char *) macro->name);
free (macro);
}
-/* Define a new macro. Returns NULL on success, otherwise returns an
- error message. If NAMEP is not NULL, *NAMEP is set to the name of
- the macro which was defined. */
+/* Define a new macro. */
-const char *
-define_macro (size_t idx, sb *in, sb *label,
- size_t (*get_line) (sb *),
- const char *file, unsigned int line,
- const char **namep)
+macro_entry *
+define_macro (sb *in, sb *label, size_t (*get_line) (sb *))
{
macro_entry *macro;
sb name;
+ size_t idx;
const char *error = NULL;
macro = XNEW (macro_entry);
sb_new (&macro->sub);
sb_new (&name);
- macro->file = file;
- macro->line = line;
+ macro->file = as_where (&macro->line);
macro->formal_count = 0;
macro->formals = 0;
macro->formal_hash = str_htab_create ();
- idx = sb_skip_white (idx, in);
+ idx = sb_skip_white (0, in);
if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
error = _("unexpected end of file in macro `%s' definition");
if (label != NULL && label->len != 0)
@@ -740,15 +746,16 @@ define_macro (size_t idx, sb *in, sb *label,
error = _("Macro `%s' was already defined");
}
- if (namep != NULL)
- *namep = macro->name;
-
if (!error)
macro_defined = 1;
else
- free_macro (macro);
+ {
+ as_bad_where (macro->file, macro->line, error, macro->name);
+ free_macro (macro);
+ macro = NULL;
+ }
- return error;
+ return macro;
}
/* Scan a token, and then skip KIND. */
@@ -1318,10 +1325,7 @@ delete_macro (const char *name)
macro = str_hash_find (macro_hash, copy);
if (macro != NULL)
- {
- free_macro (macro);
- str_hash_delete (macro_hash, copy);
- }
+ str_hash_delete (macro_hash, copy);
else
as_warn (_("Attempt to purge non-existing macro `%s'"), copy);
free (copy);