From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from progateway7-pub.mail.pro1.eigbox.com (gproxy5-pub.mail.unifiedlayer.com [67.222.38.55]) by sourceware.org (Postfix) with ESMTPS id 540853858D38 for ; Mon, 19 Jun 2023 17:51:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 540853858D38 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw14.mail.unifiedlayer.com (unknown [10.0.90.129]) by progateway7.mail.pro1.eigbox.com (Postfix) with ESMTP id BCA3210041F51 for ; Mon, 19 Jun 2023 17:51:25 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id BJ2Dq407HH78gBJ2DqIzLM; Mon, 19 Jun 2023 17:51:25 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=b753XvKx c=1 sm=1 tr=0 ts=6490959d a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10:nop_charset_1 a=of4jigFt-DYA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=XXUw3hCUlIL_i_SE1mUA:9 a=QEXdDO2ut3YA:10:nop_charset_2 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=g2XKhmXCf7Kw4IJoZI2DKw8wc46vP2BQdufZ5iAZ1vM=; b=T8v1GjUYaCgv2tIGLZRZSEsrTw uyXqwa1kU/TG8cMY40OUCL2n7+Pc+wZgY37mWKcDIB8i3RuWWcE9g/Kefilvnz7E51xtIuFUr0goM XSrJa/m2IuPsTVJXxe7Vev93S; Received: from 75-166-136-83.hlrn.qwest.net ([75.166.136.83]:49210 helo=[192.168.0.21]) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1qBJ2D-0019zn-Fd for gdb-patches@sourceware.org; Mon, 19 Jun 2023 11:51:25 -0600 From: Tom Tromey Date: Mon, 19 Jun 2023 11:51:22 -0600 Subject: [PATCH 1/3] Use std::string in linux-osdata.c MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230619-remove-some-realloc-v1-1-8bee74d5b934@tromey.com> References: <20230619-remove-some-realloc-v1-0-8bee74d5b934@tromey.com> In-Reply-To: <20230619-remove-some-realloc-v1-0-8bee74d5b934@tromey.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.136.83 X-Source-L: No X-Exim-ID: 1qBJ2D-0019zn-Fd X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-136-83.hlrn.qwest.net ([192.168.0.21]) [75.166.136.83]:49210 X-Source-Auth: tom+tromey.com X-Email-Count: 16 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3025.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,JMQ_SPF_NEUTRAL,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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: I found some code in linux-osdata that manually managed a string. Replacing this with std::string simplifies it. --- gdb/nat/linux-osdata.c | 51 +++++++++++++++----------------------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index d27ddc5de23..103b7ab26cd 100644 --- a/gdb/nat/linux-osdata.c +++ b/gdb/nat/linux-osdata.c @@ -133,55 +133,40 @@ command_from_pid (char *command, int maxlen, PID_T pid) command[maxlen - 1] = '\0'; /* Ensure string is null-terminated. */ } -/* Returns the command-line of the process with the given PID. The - returned string needs to be freed using xfree after use. */ +/* Returns the command-line of the process with the given PID. */ -static char * +static std::string commandline_from_pid (PID_T pid) { std::string pathname = string_printf ("/proc/%lld/cmdline", pid); - char *commandline = NULL; + std::string commandline; gdb_file_up f = gdb_fopen_cloexec (pathname, "r"); if (f) { - size_t len = 0; - while (!feof (f.get ())) { char buf[1024]; size_t read_bytes = fread (buf, 1, sizeof (buf), f.get ()); if (read_bytes) - { - commandline = (char *) xrealloc (commandline, len + read_bytes + 1); - memcpy (commandline + len, buf, read_bytes); - len += read_bytes; - } + commandline.append (buf, read_bytes); } - if (commandline) + if (!commandline.empty ()) { - size_t i; - /* Replace null characters with spaces. */ - for (i = 0; i < len; ++i) - if (commandline[i] == '\0') - commandline[i] = ' '; - - commandline[len] = '\0'; + for (char &c : commandline) + if (c == '\0') + c = ' '; } else { /* Return the command in square brackets if the command-line is empty. */ - commandline = (char *) xmalloc (32); - commandline[0] = '['; - command_from_pid (commandline + 1, 31, pid); - - len = strlen (commandline); - if (len < 31) - strcat (commandline, "]"); + char cmd[32]; + command_from_pid (cmd, 31, pid); + commandline = std::string ("[") + cmd + "]"; } } @@ -349,7 +334,6 @@ linux_xfer_osdata_processes () PID_T pid; uid_t owner; char user[UT_NAMESIZE]; - char *command_line; int *cores; int task_count; std::string cores_str; @@ -360,7 +344,7 @@ linux_xfer_osdata_processes () continue; sscanf (dp->d_name, "%lld", &pid); - command_line = commandline_from_pid (pid); + std::string command_line = commandline_from_pid (pid); if (get_process_owner (&owner, pid) == 0) user_from_uid (user, sizeof (user), owner); @@ -393,10 +377,8 @@ linux_xfer_osdata_processes () "", pid, user, - command_line ? command_line : "", + command_line.c_str (), cores_str.c_str()); - - xfree (command_line); } closedir (dirp); @@ -487,10 +469,9 @@ linux_xfer_osdata_processgroups () PID_T pid = entry.pid; PID_T pgid = entry.pgid; char leader_command[32]; - char *command_line; command_from_pid (leader_command, sizeof (leader_command), pgid); - command_line = commandline_from_pid (pid); + std::string command_line = commandline_from_pid (pid); string_xml_appendf (buffer, @@ -503,9 +484,7 @@ linux_xfer_osdata_processgroups () pgid, leader_command, pid, - command_line ? command_line : ""); - - xfree (command_line); + command_line.c_str ()); } } -- 2.39.2