From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from donkey.ash.relay.mailchannels.net (donkey.ash.relay.mailchannels.net [23.83.222.49]) by sourceware.org (Postfix) with ESMTPS id AB7F9385700F for ; Thu, 1 Jun 2023 16:49:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB7F9385700F 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 1970A261036; Thu, 1 Jun 2023 16:49:19 +0000 (UTC) Received: from pdx1-sub0-mail-a202.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id A8267260C9B; Thu, 1 Jun 2023 16:49:18 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1685638158; a=rsa-sha256; cv=none; b=DjU9sAya4qNygMk+ohbJz3Cs5SsusdCXSRtIfXbYCiE7ZRXU41FF9zgP4/Bnia6t+gYv+5 AZ/NwoqoVtRGCzosrjzp/X8fT+mgwJhAivY2EFohHbul9NifCYOehKYbnjEjYsJbxqwtXd Bo4krFmrJL8p6rvmsHHJExr/E49zYlRvJJSjMqFVSmOdjFlL4fsDWBZ7FzSRpd0JscaVVT 5RADNn6/EvTj1beeja//nhbFGif71IzeSZEsVbsQ3TXrOXsXPqYCXrT8BmLCeQ43qkqRgQ qQrn8tKeTg+Y7EgapYDahmvsIAcJTww0y9rnHIhOWFtBn4ze2y4i5iApx4bDdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1685638158; 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=8PmjEaFHTHUii51xvKFmUqS1xtQmDfc9RcbfreE42QI=; b=1KPJE0jkCVonb7ksBmX2Q5UxuatodkQU+tLLA298tUhxFIVTBwQVnZxTxKCxNxzRyquQ4J uPieqh8duQLjauU4ffPkC5uBJ/5TyRWUWDgkjpeT3gYIlh3oREJfA9enUcqAoViVOMMRMy rSm4lFVjdMRw2R8I3OYSX6HDBKckTvTEtOVD/SOCroAbnwgU/XpQ/rz8FbiFAbKGPGEyqV JLhAnjY/g0+hHYxPnWMGiFuDMAwQd/YzvL5gAiaZhwI9Mz3CigeaWbYAITWjY49+A8s0Bn SNS01d231ZdeHXdcd+xhTcKtLI0+2S6cYKZIIkZp3diBp8UhkfxjaHeTEjBuKQ== ARC-Authentication-Results: i=1; rspamd-canary-5c954bb8c6-fbcc9; 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-Occur-Cold: 3de98fa72b553c4d_1685638158925_1941207812 X-MC-Loop-Signature: 1685638158925:4006543903 X-MC-Ingress-Time: 1685638158925 Received: from pdx1-sub0-mail-a202.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.126.30.34 (trex/6.8.1); Thu, 01 Jun 2023 16:49:18 +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 (2048 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a202.dreamhost.com (Postfix) with ESMTPSA id 4QXBtQ1V2czRq; Thu, 1 Jun 2023 09:49:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1685638158; bh=8PmjEaFHTHUii51xvKFmUqS1xtQmDfc9RcbfreE42QI=; h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding; b=qsde6DHGlPIGzS9A7YfPoydHmPldOYkLLiSYSsEuZbjwoK4Udsfy67GIBKFPfz8OA sgEk6PJjA+nTV+lrU9qlRoseMu4F3igAyPkQM9OzMxotvKy9hGj8zwjk4R4TvUzrMa FG9YFHABOUwuCVPjbU2rxx5q2HZgltfjECEibxjsNEf4pOcCCXW+mmkdTXAipN7xg6 wrN2U6DSXpTJG2KiqRhlv69CroMHsw4t70XSWolTWTe6IotgKGWO4ThUAFnOihcCai qRe2ehU6J+JpCkmUjPyn6URRFZLqkq5F3kAPh5j5DwUWhKaaQ1wC37+Vd+iZyBSncO ah/vYx9fatEFQ== Message-ID: Date: Thu, 1 Jun 2023 12:49:17 -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 v5 02/12] malloc/{memusage.c,memusagestat.c}: fix warn unused result Content-Language: en-US To: =?UTF-8?B?RnLDqWTDqXJpYyBCw6lyYXQ=?= , libc-alpha@sourceware.org References: <20230601142747.104444-1-fberat@redhat.com> <20230601142747.104444-2-fberat@redhat.com> From: Siddhesh Poyarekar In-Reply-To: <20230601142747.104444-2-fberat@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3037.4 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,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-06-01 10:27, Frédéric Bérat wrote: > Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in > glibc. > --- > Changes since v4: > - Mark strings for translation with gettext. > - Fixed typo in (ret == 0) case > > malloc/memusage.c | 54 +++++++++++++++++++++++++++++++++---------- > malloc/memusagestat.c | 48 ++++++++++++++++++++++++++++++++++---- > 2 files changed, 86 insertions(+), 16 deletions(-) > LGTM. You're moving write_all and read_all into a separate file later, so OK. Reviewed-by: Siddhesh Poyarekar > diff --git a/malloc/memusage.c b/malloc/memusage.c > index 2a3a508557..53a3630651 100644 > --- a/malloc/memusage.c > +++ b/malloc/memusage.c > @@ -18,7 +18,10 @@ > > #include > #include > +#include > +#include > #include > +#include > #include > #include > #include > @@ -142,6 +145,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, > + gettext ("write of %zu bytes failed after %td: %m"), > + length, p - (const char *) buffer); > + > + if (ret == 0) > + error (EXIT_FAILURE, 0, > + gettext ("write returned 0 after writing %td bytes of %zu"), > + p - (const char *) buffer, length); > + 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 +234,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 +324,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 +875,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..cc066d4af9 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, > + gettext ("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, > + gettext ("write of %zu bytes failed after %td: %m"), > + length, p - (const char *) buffer); > + > + if (ret == 0) > + error (EXIT_FAILURE, 0, > + gettext ("write returned 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;