From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from beige.elm.relay.mailchannels.net (beige.elm.relay.mailchannels.net [23.83.212.16]) by sourceware.org (Postfix) with ESMTPS id 2686D3858C2D for ; Thu, 25 May 2023 00:50:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2686D3858C2D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 5C6126C0B90; Thu, 25 May 2023 00:50:03 +0000 (UTC) Received: from pdx1-sub0-mail-a305.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id D75586C0ADD; Thu, 25 May 2023 00:50:02 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1684975802; a=rsa-sha256; cv=none; b=Z6QGMowpSd8vh9RcWs6La8cJSF9NlQ40XcwBbcDzYtdvRqfPA6B5pX2DpPfFTkHnaplXy5 XOg+pbjgDYQ0aCjlLFKSdzmrBL+0z4EVZYJ26W7AWt6VdqrDSeh4N/Zavvub//1fOUBb8Z MopaAOfLGVWKjl0iDD3uZPk8JUXwIZb/LlzYD6rk72hTlnwkO4Msi2C1Kpz8gJ/lN+Lm0g nhcB8XH5EefbLYQYhfSNhwinfsCjViMJwvj+2KxWKDIQg5cBLVTIxfBagLfMPe2tZb1Jws smP4znln3kMa+V/rPWt2qPXaYDjOXOPRU+n1YQoO83DTtRrB9p09U51w16h6vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1684975802; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ymGoypUNW3vw1cq/E9l7jrA52blJ6G+tA2NlpyBKg3w=; b=LT2WfZTaLgGTpmGTtMucI5KpoRtgBapcZI6st5f+OXGdSFIYkOj1SPdMsUE34L2kLRVKYF ItNCS4NFyPcE4RhL5pnhINIlJL/W3JDNQ2/G7IQWSYhhAg0HtNjHty1RlpDSq30EW9wUqu 9/2hEA6tiIpm5+sGiQLpmyNqxWy7okC3jB1CN1EipUFPH047GarHtJCmcdWauMXqRiIOMv KdQt/+1X7DPTiEkyZSfuSzt0H/2Q+56QHYhhSjDrRQXlqrwdCiRuEseOJgMeL/4MaoX7xU V8ULOf1s2kBmGCCszLP4/87pk3RrXLDwuIVntSLKODvoYkyBbpje2SdW5euXlw== ARC-Authentication-Results: i=1; rspamd-5cdf8fd7d9-r57sl; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Cooperative-Left: 6c2589ee1d146155_1684975803135_1193067849 X-MC-Loop-Signature: 1684975803135:2854628626 X-MC-Ingress-Time: 1684975803135 Received: from pdx1-sub0-mail-a305.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.126.30.61 (trex/6.8.1); Thu, 25 May 2023 00:50:03 +0000 Received: from [192.168.0.182] (bras-vprn-toroon4834w-lp130-02-142-113-138-85.dsl.bell.ca [142.113.138.85]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a305.dreamhost.com (Postfix) with ESMTPSA id 4QRTwp3L3jzBD; Wed, 24 May 2023 17:50:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1684975802; bh=ymGoypUNW3vw1cq/E9l7jrA52blJ6G+tA2NlpyBKg3w=; h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding; b=CeoWxxieIr2BcDgoRbTW2q744aHftnqT1ydfU2eLiucRLRyJbEHMevuXquWjGqE48 /E0XwQW+9bjH+ZpotSc46j5XseXRj44gPt50x9zjw7K+HY2eFKA3tMzSDpit8X3wAp zdn9lEPYgtZvO+9IS8wrM3vnkTy1Js3Mpd4ivaP9qXSDDRHyxW18uLTYOfy1K3w89s NfmMiz94yHq3kPtWYJCfTbtlRG6GWI5A54q1lGcxURyvUPwkU0hKTdWxVKrIqu9KGB udtdVLxogz1QjgJtSokN4YCwzp9bTLH5GX2FzFfypn+rYni5R8SiEjm36C/X0mrIlV 5Mkd7082g0WNg== Message-ID: Date: Wed, 24 May 2023 20:50:01 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v4 03/15] malloc/{memusage.c,memusagestat.c}: fix warn unused result Content-Language: en-US To: =?UTF-8?B?RnLDqWTDqXJpYyBCw6lyYXQ=?= , libc-alpha@sourceware.org References: <20230428122142.928135-1-fberat@redhat.com> <20230428122142.928135-4-fberat@redhat.com> From: Siddhesh Poyarekar In-Reply-To: <20230428122142.928135-4-fberat@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3037.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,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 List-Id: On 2023-04-28 08:21, Frédéric Bérat wrote: > Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in > glibc. > --- > malloc/memusage.c | 53 +++++++++++++++++++++++++++++++++---------- > malloc/memusagestat.c | 48 +++++++++++++++++++++++++++++++++++---- > 2 files changed, 85 insertions(+), 16 deletions(-) > > diff --git a/malloc/memusage.c b/malloc/memusage.c > index 2a3a508557..0c414b0e85 100644 > --- a/malloc/memusage.c > +++ b/malloc/memusage.c > @@ -18,6 +18,8 @@ > > #include > #include > +#include > +#include > #include > #include > #include > @@ -142,6 +144,27 @@ peak_atomic_max (_Atomic size_t *peak, size_t val) > while (! atomic_compare_exchange_weak (peak, &v, val)); > } > > +static void > +write_all (int fd, const void *buffer, size_t length) > +{ > + const char *p = buffer; > + const char *end = p + length; > + while (p < end) > + { > + ssize_t ret = write (fd, p, end - p); > + if (ret < 0) > + error (EXIT_FAILURE, errno, > + "write of %zu bytes failed after %td: %m", > + length, p - (const char *) buffer); > + > + if (ret == 0) > + error (EXIT_FAILURE, 0, > + "write return 0 after writing %td bytes of %zu", > + p - (const char *) buffer, length); Same as 1/15, this needs to be a translatable string. Also "write returned 0 after...". Also since this is repeated thrice, I wonder if it should get pulled out into its own header and included in memusage.c, memusagestat.c and gencat.c. > + p += ret; > + } > +} > + > /* Update the global data after a successful function call. */ > static void > update_data (struct header *result, size_t len, size_t old_len) > @@ -210,10 +233,11 @@ update_data (struct header *result, size_t len, size_t old_len) > gettime (&buffer[idx]); > > /* Write out buffer if it is full. */ > - if (idx + 1 == buffer_size) > - write (fd, buffer, buffer_size * sizeof (struct entry)); > - else if (idx + 1 == 2 * buffer_size) > - write (fd, &buffer[buffer_size], buffer_size * sizeof (struct entry)); > + if (idx + 1 == buffer_size || idx + 1 == 2 * buffer_size) > + { > + uint32_t write_size = buffer_size * sizeof (buffer[0]); > + write_all (fd, &buffer[idx + 1 - buffer_size], write_size); > + } > } > } > > @@ -299,8 +323,8 @@ me (void) > first.stack = 0; > gettime (&first); > /* Write it two times since we need the starting and end time. */ > - write (fd, &first, sizeof (first)); > - write (fd, &first, sizeof (first)); > + write_all (fd, &first, sizeof (first)); > + write_all (fd, &first, sizeof (first)); > > /* Determine the buffer size. We use the default if the > environment variable is not present. */ > @@ -850,24 +874,29 @@ dest (void) > if (fd != -1) > { > /* Write the partially filled buffer. */ > + struct entry *start = buffer; > + uint32_t write_cnt = buffer_cnt; > + > if (buffer_cnt > buffer_size) > - write (fd, buffer + buffer_size, > - (buffer_cnt - buffer_size) * sizeof (struct entry)); > - else > - write (fd, buffer, buffer_cnt * sizeof (struct entry)); > + { > + start = buffer + buffer_size; > + write_cnt = buffer_cnt - buffer_size; > + } > + > + write_all (fd, start, write_cnt * sizeof (buffer[0])); > > /* Go back to the beginning of the file. We allocated two records > here when we opened the file. */ > lseek (fd, 0, SEEK_SET); > /* Write out a record containing the total size. */ > first.stack = peak_total; > - write (fd, &first, sizeof (struct entry)); > + write_all (fd, &first, sizeof (first)); > /* Write out another record containing the maximum for heap and > stack. */ > first.heap = peak_heap; > first.stack = peak_stack; > gettime (&first); > - write (fd, &first, sizeof (struct entry)); > + write_all (fd, &first, sizeof (first)); > > /* Close the file. */ > close (fd); > diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c > index 67c5131f79..9fe6dba2d8 100644 > --- a/malloc/memusagestat.c > +++ b/malloc/memusagestat.c > @@ -114,6 +114,45 @@ static int time_based; > static int also_total = 0; > > > +static void > +read_all (int fd, void *buffer, size_t length) > +{ > + char *p = buffer; > + char *end = p + length; > + while (p < end) > + { > + ssize_t ret = read (fd, p, end - p); > + if (ret < 0) > + error (EXIT_FAILURE, errno, > + "read of %zu bytes failed after %td: %m", > + length, p - (char *) buffer); > + > + p += ret; > + } > +} > + > +static void > +write_all (int fd, const void *buffer, size_t length) > +{ > + const char *p = buffer; > + const char *end = p + length; > + while (p < end) > + { > + ssize_t ret = write (fd, p, end - p); > + if (ret < 0) > + error (EXIT_FAILURE, errno, > + "write of %zu bytes failed after %td: %m", > + length, p - (const char *) buffer); > + > + if (ret == 0) > + error (EXIT_FAILURE, 0, > + "write return 0 after writing %td bytes of %zu", > + p - (const char *) buffer, length); > + p += ret; > + } > +} > + > + > int > main (int argc, char *argv[]) > { > @@ -188,7 +227,7 @@ main (int argc, char *argv[]) > total = st.st_size / sizeof (struct entry) - 2; > > /* Read the administrative information. */ > - read (fd, headent, sizeof (headent)); > + read_all (fd, headent, sizeof (headent)); > maxsize_heap = headent[1].heap; > maxsize_stack = headent[1].stack; > maxsize_total = headent[0].stack; > @@ -220,7 +259,8 @@ main (int argc, char *argv[]) > > /* Write the computed values in the file. */ > lseek (fd, 0, SEEK_SET); > - write (fd, headent, 2 * sizeof (struct entry)); > + write_all (fd, headent, sizeof (headent)); > + > } > > if (also_total) > @@ -372,7 +412,7 @@ main (int argc, char *argv[]) > size_t new[2]; > uint64_t now; > > - read (fd, &entry, sizeof (entry)); > + read_all (fd, &entry, sizeof (entry)); > > now = ((uint64_t) entry.time_high) << 32 | entry.time_low; > > @@ -455,7 +495,7 @@ main (int argc, char *argv[]) > size_t xpos; > uint64_t now; > > - read (fd, &entry, sizeof (entry)); > + read_all (fd, &entry, sizeof (entry)); > > now = ((uint64_t) entry.time_high) << 32 | entry.time_low; > xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;