[{"id":3680000,"web_url":"http://patchwork.ozlabs.org/comment/3680000/","msgid":"<aee5FYRL_PWbz9iL@redhat.com>","list_archive_url":null,"date":"2026-04-21T17:51:17","subject":"Re: [PATCH] c-family: Fix ICE with counted_by attribute [PR124969]","submitter":{"id":14370,"url":"http://patchwork.ozlabs.org/api/people/14370/","name":"Marek Polacek","email":"polacek@redhat.com"},"content":"On Tue, Apr 21, 2026 at 07:28:22PM +0200, Jakub Jelinek wrote:\n> Hi!\n> \n> The following valid testcase ICEs, because we try to use\n> TREE_CODE (NULL_TREE).\n> We document that counted_by is supported on pointers to void\n> and that it behaves like the GNU pointer to void arithmetics\n> extension in that case.  build_access_with_size_for_counted_by\n> already uses 1 in that case as element_size.\n> \n> The following patch fixes it, plus for error recovery punts\n> if it is a pointer to incomplete type other than void (pointer\n> arithmetics on such type will be diagnosed as error later on).\n> \n> Ok for trunk if it passes full bootstrap/regtest?\n> \n> 2026-04-21  Jakub Jelinek  <jakub@redhat.com>\n> \n> \tPR sanitizer/124969\n> \t* c-ubsan.cc (get_index_from_pointer_addr_expr): For\n> \tVOID_TYPE_P (pointee_type) use size_one_node instead of\n> \tTYPE_SIZE_UNIT (pointee_type) as pointee_size.  Punt if\n> \tpointee_size is NULL_TREE.  Formatting fix.\n> \n> \t* gcc.dg/ubsan/pr124969-1.c: New test.\n> \n> --- gcc/c-family/c-ubsan.cc.jj\t2026-03-27 10:17:13.716337560 +0100\n> +++ gcc/c-family/c-ubsan.cc\t2026-04-21 19:02:54.261275681 +0200\n> @@ -741,7 +741,11 @@ get_index_from_pointer_addr_expr (tree p\n>    /* Get the pointee type of the call to .ACCESS_WITH_SIZE.\n>       This should be the element type of the pointer array.  */\n>    tree pointee_type = TREE_TYPE (TREE_TYPE (call));\n> -  tree pointee_size = TYPE_SIZE_UNIT (pointee_type);\n> +  tree pointee_size;\n> +  if (VOID_TYPE_P (pointee_type))\n> +    pointee_size = size_one_node;\n> +  else\n> +    pointee_size = TYPE_SIZE_UNIT (pointee_type);\n>  \n>    tree index_exp = TREE_OPERAND (pointer, 1);\n>    *index_p = pointer;\n> @@ -750,6 +754,8 @@ get_index_from_pointer_addr_expr (tree p\n>    if (!(TREE_CODE (index_exp) != MULT_EXPR\n>  \t&& tree_int_cst_equal (pointee_size, integer_one_node)))\n>      {\n> +      if (pointee_size == NULL_TREE)\n> +\treturn NULL_TREE;\n\nI won't insist, but could this instead be\n\n  if (!COMPLETE_OR_VOID_TYPE_P (pointee_type))\n    return NULL_TREE;\n\nabove?\n\n>        while (CONVERT_EXPR_CODE_P (TREE_CODE (index_exp)))\n\nNot your problem, but this is CONVERT_EXPR_P.\n\n>  \t{\n>  \t  *index_p = index_exp;\n> @@ -760,7 +766,7 @@ get_index_from_pointer_addr_expr (tree p\n>  \t\t\t\t\t index_n, pointee_size);\n>  \n>        if (!index_exp)\n> -      return NULL_TREE;\n> +\treturn NULL_TREE;\n>      }\n>  \n>    while (CONVERT_EXPR_CODE_P (TREE_CODE (index_exp)))\n\nAlso CONVERT_EXPR_P.\n\nI think the patch is OK with or without these changes.  Thanks.\n\n> --- gcc/testsuite/gcc.dg/ubsan/pr124969-1.c.jj\t2026-04-21 19:18:43.197227776 +0200\n> +++ gcc/testsuite/gcc.dg/ubsan/pr124969-1.c\t2026-04-21 19:19:23.288549722 +0200\n> @@ -0,0 +1,14 @@\n> +/* PR sanitizer/124969 */\n> +/* { dg-do compile } */\n> +/* { dg-options \"-fsanitize=undefined\" } */\n> +\n> +struct S {\n> +  int c;\n> +  void *b __attribute__ ((counted_by (c)));\n> +} a;\n> +\n> +void *\n> +foo (void)\n> +{\n> +  return a.b + 10;\n> +}\n> \n> \tJakub\n> \n\nMarek","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=W+TehBl9;\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=W+TehBl9","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 4g0VKw4ZHcz1yGs\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 22 Apr 2026 03:52:03 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id ABC874BA901D\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 21 Apr 2026 17:51:59 +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 E498E4BA23CC\n for <gcc-patches@gcc.gnu.org>; Tue, 21 Apr 2026 17:51:24 +0000 (GMT)","from mail-qt1-f197.google.com (mail-qt1-f197.google.com\n [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS\n (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n us-mta-235--4YmI8GtOwqTNr2ZF6flJQ-1; Tue, 21 Apr 2026 13:51:21 -0400","by mail-qt1-f197.google.com with SMTP id\n d75a77b69052e-50fb6d713ddso14012361cf.1\n for <gcc-patches@gcc.gnu.org>; Tue, 21 Apr 2026 10:51:21 -0700 (PDT)","from redhat.com ([2603:7000:9500:10::1db4])\n by smtp.gmail.com with ESMTPSA id\n d75a77b69052e-50e394c1fddsm118906941cf.30.2026.04.21.10.51.19\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Tue, 21 Apr 2026 10:51:19 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org ABC874BA901D","OpenDKIM Filter v2.11.0 sourceware.org E498E4BA23CC"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org E498E4BA23CC","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org E498E4BA23CC","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776793885; cv=none;\n b=g46GlaArjzvQS/KmzWDpVDJ1qVavYNXrO7l69bUWBho7KO9y5b+ZHAuAzI2hovvYnE6K6+HzEQpy5Mrhi8sRtfA2B0F/6JE57V0ciOODwks6tewdKxsDtgXg3arQSzmtOq6Qm+bWjK7OrpKF5Xxc2+oiPKnmYGlhvnTDwFwgW7k=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1776793885; c=relaxed/simple;\n bh=lh63LPA0+1g9SMAjQ6exJL8M3g7T/tlpl0vJMV/k7ug=;\n h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version;\n b=J9Xb3ZKsdzzhd/fT8ya4Vcb2nUSs1EjLqyv/qQ2XZc5+gfTgHvIERZUGbO446ZhgNjoP8QPYFyY+0GGO4UAjsZofSNAdvvdztr9ecbkHRlJ65rbK6PIp9RUtoQsUBlB97qTCIKXvUgt4W6We+u8K1taG5ck4Kr8iPwJjclt3Pvc=","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=1776793884;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:cc:mime-version:mime-version:content-type:content-type:\n in-reply-to:in-reply-to:references:references;\n bh=fHeJ4N8LzOBJJI0lUtR7j9ddogC8xdrGxpOYW7BxRe4=;\n b=W+TehBl9RaVVWAYcBFFMT7lDum89S8ckR9tMdh5zAsy4hAQz60H2zV9iauONI9FjO3yPOJ\n txYtDWVxYwHq2d0yRwLZESs/q9vRG6+u9LsZFkpez0GF/5ls+Kf2xcOu/gqQRaF4MPIvKw\n Rt9aqjqkWjyWud/lHJRqkzt3D0YJXlg=","X-MC-Unique":"-4YmI8GtOwqTNr2ZF6flJQ-1","X-Mimecast-MFC-AGG-ID":"-4YmI8GtOwqTNr2ZF6flJQ_1776793881","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1776793881; x=1777398681;\n h=user-agent:in-reply-to:content-disposition:mime-version:references\n :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=fHeJ4N8LzOBJJI0lUtR7j9ddogC8xdrGxpOYW7BxRe4=;\n b=eFj1hSjkZ5DFkckFWTl0FpqKO7R2qlhC/KVm4d/OFWoAcA9MeyX/YQZc46yTG8nuqM\n hjNTlUQZ/7pdGRfmi3M+cFa7S182HEo6Zo0FEnN6GJNhtAIfmRBC94o6qVQWhX27ODD8\n IgEfN2jLwmn5clEygsaA8kDiDjbCdEralfWOInLrRim/lPwhErKLEMQK8ofrvvZF9GoM\n SM8hZhfwO8jWKO2MSAkIMR4MQMphoB21Ezmt/2nEB/BwBM+5u+7hkk+yPWAzqHlbyitb\n q9rckhShcwbDEoU3yHsm5F0mTzHOX0f+NuPtw4vO/hEsHEV4kIFUqKARSsELYJlJfNlt\n FFBg==","X-Forwarded-Encrypted":"i=1;\n AFNElJ824JLAGYYc7dIL0gv+rrEc7X97J/CXOLxYNYnwYD/bXQZ6wIcdyVodgD60XGPzuCtrlTSkTDmcAa6QZg==@gcc.gnu.org","X-Gm-Message-State":"AOJu0YxKkQkEVo90ArkFbadIjNv7zxap6pKF0U5LlP2dqQBmhBE8Fyw2\n Bg+d0F3cuEJuB91HSVbHXj92C2BGXYCdrJxlckmc74LN3bhiXPZ+lC+QpL1DmCLH5/GXfbQ/k8u\n OBAAEhoAjPC9djibsq/LR3ahukLSN7AZPaZ++ItCtCJo9QMc14uB9zeXgojM=","X-Gm-Gg":"AeBDieuO7agwuGU72uSCrnaWRiUS0nsQb4He0rfebDlQXtnuwpO1kldiQau/MBe2CGm\n aV4asUgSDHnTHEGxr2kzxB2dZilOg9g7Fv/1mhyb2CGRkublLVtELvX8LgIpJ4TtKyt1NXuN6uj\n G3JIzzBU1FenQ/SuSr3q/iRscMRTTxPx5CPBX0diwCsv+h2QY41nsFaDxtx8slQBeDW0Kq8shqn\n 0dQyU2HsIdx+piBwHA2m1FR75+ltGkxX+kOJhY81q/dNVYSMkXE2CsrS/B9kArWqN2Bpg3ODTJM\n tGfdkbcDpNLeNEMi/8EG4bq23rwqlqaxP/9ocAbAy/5Gm02kXUnspFZkYCT1qMfNwI53bzI63gJ\n n8VdiduilCmsL","X-Received":["by 2002:ac8:5ccb:0:b0:50d:8080:2a7 with SMTP id\n d75a77b69052e-50e36b8577emr290268391cf.21.1776793880869;\n Tue, 21 Apr 2026 10:51:20 -0700 (PDT)","by 2002:ac8:5ccb:0:b0:50d:8080:2a7 with SMTP id\n d75a77b69052e-50e36b8577emr290267771cf.21.1776793880293;\n Tue, 21 Apr 2026 10:51:20 -0700 (PDT)"],"Date":"Tue, 21 Apr 2026 13:51:17 -0400","From":"Marek Polacek <polacek@redhat.com>","To":"Jakub Jelinek <jakub@redhat.com>","Cc":"\"Joseph S. Myers\" <josmyers@redhat.com>, Jason Merrill <jason@redhat.com>,\n Richard Biener <rguenther@suse.de>, gcc-patches@gcc.gnu.org","Subject":"Re: [PATCH] c-family: Fix ICE with counted_by attribute [PR124969]","Message-ID":"<aee5FYRL_PWbz9iL@redhat.com>","References":"<aeeztiQrDfqssPGq@tucnak>","MIME-Version":"1.0","In-Reply-To":"<aeeztiQrDfqssPGq@tucnak>","User-Agent":"Mutt/2.3.1 (2026-03-20)","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"yIlLG8oVe7B1uqv_tDjoFgEe6OF-36NNPv3e35TYDNQ_1776793881","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>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"}}]