From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by sourceware.org (Postfix) with ESMTPS id 2099E3858286 for ; Fri, 4 Nov 2022 13:44:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2099E3858286 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-qk1-x730.google.com with SMTP id k26so3056161qkg.2 for ; Fri, 04 Nov 2022 06:44:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PqljkW5Gt+9XoJUg1yU15qhIpCcIQ9l9wZi0IX7wwp4=; b=EI7wFsjjaRsm0aLw/uSAZPQTGpj1Q0XHx1J4cFrpUZau0VBh9EjhioWDVnqRFaoM4R A0rIURBjWgAIrVIh+OuI62lho1Jcvwe0n0EWvdVJd9tc7xY5JGZ0z4OCplIGEfG7S+l2 mWSd+FXpVwa1x0rRh6MXI7JsfSVovR7V02ehOGDEl8fERtA/8bwqaPkOM4AlqK3CZNVR BFc818sQ0Mrx1mh/n0IT46PywK0jDvx3WhZrSDMeufWKcGsGi6Dr8ySN747Qd/1QSLbS QZiAPovqjssQFnnx7oBklg1pqlkOoZYdX6XXjuWnT9bM/S5+hRAzWZPRHWdL3HgVBDPy teUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PqljkW5Gt+9XoJUg1yU15qhIpCcIQ9l9wZi0IX7wwp4=; b=xtB8/TPQ19PFGdEL16OSbVy6nV2tY1NhUBZHjm3O62bxs4sa1k8O+nNp/P74DkMnco aHQApLAdTD8C8jh4VhcOs+mATRPO0SmUeU+u4rZuD8DRp4PxabmWV0mtw8PyGQqB8iA2 6JrL+sBAWvUjSLo9MLutNULP7JxHrRWd9iTKGQlBraBwKbDkrU/ZegZ7x7QRndbG0Rnk Mq4cF3tz66OpYxDpNP1fgofAcCcCr7nKSnJeuFhpFOIWyheIsogc3OSabQfp09fGbsL6 5ZrpBGuUZsK8543gLRd8IHwpoGEYWJv9wdbuJt09BlJKCzIJH9x5pI1b9YED28vzBlAU 3LMg== X-Gm-Message-State: ACrzQf2Ub+K9mi6gSrUhy37XYB0uZshS7Rn+Lz53Pj7up024M63RnYs8 +9hlMWjYZ0fRAD/EESfYjjar7WMs8fc= X-Google-Smtp-Source: AMsMyM4O663iBZAjDIl9OGYN+Wnd9G4oZwPJJ+CU68cvkPX3RTB6qNuweXEu25j3tIgzzKJid4AHpw== X-Received: by 2002:a37:6588:0:b0:6fa:3046:7f8b with SMTP id z130-20020a376588000000b006fa30467f8bmr19084106qkb.752.1667569483402; Fri, 04 Nov 2022 06:44:43 -0700 (PDT) Received: from localhost.localdomain (96-67-140-173-static.hfc.comcastbusiness.net. [96.67.140.173]) by smtp.gmail.com with ESMTPSA id ey21-20020a05622a4c1500b003988b3d5280sm2470577qtb.70.2022.11.04.06.44.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 06:44:42 -0700 (PDT) From: Lewis Hyatt To: gcc-patches@gcc.gnu.org Cc: Lewis Hyatt Subject: [PATCH 3/6] libcpp: Fix paste error with unknown pragma after macro expansion Date: Fri, 4 Nov 2022 09:44:11 -0400 Message-Id: <2222c8ff04699ae5671e1b654aafe5502259feaa.1667514153.git.lhyatt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: 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,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: In directives.cc, do_pragma() contains logic to handle a case such as the new testcase pragma-omp-unknown.c, where an unknown pragma was the result of macro expansion (for pragma namespaces that permit expansion). This no longer works correctly as shown by the testcase, fixed by adding PREV_WHITE to the flags on the second token to prevent an unwanted paste. Also fixed the memory leak, since the temporary tokens are pushed on their own context, nothing prevents freeing of the buffer that holds them when the context is eventually popped. libcpp/ChangeLog: * directives.cc (do_pragma): Fix memory leak in token buffer. Fix unwanted paste between two tokens. gcc/testsuite/ChangeLog: * c-c++-common/gomp/pragma-omp-unknown.c: New test. --- gcc/testsuite/c-c++-common/gomp/pragma-omp-unknown.c | 10 ++++++++++ libcpp/directives.cc | 10 +++++----- 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/gomp/pragma-omp-unknown.c diff --git a/gcc/testsuite/c-c++-common/gomp/pragma-omp-unknown.c b/gcc/testsuite/c-c++-common/gomp/pragma-omp-unknown.c new file mode 100644 index 00000000000..04881f786ab --- /dev/null +++ b/gcc/testsuite/c-c++-common/gomp/pragma-omp-unknown.c @@ -0,0 +1,10 @@ +/* { dg-do preprocess } */ +/* { dg-options "-fopenmp" } */ + +#define X UNKNOWN1 +#pragma omp X +/* { dg-final { scan-file pragma-omp-unknown.i "#pragma omp UNKNOWN1" } } */ + +#define Y UNKNOWN2 +_Pragma("omp Y") +/* { dg-final { scan-file pragma-omp-unknown.i "#pragma omp UNKNOWN2" } } */ diff --git a/libcpp/directives.cc b/libcpp/directives.cc index 918752f6b1f..9dc4363c65a 100644 --- a/libcpp/directives.cc +++ b/libcpp/directives.cc @@ -1565,15 +1565,15 @@ do_pragma (cpp_reader *pfile) { /* Invalid name comes from macro expansion, _cpp_backup_tokens won't allow backing 2 tokens. */ - /* ??? The token buffer is leaked. Perhaps if def_pragma hook - reads both tokens, we could perhaps free it, but if it doesn't, - we don't know the exact lifespan. */ - cpp_token *toks = XNEWVEC (cpp_token, 2); + const auto tok_buff = _cpp_get_buff (pfile, 2 * sizeof (cpp_token)); + const auto toks = (cpp_token *)tok_buff->base; toks[0] = ns_token; toks[0].flags |= NO_EXPAND; toks[1] = *token; - toks[1].flags |= NO_EXPAND; + toks[1].flags |= NO_EXPAND | PREV_WHITE; _cpp_push_token_context (pfile, NULL, toks, 2); + /* Arrange to free this buffer when no longer needed. */ + pfile->context->buff = tok_buff; } pfile->cb.def_pragma (pfile, pfile->directive_line); }