{"id":2230108,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2230108/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/afHNRKVIJNarCva4@tucnak/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/1.1/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null},"msgid":"<afHNRKVIJNarCva4@tucnak>","date":"2026-04-29T09:20:04","name":"libcpp: Fix up ICE with __VA_OPT__ [PR125048]","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"8de6f8e0e2a62224c4e3e816d33a95be4d737b09","submitter":{"id":671,"url":"http://patchwork.ozlabs.org/api/1.1/people/671/?format=json","name":"Jakub Jelinek","email":"jakub@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/afHNRKVIJNarCva4@tucnak/mbox/","series":[{"id":502020,"url":"http://patchwork.ozlabs.org/api/1.1/series/502020/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=502020","date":"2026-04-29T09:20:04","name":"libcpp: Fix up ICE with __VA_OPT__ [PR125048]","version":1,"mbox":"http://patchwork.ozlabs.org/series/502020/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2230108/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2230108/checks/","tags":{},"headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=Lhe2EQOy;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (1024-bit key,\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=Lhe2EQOy","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=redhat.com","sourceware.org; spf=pass smtp.mailfrom=redhat.com","server2.sourceware.org;\n arc=none smtp.remote-ip=170.10.129.124"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g5BcF5hWBz1xqf\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 29 Apr 2026 19:20:43 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 86D184BB1C30\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 29 Apr 2026 09:20:41 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.129.124])\n by sourceware.org (Postfix) with ESMTP id 59A914BB1C2C\n for <gcc-patches@gcc.gnu.org>; Wed, 29 Apr 2026 09:20:11 +0000 (GMT)","from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com\n (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by\n relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n cipher=TLS_AES_256_GCM_SHA384) id us-mta-533-7KPc5m6zPoChdboVrniVrQ-1; Wed,\n 29 Apr 2026 05:20:09 -0400","from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest\n SHA256)\n (No client certificate requested)\n by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id A7D3F1800364\n for <gcc-patches@gcc.gnu.org>; Wed, 29 Apr 2026 09:20:08 +0000 (UTC)","from tucnak.zalov.cz (unknown [10.44.34.21])\n by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with\n ESMTPS\n id DFD08300019F; Wed, 29 Apr 2026 09:20:07 +0000 (UTC)","from tucnak.zalov.cz (localhost [127.0.0.1])\n by tucnak.zalov.cz (8.18.1/8.18.1) with ESMTPS id 63T9K5Ps391724\n (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT);\n Wed, 29 Apr 2026 11:20:05 +0200","(from jakub@localhost)\n by tucnak.zalov.cz (8.18.1/8.18.1/Submit) id 63T9K5ma391723;\n Wed, 29 Apr 2026 11:20:05 +0200"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 86D184BB1C30","OpenDKIM Filter v2.11.0 sourceware.org 59A914BB1C2C"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 59A914BB1C2C","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 59A914BB1C2C","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777454411; cv=none;\n b=k9Ls/DJNnKaFYSCxZTtet3AqWTDBOy/bwwnP+6B47nMgz2TssHugorS6aN/uCasaBf9fuWasl4zgFh/sK0WHXvohjD7L94TsNsBex8QTh0JiR/EJASd+hdMgwn7lAsxmn6ai/sDO0UOetrAMP5qIMurklyd5GInHe98NYtV8ZYI=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777454411; c=relaxed/simple;\n bh=u7m/6+vVqdBzypwAnol6bwKPtJuRUy1M7inNny6BKGw=;\n h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version;\n b=cU+YX+c/QhDbN3rY6xlEB5tChGRQtL213O3tTsAsOHC5hywGz6VsUsN+YsMhcAj2DTI/t+dZ2qkt9PEALspX0Tx0v4U1ljvrDU51kCcdc/rOKSWoNYaiJSr59V4iCUrLOs8ZX4R6XT4DUJrkQTAsA4c4niNyNmHSuhQI5V/mJW0=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1777454411;\n h=from:from:reply-to:reply-to:subject:subject:date:date:\n message-id:message-id:to:to:cc:cc:mime-version:mime-version:\n content-type:content-type; bh=ZKS347TpmWQi8kq4tfrB8Ecp44S8aOYsX8gK6eBXWZ8=;\n b=Lhe2EQOyXi4yQsRc3lr6KZ7VnBv5QLTzJUHpl8+cJOPcrHHuXVzKFmMkCErIVygoC/39hA\n AjT3P2s6VtqHoNcNg00v9gIj7OudiJu7PVoHFeBDK5J2xKKPlc0Cesl9t/FB7gbVRavSVZ\n XQzN48oGOGQdW70N/SJCTwBlZhcoI7A=","X-MC-Unique":"7KPc5m6zPoChdboVrniVrQ-1","X-Mimecast-MFC-AGG-ID":"7KPc5m6zPoChdboVrniVrQ_1777454408","Date":"Wed, 29 Apr 2026 11:20:04 +0200","From":"Jakub Jelinek <jakub@redhat.com>","To":"\"Joseph S. Myers\" <josmyers@redhat.com>,\n Marek Polacek <polacek@redhat.com>, Jason Merrill <jason@redhat.com>","Cc":"gcc-patches@gcc.gnu.org","Subject":"[PATCH] libcpp: Fix up ICE with __VA_OPT__ [PR125048]","Message-ID":"<afHNRKVIJNarCva4@tucnak>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.4.1 on 10.30.177.4","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"aD7RY_Xaa4aQeKbY3ps95pcU9IfwihJqqHOwX-3L7k0_1777454408","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Reply-To":"Jakub Jelinek <jakub@redhat.com>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"},"content":"Hi!\n\nThe following testcase ICEs, because we call linemap_enter_macro\nand remember what it returned and then in a loop sometimes call\nvaopt_state::update (src) which can call expand_arg which under the\nhood can reallocate the pfile->line_table->info_macro.maps array.\nAs linemap_enter_macro returns a pointer into that array, if the\narray is reallocated, we then dereference the freed memory.\n\nThe following patch fixes it by remembering the offset into the\npfile->line_table->info_macro.maps array and when we could have called\nexpand_arg, updates the map variable so that it is correct even after\npossible reallocation.\n\nBootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/16.2?\n\n2026-04-28  Jakub Jelinek  <jakub@redhat.com>\n\n\tPR preprocessor/125048\n\t* macro.cc (replace_args): Remember the position of map\n\tin the pfile->line_table->info_macro.maps array and if vostate\n\tis DROP, recompute map pointer as expand_arg call could have\n\treallocated the pfile->line_table->info_macro.maps array.\n\n\t* c-c++-common/cpp/va-opt-11.c: New test.\n\n\n\tJakub","diff":"--- libcpp/macro.cc.jj\t2026-03-27 10:17:21.000000000 +0100\n+++ libcpp/macro.cc\t2026-04-28 11:25:22.767163448 +0200\n@@ -1995,6 +1995,7 @@ replace_args (cpp_reader *pfile, cpp_has\n   location_t *virt_locs = NULL;\n   unsigned int exp_count;\n   const line_map_macro *map = NULL;\n+  size_t map_idx = 0;\n   int track_macro_exp;\n \n   /* First, fully macro-expand arguments, calculating the number of\n@@ -2105,6 +2106,7 @@ replace_args (cpp_reader *pfile, cpp_has\n       map = linemap_enter_macro (pfile->line_table, node,\n \t\t\t\t expansion_point_loc,\n \t\t\t\t num_macro_tokens);\n+      map_idx = map - pfile->line_table->info_macro.maps;\n     }\n   i = 0;\n   vaopt_state vaopt_tracker (pfile, macro->variadic, &args[macro->paramc - 1]);\n@@ -2226,6 +2228,11 @@ replace_args (cpp_reader *pfile, cpp_has\n \t\t\t\t\t NULL, 0);\n \t\t}\n \t    }\n+\t  else if (vostate == vaopt_state::DROP && map)\n+\t    /* For the DROP case vaopt_tracker.update (src) can call\n+\t       expand_arg and that can reallocate the maps, so need to\n+\t       update the map pointer.  */\n+\t    map = pfile->line_table->info_macro.maps + map_idx;\n \t  continue;\n \t}\n \n--- gcc/testsuite/c-c++-common/cpp/va-opt-11.c.jj\t2026-04-28 11:34:59.783177007 +0200\n+++ gcc/testsuite/c-c++-common/cpp/va-opt-11.c\t2026-04-28 11:34:52.220307897 +0200\n@@ -0,0 +1,13 @@\n+/* PR preprocessor/125048 */\n+/* { dg-do preprocess } */\n+/* { dg-options \"-std=c23\" { target c } } */\n+/* { dg-options \"-std=c++20\" { target c++ } } */\n+\n+#define A(...)B(B(B(B(__VA_ARGS__##__VA_OPT__()))))\n+#define B(...)C(C(C(C(__VA_ARGS__##__VA_OPT__()))))\n+#define C(...)D(D(D(D(__VA_ARGS__##__VA_OPT__()))))\n+#define D(...)E(E(E(E(__VA_ARGS__##__VA_OPT__()))))\n+#define E(...)__VA_ARGS__\n+#define F\n+A(F)\n+/* { dg-final { scan-file va-opt-11.i \"D\\\\\\(D\\\\\\(D\\\\\\(C\\\\\\(C\\\\\\(C\\\\\\(B\\\\\\(B\\\\\\(B\\\\\\(\\\\\\)\\\\\\)\\\\\\)\\\\\\)\\\\\\)\\\\\\)\\\\\\)\\\\\\)\\\\\\)\" } } */\n","prefixes":[]}