From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id 961F53836E7B for ; Wed, 1 Jun 2022 00:10:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 961F53836E7B Received: by mail-pl1-x635.google.com with SMTP id d22so210334plr.9 for ; Tue, 31 May 2022 17:10:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=TzbcSPcFj5ofzwbhk+UmyE2DXvGbLntx8OyaiQnJ4I0=; b=nMDedO11B9Hs6D+Cm6SsU0ht/TIT9WlBvdmYxfC0JxQtmThYH+Dh03Ht7elKaIj44F x289qRTuOGjiXm7u6UL3CQ8gHA5h2MfgdpVTPtrvCxPdZew2qHC66ylKzjMV0g0+/y/l EoX0LfjlPTWdGDUvMKD2PPWtEatjJLJBJFGG3LMrvmUxkCq++Uk/DZf8RUwrIN8LWUzM GtzEcxHzpDE4EmWo9el0s1YdoseDhSXpqoRYAcXuZBMGzyfmWzjqZlq7b1XoGlxxu4JG fLKhj5b+LXnYNPUcgOfMSX/RmCrccWiRxt6YiLMym1AqSxl3iV2WSllFHWxH3QTmnAhm 0CkQ== X-Gm-Message-State: AOAM531cmv+4oophUJzeyD055fZ7+DrbUv6/KeZyNcjTG1EU9dDURQ44 uHYNApJkIN5m3oeHPGAnI9Zjt9O5X1TUr6KD6PMgYjj6g1A= X-Google-Smtp-Source: ABdhPJyyvk0ySOjAsR96dc2pVX5+Gnvl6ZSsaSwZNRhMSzRxuhauYKYZGFStT14bN/7JHMYa69yyBU69X6fK08A/AeI= X-Received: by 2002:a17:90b:314e:b0:1dc:d143:a15d with SMTP id ip14-20020a17090b314e00b001dcd143a15dmr31145983pjb.111.1654042252483; Tue, 31 May 2022 17:10:52 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: "H.J. Lu" Date: Tue, 31 May 2022 17:10:16 -0700 Message-ID: Subject: Re: use libiberty xmalloc in bfd/doc/chew.c To: Alan Modra Cc: Binutils Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3025.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, T_SCC_BODY_TEXT_LINE 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-List-Received-Date: Wed, 01 Jun 2022 00:10:56 -0000 On Mon, May 30, 2022 at 2:36 AM Alan Modra via Binutils wrote: > > Catch out-of-memory. > > * doc/chew.c: Include libibery.h. > (init_string_with_size, nextword): Replace malloc with xmalloc. > (newentry, add_to_definition): Likewise. > (catchar, catbuf): Replace realloc with xrealloc. > (add_intrinsic): Replace strdup with xstrdup. > * doc/local.mk (LIBIBERTY): Define. > (chew): Link against libiberty. > * Makefile.in: Regenerate. > > diff --git a/bfd/Makefile.in b/bfd/Makefile.in > index 741e08d603c..53cac75af0e 100644 > --- a/bfd/Makefile.in > +++ b/bfd/Makefile.in > @@ -1301,6 +1301,7 @@ doc_bfd_TEXINFOS = $(DOCFILES) doc/bfdsumm.texi > AM_MAKEINFOFLAGS = --no-split -I "$(srcdir)/doc" -I doc > TEXI2DVI = texi2dvi -I "$(srcdir)/doc" -I doc > MKDOC = doc/chew$(EXEEXT_FOR_BUILD) > +LIBIBERTY = ../libiberty/libiberty.a This breaks --enable-pgo-build since ../libiberty/libiberty.a is a host library compiled with -fprofile-generate, but chew is a build program. If we want to use libiberty.a in a build program, we need to build a libiberty.a for build. > # We can't replace these rules with an implicit rule, because > # makes without VPATH support couldn't find the .h files in `..'. > @@ -2487,7 +2488,7 @@ doc/chew.stamp: $(srcdir)/doc/chew.c doc/$(am__dirstamp) > $(AM_V_CCLD)$(CC_FOR_BUILD) -o doc/chw$$$$$(EXEEXT_FOR_BUILD) $(CFLAGS_FOR_BUILD) \ > $(LDFLAGS_FOR_BUILD) $(H_CFLAGS) \ > -I. -I$(srcdir) -Idoc -I$(srcdir)/../include -I$(srcdir)/../intl -I../intl \ > - $(srcdir)/doc/chew.c && \ > + $(srcdir)/doc/chew.c $(LIBIBERTY) && \ > $(SHELL) $(srcdir)/../move-if-change \ > doc/chw$$$$$(EXEEXT_FOR_BUILD) $(MKDOC) && \ > touch $@ > diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c > index 22921e68431..9722a89f0e6 100644 > --- a/bfd/doc/chew.c > +++ b/bfd/doc/chew.c > @@ -82,6 +82,7 @@ > Foo. */ > > #include "ansidecl.h" > +#include "libiberty.h" > #include > #include > #include > @@ -141,7 +142,7 @@ init_string_with_size (string_type *buffer, unsigned int size) > { > buffer->write_idx = 0; > buffer->size = size; > - buffer->ptr = (char *) malloc (size); > + buffer->ptr = xmalloc (size); > } > > static void > @@ -201,7 +202,7 @@ catchar (string_type *buffer, int ch) > if (buffer->write_idx == buffer->size) > { > buffer->size *= 2; > - buffer->ptr = (char *) realloc (buffer->ptr, buffer->size); > + buffer->ptr = xrealloc (buffer->ptr, buffer->size); > } > > buffer->ptr[buffer->write_idx++] = ch; > @@ -223,7 +224,7 @@ catbuf (string_type *buffer, char *buf, unsigned int len) > { > while (buffer->write_idx + len >= buffer->size) > buffer->size *= 2; > - buffer->ptr = (char *) realloc (buffer->ptr, buffer->size); > + buffer->ptr = xrealloc (buffer->ptr, buffer->size); > } > memcpy (buffer->ptr + buffer->write_idx, buf, len); > buffer->write_idx += len; > @@ -1102,7 +1103,7 @@ nextword (char *string, char **word) > } > } > > - *word = (char *) malloc (length + 1); > + *word = xmalloc (length + 1); > > dst = *word; > src = word_start; > @@ -1216,11 +1217,11 @@ perform (void) > dict_type * > newentry (char *word) > { > - dict_type *new_d = (dict_type *) malloc (sizeof (dict_type)); > + dict_type *new_d = xmalloc (sizeof (*new_d)); > new_d->word = word; > new_d->next = root; > root = new_d; > - new_d->code = (stinst_type *) malloc (sizeof (stinst_type)); > + new_d->code = xmalloc (sizeof (*new_d->code)); > new_d->code_length = 1; > new_d->code_end = 0; > return new_d; > @@ -1232,9 +1233,8 @@ add_to_definition (dict_type *entry, stinst_type word) > if (entry->code_end == entry->code_length) > { > entry->code_length += 2; > - entry->code = > - (stinst_type *) realloc ((char *) (entry->code), > - entry->code_length * sizeof (stinst_type)); > + entry->code = xrealloc (entry->code, > + entry->code_length * sizeof (*entry->code)); > } > entry->code[entry->code_end] = word; > > @@ -1244,7 +1244,7 @@ add_to_definition (dict_type *entry, stinst_type word) > void > add_intrinsic (char *name, void (*func) (void)) > { > - dict_type *new_d = newentry (strdup (name)); > + dict_type *new_d = newentry (xstrdup (name)); > add_to_definition (new_d, func); > add_to_definition (new_d, 0); > } > diff --git a/bfd/doc/local.mk b/bfd/doc/local.mk > index 931942f874c..8c6932802f6 100644 > --- a/bfd/doc/local.mk > +++ b/bfd/doc/local.mk > @@ -82,12 +82,14 @@ TEXI2DVI = texi2dvi -I "$(srcdir)/%D%" -I %D% > > MKDOC = %D%/chew$(EXEEXT_FOR_BUILD) > > +LIBIBERTY = ../libiberty/libiberty.a > + > $(MKDOC): %D%/chew.stamp ; @true > %D%/chew.stamp: $(srcdir)/%D%/chew.c %D%/$(am__dirstamp) > $(AM_V_CCLD)$(CC_FOR_BUILD) -o %D%/chw$$$$$(EXEEXT_FOR_BUILD) $(CFLAGS_FOR_BUILD) \ > $(LDFLAGS_FOR_BUILD) $(H_CFLAGS) \ > -I. -I$(srcdir) -I%D% -I$(srcdir)/../include -I$(srcdir)/../intl -I../intl \ > - $(srcdir)/%D%/chew.c && \ > + $(srcdir)/%D%/chew.c $(LIBIBERTY) && \ > $(SHELL) $(srcdir)/../move-if-change \ > %D%/chw$$$$$(EXEEXT_FOR_BUILD) $(MKDOC) && \ > touch $@ > > -- > Alan Modra > Australia Development Lab, IBM -- H.J.