From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x829.google.com (mail-qt1-x829.google.com [IPv6:2607:f8b0:4864:20::829]) by sourceware.org (Postfix) with ESMTPS id C209A3858D1E for ; Sat, 14 Jan 2023 18:46:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C209A3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qt1-x829.google.com with SMTP id h21so21555614qta.12 for ; Sat, 14 Jan 2023 10:46:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=vyPAeaC3eoXGPSsGMy4eWEvK2CtPJKXqf22zauaH4/E=; b=Nd57NybmP1vjCAvhy4u7Mf7bK3u1VBASlC9vPEOXVI7uSv2SraJMwrC7kl748UiZeg ngBYxoRYsYLH1hYEyspmpdDtQIC0qM6vB1vFS1Lpc+STMXbOVXBo9pqmpopwHx9Y7ZEL 9JdBEy6Z4lk0C/8p3VEf0/DNFkupLeB2z8r6zgGj7GI1cKIorswZ/cYGH9QMJjdCI0gZ rbfgq9IleIPqseuqg5Xll7R1tNzYfILqhZXDn3pIS7TtnvT3NFha69YzkrkpW2zFxdam iG6LQTdA6pOu8yuS7KKrXVhiAdqIaTRTXQ3AD38SMdu57N9UCuc7SOa0S0hONcFSRXRu pp9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vyPAeaC3eoXGPSsGMy4eWEvK2CtPJKXqf22zauaH4/E=; b=jGkp93G3HY9X1yTNkuDcNrVd02De53BHQm70X0Jced8CLUbIXDES9KlhklxztXpgI0 nJUSkqdSqyV4s1E0sEj7hS6G0Boa1Vlh70uMFC0ftPsb6uWkRSec/jtxhjYcKl21GZWT ZA+MZxNdxHone2APfKaSUHkIFGgrXHCW/rU6JjtRXF05/tZs3ozI31k/1erHQwlgv/U4 5+9twfAq4NqKcrRzgKLJcSYHHW389BcAuI20/ultDZHBv9XTU7ykHA53iYHmA5sqxjvu y5s1iGWDf4vLletSHYz90BlETu0LEyHQVTy/O+kBgxh10HXPDnDBkG+x+8UMhn6V8s1C ezfw== X-Gm-Message-State: AFqh2ko2rtJBRypP7ciXC8IoSTSXM37yOApAe0ZKE79FPGcL+qQ3IjXY wTR43bFTiAqqmUWlOEeNF8YLc/CQinY= X-Google-Smtp-Source: AMrXdXsQPYcJsmEP4RrAssBm9pQgNq4tj3jreaJK6U7PPhatmLMstazMxfkrKZIwP1ysnaOwQiSSVQ== X-Received: by 2002:ac8:5a85:0:b0:3ab:b6cd:3758 with SMTP id c5-20020ac85a85000000b003abb6cd3758mr91393759qtc.54.1673722001016; Sat, 14 Jan 2023 10:46:41 -0800 (PST) Received: from localhost.localdomain (96-67-140-173-static.hfc.comcastbusiness.net. [96.67.140.173]) by smtp.gmail.com with ESMTPSA id bi1-20020a05620a318100b006fb0e638f12sm14971692qkb.4.2023.01.14.10.46.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Jan 2023 10:46:40 -0800 (PST) From: Lewis Hyatt To: gcc-patches@gcc.gnu.org Cc: Lewis Hyatt Subject: [PATCH] libcpp: Fix ICE on directive inside _Pragma() operator [PR67046] Date: Sat, 14 Jan 2023 13:46:24 -0500 Message-Id: X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3038.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,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: get__Pragma_string() in directives.cc is responsible for lexing the parens and the string argument from a _Pragma("...") operator. This function does not handle the case when the closing paren is not on the same line as the string; in that case, libcpp will by default reuse the token buffer it previously used for the string, so that the string token returned by get__Pragma_string() may be corrupted, as shown in the testcase. Fix using the existing keep_tokens mechanism that temporarily disables the reuse of token buffers. libcpp/ChangeLog: PR preprocessor/67046 * directives.cc (_cpp_do__Pragma): Increment pfile->keep_tokens to ensure the returned string token is valid. gcc/testsuite/ChangeLog: PR preprocessor/67046 * c-c++-common/cpp/pr67046.c: New test. --- Notes: Hello- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67046 This fixes an old ICE in libcpp that can happen when lexing the tokens from a _Pragma operator. Bootstrapped+tested on x86-64 Linux with no regressions. Please let me know if it's OK? Thanks... -Lewis gcc/testsuite/c-c++-common/cpp/pr67046.c | 10 ++++++++++ libcpp/directives.cc | 5 +++++ 2 files changed, 15 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/cpp/pr67046.c diff --git a/gcc/testsuite/c-c++-common/cpp/pr67046.c b/gcc/testsuite/c-c++-common/cpp/pr67046.c new file mode 100644 index 00000000000..f37f20c624e --- /dev/null +++ b/gcc/testsuite/c-c++-common/cpp/pr67046.c @@ -0,0 +1,10 @@ +/* { dg-do preprocess } */ + +_Pragma( +"message(\"msg\")" +) + +_Pragma( +"message(\"msg\")" +# +) diff --git a/libcpp/directives.cc b/libcpp/directives.cc index 9dc4363c65a..ffd262bce7d 100644 --- a/libcpp/directives.cc +++ b/libcpp/directives.cc @@ -1996,7 +1996,12 @@ destringize_and_run (cpp_reader *pfile, const cpp_string *in, int _cpp_do__Pragma (cpp_reader *pfile, location_t expansion_loc) { + /* Make sure we don't invalidate the string token, if the closing parenthesis + ended up on a different line. */ + ++pfile->keep_tokens; const cpp_token *string = get__Pragma_string (pfile); + --pfile->keep_tokens; + pfile->directive_result.type = CPP_PADDING; if (string)