From patchwork Sat May 4 19:34:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Damianov X-Patchwork-Id: 1931377 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=disroot.org header.i=@disroot.org header.a=rsa-sha256 header.s=mail header.b=LWRfL8l2; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VWyZt6Nb5z1ybC for ; Sun, 5 May 2024 05:36:04 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3640C384404C for ; Sat, 4 May 2024 19:36:01 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from layka.disroot.org (layka.disroot.org [178.21.23.139]) by sourceware.org (Postfix) with ESMTPS id BE9AC3858C53 for ; Sat, 4 May 2024 19:35:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BE9AC3858C53 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=disroot.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=disroot.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BE9AC3858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=178.21.23.139 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714851344; cv=none; b=anW4JX0c+K1R2qVELt2Gv3pL8hwiJZUOSDmpHQ/ux56/On5VibQOkCnccSXMnm289Aon5MbkF+0rkInt7Tp+8qyJe0vvW0gI85Kw8VhDpD+Ap6D4248g7KnoRZi9nsaXHRV7S+fcwg2u/mZRLy1gdSuG4S6GH0nBYlB/d+/K8x4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714851344; c=relaxed/simple; bh=W2qkM3iw615cc3WALAfHf29BtsxVAl+1Y08MxDegdVw=; h=From:DKIM-Signature:To:Subject:Date:Message-Id:MIME-Version; b=egvd02wiZCKlc/Te9C0jOBgUUzExCm+5RG11L4RXrKlw2yXWdr7FO3UuMWdRXd7o1EMDipPuvS7g7/bTcxCKad+fuTsm9HemIcdZswpSY8JnrYLMpCWH5edZjGvh+TG4rsrfgRlLi6JrJrXhVo3FEQ8CnSd0h6tHFH3qDb0EcVY= ARC-Authentication-Results: i=1; server2.sourceware.org X-Virus-Scanned: SPAM Filter at disroot.org From: Peter Damianov DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1714851339; bh=W2qkM3iw615cc3WALAfHf29BtsxVAl+1Y08MxDegdVw=; h=From:To:Cc:Subject:Date; b=LWRfL8l2pTyvklpthB1daRlhkd+x5hu0gY80rEa4nVxbnYlNj4tuWrfQI6yEIYzHH NpYufgcfcIZX9fd4mS9qGDLZ3vlXQNu++/6hFf1t5z5To1NRcA/vupfgEqWn+WXk2z RFcDq3sEvP0JjKdNlIIP5A+cJKd/bkAKs8IfuTYyTFb8yEYlJfeE6MVNWBE7PabuBA O0CkgRanUXNO0TqgHFb6CVzzr9PCRTkpMXvueoH6a6+NcHqR9H2YkOtvtEiOzIvTQW qW1khQrJx3HrmWkeicn3WSob8jdlvG/0fAFMwQe4THtzhfE+9cmF8rxpQg62cGcZ7q 4bEdnfoVlvriA== To: gcc-patches@gcc.gnu.org Cc: Peter Damianov Subject: [PATCH] Driver: Reject output filenames with the same suffixes as source files [PR80182] Date: Sat, 4 May 2024 12:34:41 -0700 Message-Id: <20240504193439.20637-1-peter0x44@disroot.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Currently, commands like: gcc -o file.c -lm will delete the user's code. This patch checks the suffix of the output, and errors if the output ends in any of the suffixes listed in default_compilers. Unfortunately, I couldn't come up with a better heuristic to diagnose this case more specifically, so it is now not possible to directly make executables with said suffixes. I am unsure if any users are depending on this. PR driver/80182 * gcc.cc (process_command): fatal_error if the output has the suffix of a source file. (have_c): Change type to bool. (have_O): Change type to bool. (have_E): Change type to bool. (have_S): New global variable. (driver_handle_option): Assign have_S Signed-off-by: Peter Damianov --- gcc/gcc.cc | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/gcc/gcc.cc b/gcc/gcc.cc index 830a4700a87..53169c16460 100644 --- a/gcc/gcc.cc +++ b/gcc/gcc.cc @@ -2127,13 +2127,16 @@ static vec at_file_argbuf; static bool in_at_file = false; /* Were the options -c, -S or -E passed. */ -static int have_c = 0; +static bool have_c = false; /* Was the option -o passed. */ -static int have_o = 0; +static bool have_o = false; /* Was the option -E passed. */ -static int have_E = 0; +static bool have_E = false; + +/* Was the option -S passed. */ +static bool have_S = false; /* Pointer to output file name passed in with -o. */ static const char *output_file = 0; @@ -4593,6 +4596,10 @@ driver_handle_option (struct gcc_options *opts, have_E = true; break; + case OPT_S: + have_S = true; + break; + case OPT_x: spec_lang = arg; if (!strcmp (spec_lang, "none")) @@ -5058,6 +5065,22 @@ process_command (unsigned int decoded_options_count, output_file); } + /* Reject output file names that have the same suffix as a source + file. This is to catch mistakes like: gcc -o file.c -lm + that could delete the user's code. */ + if (have_o && output_file != NULL && !have_E && !have_S) + { + const char* filename = lbasename(output_file); + const char* suffix = strchr(filename, '.'); + if (suffix != NULL) + for (int i = 0; i < n_default_compilers; ++i) + if (!strcmp(suffix, default_compilers[i].suffix)) + fatal_error (input_location, + "output file suffix %qs could be a source file", + suffix); + } + + if (output_file != NULL && output_file[0] == '\0') fatal_error (input_location, "output filename may not be empty");