From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id 634253858D20 for ; Fri, 27 Jan 2023 07:00:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 634253858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-x102e.google.com with SMTP id v6-20020a17090ad58600b00229eec90a7fso7795170pju.0 for ; Thu, 26 Jan 2023 23:00:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=nKtnoE4OYNZiSf3OMHXNQMd5yEK21KkeREeyHlyjQzE=; b=hKVpIT01aJ1zSysBpWXRdEsk0O031EUooOeepyDlugbh7l8SIs2IP1c3NRf56g5HCW Wmh31/3sIncflSR1d27WSOCniDnRDCCId6CRoqh0+0glNzAKbGISPIvfE9MaLN4Prj4V mNnrIPFiDPlL1c1Wc6Ntfkp5uvJfoUhdtUlsnRp5nFdcpSH7kCBxSfX7piEwnfZzLODs ov6gQzEJG8nR9qPwrrjOST5mYsLyi2Hu+XlnrUkL7ZUQFG8jKRBzqgmj0TtwIMG6w0XI F5iCIdCN19l3xJLWQh/Zc9L3zboJul1UHdZjVEw7snonDlaWlzIOwndsmNH2j3UWloxh UGYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nKtnoE4OYNZiSf3OMHXNQMd5yEK21KkeREeyHlyjQzE=; b=rf0U5oBKMf8rqhezdyeZPQ8ehDc19T8aTj9DcPv9L8p239zv/tqGWMaI3p1tD6ewHm X+9Rp5KQSTHJtzy1M0AXoivvX9l08TRoMu1tYtP/r4k9CPKM/AEiwOVDziOf5YiO1Ssi VoiXsYubFU280o7MuCPnFcKnDmx3iRz7cnnyhAk9glTcEBwcfza7ghJHRXy8K+t8HBRg 2SU+XetLA7nDvO6aqRamEoRZoW0V9yoxVbOU/pmVcFYvRzmGc7CCAkxXN4a7wIwNX797 XHZfK2g+8lvIj8jFW3Xxq7axxeC7CT8p3qZtQHyMrB1GilQ581BV50O6GFAYaP9SAg54 nhkQ== X-Gm-Message-State: AFqh2kqOZwrEPdbjhk/Hn2arb0e5ccI8UwkRoJgvqFjbloRNj6NWSjHl SnnpjPUXqlwvZbxXkfQSKirmjJSv70o= X-Google-Smtp-Source: AMrXdXtta+x9LBRnJD1CDHR8l0QGyAWdTYbtggcZUMljEPBwi1+isHeQ37Irb4ZUHMdfMEweMa1snw== X-Received: by 2002:a05:6a20:d695:b0:af:9391:449 with SMTP id it21-20020a056a20d69500b000af93910449mr41755356pzb.45.1674802849017; Thu, 26 Jan 2023 23:00:49 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:fb44:1882:a135:67f]) by smtp.gmail.com with ESMTPSA id x3-20020a170902820300b0019600b78487sm2090449pln.33.2023.01.26.23.00.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 23:00:48 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 6072E114037D; Fri, 27 Jan 2023 17:30:46 +1030 (ACDT) Date: Fri, 27 Jan 2023 17:30:46 +1030 From: Alan Modra To: binutils@sourceware.org Subject: gas macro memory leaks Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3035.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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. diff --git a/gas/config/tc-iq2000.c b/gas/config/tc-iq2000.c index f5d7c90d0d7..4e169727b84 100644 --- a/gas/config/tc-iq2000.c +++ b/gas/config/tc-iq2000.c @@ -229,15 +229,14 @@ iq2000_add_macro (const char * name, const char ** arguments) { macro_entry *macro; - sb macro_name; - const char *namestr; macro = XNEW (macro_entry); + macro->name = xstrdup (name); sb_new (& macro->sub); - sb_new (& macro_name); - macro->formal_count = 0; macro->formals = 0; + macro->formal_hash = str_htab_create (); + macro->file = as_where (¯o->line); sb_add_string (& macro->sub, semantics); @@ -245,9 +244,6 @@ iq2000_add_macro (const char * name, { formal_entry ** p = ¯o->formals; - macro->formal_count = 0; - macro->formal_hash = str_htab_create (); - while (*arguments != NULL) { formal_entry *formal; @@ -261,7 +257,7 @@ iq2000_add_macro (const char * name, /* chlm: Added the following to allow defaulted args. */ if (strchr (*arguments,'=')) { - char * tt_args = strdup (*arguments); + char * tt_args = xstrdup (*arguments); char * tt_dflt = strchr (tt_args,'='); *tt_dflt = 0; @@ -283,9 +279,7 @@ iq2000_add_macro (const char * name, } } - sb_add_string (¯o_name, name); - namestr = sb_terminate (¯o_name); - str_hash_insert (macro_hash, namestr, macro, 1); + str_hash_insert (macro_hash, macro->name, macro, 1); macro_defined = 1; } diff --git a/gas/macro.c b/gas/macro.c index ed1e9ef605a..763bafbdc3d 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 (¯o->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 (¯o->sub); sb_new (&name); - macro->file = file; - macro->line = line; + macro->file = as_where (¯o->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", ¯o->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); diff --git a/gas/macro.h b/gas/macro.h index 5593281c552..2ec0ec451d5 100644 --- a/gas/macro.h +++ b/gas/macro.h @@ -88,8 +88,7 @@ extern void macro_init (int, int, int, extern void macro_end (void); extern void macro_set_alternate (int); extern void macro_mri_mode (int); -extern const char *define_macro (size_t, sb *, sb *, size_t (*) (sb *), - const char *, unsigned int, const char **); +extern macro_entry *define_macro (sb *, sb *, size_t (*) (sb *)); extern int check_macro (const char *, sb *, const char **, macro_entry **); extern void delete_macro (const char *); extern const char *expand_irp (int, size_t, sb *, sb *, size_t (*) (sb *)); diff --git a/gas/read.c b/gas/read.c index 8fb11e8a67c..c5a477f9e08 100644 --- a/gas/read.c +++ b/gas/read.c @@ -2647,13 +2647,8 @@ void s_macro (int ignore ATTRIBUTE_UNUSED) { char *eol; - const char * file; - unsigned int line; sb s; - const char *err; - const char *name; - - file = as_where (&line); + macro_entry *macro; eol = find_end_of_line (input_line_pointer, 0); sb_build (&s, eol - input_line_pointer); @@ -2664,19 +2659,18 @@ s_macro (int ignore ATTRIBUTE_UNUSED) { sb label; size_t len; + const char *name; name = S_GET_NAME (line_label); len = strlen (name); sb_build (&label, len); sb_add_buffer (&label, name, len); - err = define_macro (0, &s, &label, get_macro_line_sb, file, line, &name); + macro = define_macro (&s, &label, get_macro_line_sb); sb_kill (&label); } else - err = define_macro (0, &s, NULL, get_macro_line_sb, file, line, &name); - if (err != NULL) - as_bad_where (file, line, err, name); - else + macro = define_macro (&s, NULL, get_macro_line_sb); + if (macro != NULL) { if (line_label != NULL) { @@ -2686,14 +2680,16 @@ s_macro (int ignore ATTRIBUTE_UNUSED) } if (((NO_PSEUDO_DOT || flag_m68k_mri) - && str_hash_find (po_hash, name) != NULL) + && str_hash_find (po_hash, macro->name) != NULL) || (!flag_m68k_mri - && *name == '.' - && str_hash_find (po_hash, name + 1) != NULL)) - as_warn_where (file, - line, - _("attempt to redefine pseudo-op `%s' ignored"), - name); + && macro->name[0] == '.' + && str_hash_find (po_hash, macro->name + 1) != NULL)) + { + as_warn_where (macro->file, macro->line, + _("attempt to redefine pseudo-op `%s' ignored"), + macro->name); + str_hash_delete (macro_hash, macro->name); + } } sb_kill (&s); -- Alan Modra Australia Development Lab, IBM