From patchwork Fri Jan 27 07:00:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 49109 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp690481wrn; Thu, 26 Jan 2023 23:01:00 -0800 (PST) X-Google-Smtp-Source: AMrXdXtuDsNfH2RToBiWMzYI97DjbwGHcTSXW4Rs5T4MbXV/AHYRB+ZX1Z2OSpAlNVn0ny5t6BBA X-Received: by 2002:a17:907:8dca:b0:85f:5d72:1841 with SMTP id tg10-20020a1709078dca00b0085f5d721841mr43431735ejc.39.1674802860401; Thu, 26 Jan 2023 23:01:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674802860; cv=none; d=google.com; s=arc-20160816; b=hBhdn0M9KVTwgAl3EF0tUEx2NHj6nxBImM4qsbvj6cjb+LnRc+vBixFnIEMMe0erJL cfm3QoNmT8TaPAOzYLhdzqJ8RmvLZvJ51UGMhfgxQOH2PbW/c7SavU96IdvRo8la2wup q8U8QNejN/cXmUfTXpOSQzT1u6mIjDk0bBfN+MsacZpUEDeO1IIMEY3jJt7SfYjGg9YT gY3+B216ecYNJHj7I3bZ5ucT6ET0WCzaLK9RG3BdBjYs3iMTCfO9Kv/4gypQFKfSrw5E 1zZ3ed2DYpMsRUpG2MUkoJwaMAEOeuslm9QiwUArgkytu9NBVolUN0z+rlFdrhC0anO9 cslQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=nKtnoE4OYNZiSf3OMHXNQMd5yEK21KkeREeyHlyjQzE=; b=fZxT024YErozx9hsc3Q26AwlFUC/X7sB2Ba7RXc/9/xnmPjwQvcAyGxfiy0Nsxk/R0 P/GTDmiBuL/txJeu74XDtttnDb9UhNlVl5+TBUevbRg+7NPXWdU8WY/oZiZCGw/YT/et XPMsCwB1KsxdG3BoHKWZdT1asWrSA9+9gzxHcOo1A1Qnh0YjmqVAuGM/R8OMgvVM7Bqn HR5skQMwfiXX8BGoXP2d/1EI0iZXjWQ4U9/GH4mfy6F3MyPSQbV94tggCLkR6Y2XzeXl YctzF2MAD7goIwBjCg3E5zfjsFJ1zKXN6uAERVk05i+vdsC81f6MbhyCifbTP/FPX4JB gnzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=fEpqaXEL; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id gk5-20020a17090790c500b00873092fd3bdsi3537963ejb.746.2023.01.26.23.01.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 23:01:00 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=fEpqaXEL; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 667463858404 for ; Fri, 27 Jan 2023 07:00:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 667463858404 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1674802859; bh=nKtnoE4OYNZiSf3OMHXNQMd5yEK21KkeREeyHlyjQzE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fEpqaXELLC4h366l+4MOu347effqQ3RrB31s2UZIVTSGEMIprJzcP7ADPfKPIoVTw ZBZqhtbj3zAcCzSJC3RKA3FDmeQeoGIniehfTAbJqUhUgzuZTGsAu2B3itmrQPJmjy MPbnR7bNgqmRXy4PLGsIjHtjfLnFQqPPIZa8fjwg= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org 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 Received: by mail-pj1-x102e.google.com with SMTP id v6-20020a17090ad58600b00229eec90a7fso7795170pju.0 for ; Thu, 26 Jan 2023 23:00:50 -0800 (PST) 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-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 To: binutils@sourceware.org Subject: gas macro memory leaks Message-ID: MIME-Version: 1.0 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756158084058589329?= X-GMAIL-MSGID: =?utf-8?q?1756158084058589329?= 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);