From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gproxy1-pub.mail.unifiedlayer.com (gproxy1-pub.mail.unifiedlayer.com [69.89.25.95]) by sourceware.org (Postfix) with ESMTPS id 8A2C43858C52 for ; Tue, 4 Apr 2023 18:52:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8A2C43858C52 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 progateway3.mail.pro1.eigbox.com (Postfix) with ESMTP id CF6BD10048EB1 for ; Tue, 4 Apr 2023 18:52:23 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id jllXpz6tGzLqsjllXp5N6z; Tue, 04 Apr 2023 18:52:23 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=M+SIlw8s c=1 sm=1 tr=0 ts=642c71e7 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=dKHAf1wccvYA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=XhSvIBBxf0PDgfEFRH4A:9 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=q73OvMet6OQ6+m7Artw26XFq/70W0limB3avX4MplBc=; b=OCpjAhanQ0Gw38tr5wU2AngYcF xagj/fQ7bp0aECQ8yK+6H3oa8ZM8s/RFUeU/SN+a/pkJ3jv1U5F9jEAW6b++dFRTCp5ST2cRNIZ3T B1jjLCeqFpt4nRVx4+/e+8NH0; Received: from 71-211-185-113.hlrn.qwest.net ([71.211.185.113]:45862 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pjllX-001LI1-JR; Tue, 04 Apr 2023 12:52:23 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Use std::string in substitute_path_component Date: Tue, 4 Apr 2023 12:52:12 -0600 Message-Id: <20230404185212.1788207-1-tom@tromey.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: 71.211.185.113 X-Source-L: No X-Exim-ID: 1pjllX-001LI1-JR X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 71-211-185-113.hlrn.qwest.net (localhost.localdomain) [71.211.185.113]:45862 X-Source-Auth: tom+tromey.com X-Email-Count: 9 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3026.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,JMQ_SPF_NEUTRAL,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,TXREP 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: This changes substitute_path_component to use std::string, simplifying it quite a bit, and allowing for the removal of an xfree as well. Regression tested on x86-64 Fedora 36. --- gdb/auto-load.c | 16 +++++------ gdb/unittests/utils-selftests.c | 6 ++--- gdb/utils.c | 47 ++++++++++++--------------------- gdb/utils.h | 2 +- 4 files changed, 26 insertions(+), 45 deletions(-) diff --git a/gdb/auto-load.c b/gdb/auto-load.c index 40b05fdc634..6f1548c00c6 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -179,18 +179,14 @@ static std::vector> auto_load_safe_path_vec; static std::vector> auto_load_expand_dir_vars (const char *string) { - char *s = xstrdup (string); - substitute_path_component (&s, "$datadir", gdb_datadir.c_str ()); - substitute_path_component (&s, "$debugdir", debug_file_directory.c_str ()); + std::string s = string; + substitute_path_component (s, "$datadir", gdb_datadir.c_str ()); + substitute_path_component (s, "$debugdir", debug_file_directory.c_str ()); - if (debug_auto_load && strcmp (s, string) != 0) - auto_load_debug_printf ("Expanded $-variables to \"%s\".", s); + if (debug_auto_load && s != string) + auto_load_debug_printf ("Expanded $-variables to \"%s\".", s.c_str ()); - std::vector> dir_vec - = dirnames_to_char_ptr_vec (s); - xfree(s); - - return dir_vec; + return dirnames_to_char_ptr_vec (s.c_str ()); } /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */ diff --git a/gdb/unittests/utils-selftests.c b/gdb/unittests/utils-selftests.c index 70609aa4294..42424b709fe 100644 --- a/gdb/unittests/utils-selftests.c +++ b/gdb/unittests/utils-selftests.c @@ -30,10 +30,8 @@ test_substitute_path_component () auto test = [] (std::string s, const char *from, const char *to, const char *expected) { - char *temp = xstrdup (s.c_str ()); - substitute_path_component (&temp, from, to); - SELF_CHECK (strcmp (temp, expected) == 0); - xfree (temp); + substitute_path_component (s, from, to); + SELF_CHECK (s == expected); }; test ("/abc/$def/g", "abc", "xyz", "/xyz/$def/g"); diff --git a/gdb/utils.c b/gdb/utils.c index 6ec1cc0d48d..6cadf64a7d2 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3263,49 +3263,36 @@ parse_pid_to_attach (const char *args) return pid; } -/* Substitute all occurrences of string FROM by string TO in *STRINGP. *STRINGP - must come from xrealloc-compatible allocator and it may be updated. FROM - needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be - located at the start or end of *STRINGP. */ +/* Substitute all occurrences of string FROM by string TO in STRINGP. + STRINGP may be updated in place. FROM needs to be delimited by + IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be located at the start + or end of STRINGP). */ void -substitute_path_component (char **stringp, const char *from, const char *to) +substitute_path_component (std::string &stringp, const char *from, + const char *to) { - char *string = *stringp, *s; const size_t from_len = strlen (from); const size_t to_len = strlen (to); - for (s = string;;) + for (size_t pos = 0;;) { - s = strstr (s, from); - if (s == NULL) + pos = stringp.find (from, pos); + if (pos == std::string::npos) break; - if ((s == string || IS_DIR_SEPARATOR (s[-1]) - || s[-1] == DIRNAME_SEPARATOR) - && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len]) - || s[from_len] == DIRNAME_SEPARATOR)) + if ((pos == 0 || IS_DIR_SEPARATOR (stringp[pos - 1]) + || stringp[pos - 1] == DIRNAME_SEPARATOR) + && (pos + from_len == stringp.length () + || IS_DIR_SEPARATOR (stringp[pos + from_len]) + || stringp[pos + from_len] == DIRNAME_SEPARATOR)) { - char *string_new; - - string_new - = (char *) xrealloc (string, (strlen (string) + to_len + 1)); - - /* Relocate the current S pointer. */ - s = s - string + string_new; - string = string_new; - - /* Replace from by to. */ - memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1); - memcpy (s, to, to_len); - - s += to_len; + stringp.replace (pos, from_len, to); + pos += to_len; } else - s++; + ++pos; } - - *stringp = string; } #ifdef HAVE_WAITPID diff --git a/gdb/utils.h b/gdb/utils.h index a383036bcfe..bd1daac2e44 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -136,7 +136,7 @@ struct set_batch_flag_and_restore_page_info extern int gdb_filename_fnmatch (const char *pattern, const char *string, int flags); -extern void substitute_path_component (char **stringp, const char *from, +extern void substitute_path_component (std::string &stringp, const char *from, const char *to); std::string ldirname (const char *filename); -- 2.39.2