From patchwork Thu Jun 1 14:27:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 1789097 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=h2nAoH6Z; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QX7lt0PLVz20QB for ; Fri, 2 Jun 2023 00:28:26 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 075643857034 for ; Thu, 1 Jun 2023 14:28:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 075643857034 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685629704; bh=daop1qTq5BTcnBF6xWaNnAGldiMKotG1qiNWGBqNpwk=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=h2nAoH6ZXPMxwaVeEQrWKCRPO2X5ZnI5NgqcVluES4WAgv3qBMHpjOjuG5Pa0hZcE wiDarO+qRYSmTc6Kqfn5F6hwsFHwis6MnlIiAsP5SYn0VQYuo6cV6R7JtWwPp7mC0s HxU8gZMfrMeFbp5Q8qnRnWY1jdksYdyB0LOFiMwE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 9485B385840D for ; Thu, 1 Jun 2023 14:28:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9485B385840D Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-636-XU7WbzQyPmOH877eIwap_Q-1; Thu, 01 Jun 2023 10:28:06 -0400 X-MC-Unique: XU7WbzQyPmOH877eIwap_Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9FD1D1C060D9; Thu, 1 Jun 2023 14:28:05 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.193.180]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DC9048162; Thu, 1 Jun 2023 14:28:04 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v5 02/12] malloc/{memusage.c, memusagestat.c}: fix warn unused result Date: Thu, 1 Jun 2023 16:27:43 +0200 Message-Id: <20230601142747.104444-2-fberat@redhat.com> In-Reply-To: <20230418121130.844302-5-fberat@redhat.com> References: <20230601142747.104444-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Fr=C3=A9d=C3=A9ric_B=C3=A9rat_via_Libc-alpha?= From: Frederic Berat Reply-To: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in glibc. Reviewed-by: Siddhesh Poyarekar --- 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(-) 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;