From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id 112D93858434 for ; Thu, 11 May 2023 16:07:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 112D93858434 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-wm1-x335.google.com with SMTP id 5b1f17b1804b1-3f4ec041fc3so5920755e9.1 for ; Thu, 11 May 2023 09:07:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683821239; x=1686413239; h=subject:from:cc:to:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=jr+IpxsOIWGyas6nlPf6rfegaN2QvN/W0dAD9pL52QM=; b=EeUjXcO7JFXzY1o6HaN6i2JWDzmPdwY1qUh8HoPo5AfVSIMooSoTGncOgMjIJqVRvP AdZFkEk6X9+RvaUNozrJO0G6abFKZwN//QlNv9vYJfh6tfx5WbMDs3lvWbapGDuN6c4o Sxx4z6Dy5NLjjK/aaQWtrFhNqMmfmOfSld/MNqFup3EQh6PiktMk+IVJLiRDNytIlbBg G9FciOBL/2eIq8Z6E+wuvMmLvu/beZfB6I22IZoISM9/htS5FIXp7qd88qA3Aq2Ybw6B Ush6B0ysC/1buK8GyrX+ZQ3WLYM5/DzOZM9pZkOqu9gQB1Ur1hGkU7xWcZeOY2vGQLus PWBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683821239; x=1686413239; h=subject:from:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jr+IpxsOIWGyas6nlPf6rfegaN2QvN/W0dAD9pL52QM=; b=Za8VSxNqHE/IqLmQFnBKFg+G0AgVLMsn5d0QP1fK8ypvgW4YYLG8ZiKaU7S4JjiSO7 Gu+lYBwacqFi+Sc2PTS0f8UXiXicJ3SZ1OwITe8kPFuirpKLC9QoFewPHtHrgVa0ECpN NO0L4BPxGjg4bHbCq23oIGSxqXHNJrkXnLmJ4LAHociu05ipFxmcKfvyYsgJ2x1bT5VT L8jNSFBlhxe124tqxdEavQo2Mq+s3dTjHeGioe5GZRmSGyJJOadvkuajuIdG9axEZQpr /KWMKi9QM368TW7st84oavE/B3mMUvDrU9GdW1h0fZaxEa0H82IUaVavdPN+xB1m4wQF lm+g== X-Gm-Message-State: AC+VfDx9Z3AWrOG2+FkPylfkKd1O3cxGG/UtdkxwQilk9E7xeP+QXLbM wkYmwmT0dkys7pgth3DL/9YnxzEO1sU= X-Google-Smtp-Source: ACHHUZ7ZqaaG60HHHS3MUMYS/DfbzRjojV3zxIQBO+ky7zkd1EPHE2PFHLYICh7GEIxd1l6/SaZWjQ== X-Received: by 2002:a7b:cb8a:0:b0:3f4:2174:b29c with SMTP id m10-20020a7bcb8a000000b003f42174b29cmr11680927wmi.20.1683821239403; Thu, 11 May 2023 09:07:19 -0700 (PDT) Received: from [192.168.0.160] ([170.253.51.134]) by smtp.gmail.com with ESMTPSA id p26-20020a05600c205a00b003f4f1b884b3sm507286wmg.20.2023.05.11.09.07.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 11 May 2023 09:07:18 -0700 (PDT) Message-ID: Date: Thu, 11 May 2023 18:07:03 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Content-Language: en-US To: GCC Cc: Alejandro Colomar From: Alejandro Colomar Subject: [wish] Flexible array members in unions Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="------------w2jX9EQ8noszfJBA0M7Mruub" X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE 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: This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --------------w2jX9EQ8noszfJBA0M7Mruub Content-Type: multipart/mixed; boundary="------------1oBa0M7luYNS9Snode7LC6Wo"; protected-headers="v1" From: Alejandro Colomar To: GCC Cc: Alejandro Colomar Message-ID: Subject: [wish] Flexible array members in unions --------------1oBa0M7luYNS9Snode7LC6Wo Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi! Currently, one can have pseudo-flexible array members in unions with [0] syntax, but it's not allowed with [] syntax. Here's an example of how it is possible today: struct s { ... size_t n; union { ptrdiff_t off[0]; // [n]; offsets from s->data. char data[0]; }; }; which is useful to have a structure with two (or really several) consecutive flexible arrays: one of offsets, which mark the positions of data, and another with the actual data. Below goes an example program, which works fine with GCC, and I believe rewriting it to not use the union would make it less clear, since I'd need to add casts to it. It works thanks to [0] pseudo-flexible arrays, but it doesn't compile with C99 flexible arrays. And of course, [0] arrays have issues with -fstrict-flex-arrays=3D3. $ cat flexi4.c=20 #include #include #include #include struct s { size_t n; union { ptrdiff_t off[0]; char data[0]; }; }; int main(void) { char *p; struct s *s; s =3D malloc(offsetof(struct s, off) + sizeof(ptrdiff_t) * 2 + sizeof("foobar") + sizeof("baz")); s->n =3D 2; p =3D s->data + sizeof(ptrdiff_t) * s->n; s->off[0] =3D p - s->data; p =3D stpcpy(p, "foobar") + 1; s->off[1] =3D p - s->data; p =3D stpcpy(p, "baz") + 1; puts(s->data + s->off[0]); puts(s->data + s->off[1]); free(s); } $ gcc-13 -Wall -Wextra -Werror -fanalyzer \ -fsanitize=3Dundefined -fsanitize=3Daddress \ -D_FORTIFY_SOURCE=3D3 -fstrict-flex-arrays=3D2 \ flexi4.c=20 $ ./a.out=20 foobar baz $ gcc-13 -Wall -Wextra -Werror -fanalyzer \ -fsanitize=3Dundefined -fsanitize=3Daddress \ -D_FORTIFY_SOURCE=3D3 -fstrict-flex-arrays=3D3 \ flexi4.c=20 $ ./a.out=20 flexi4.c:27:8: runtime error: index 0 out of bounds for type 'ptrdiff_t [= *]' flexi4.c:29:8: runtime error: index 1 out of bounds for type 'ptrdiff_t [= *]' flexi4.c:32:23: runtime error: index 0 out of bounds for type 'ptrdiff_t = [*]' foobar flexi4.c:33:23: runtime error: index 1 out of bounds for type 'ptrdiff_t = [*]' baz Would you allow flexible array members in unions? Is there any strong reason to disallow them? Currently, I get: $ gcc-13 -Wall -Wextra -fanalyzer \ -fsanitize=3Dundefined -fsanitize=3Daddress \ -D_FORTIFY_SOURCE=3D3 -fstrict-flex-arrays=3D3 \ flexi4-true.c=20 flexi4-true.c:9:28: error: flexible array member in union 9 | ptrdiff_t off[]; | ^~~ flexi4-true.c:10:28: error: flexible array member in union 10 | char data[]; | ^~~~ Cheers, Alex --=20 GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5 --------------1oBa0M7luYNS9Snode7LC6Wo-- --------------w2jX9EQ8noszfJBA0M7Mruub Content-Type: application/pgp-signature; name="OpenPGP_signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="OpenPGP_signature" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmRdEqcACgkQnowa+77/ 2zI4iQ//Rxv+pH8ikksXGpSUe9nrE2Cm8vVR/MFoZgRbw1lV+TIF6CPp0cHNjbPP hSGAgF9LiypO3YmZM4y45R6pWzu+VKcxBwrUHIulXw+RNnV9AQKIKoX8yXwelV56 Hc/kI99LHCz9iMc0wamWEeVpw8HM0GMu98icDeF+grsmpPJo4PckfImOhBm9qG8U MnRwfJ05lvkHoBI62SFP0+LZY5m0v1G8fwcgpzxn2Y/8/Ii4Zxzh8neQuTaD94ow Ed6c2E4sh9VedOWHQLsqod8VbKS3po/3KMoiLg9cR0L4XYA/mReQ7otyidrZG7Jz /JfnAZFiGGAJKNRvz52SXPebv8vZex+9aV7A6KNtbm8F0nRt33Sk4wcX5Nnb5E2v m8bK3jByQ3G2zZlgRSq8oL3UpBQzelplVHwZqtnNFKzumLRwCJ0zL/5BeN9TMFMt So90ZAXOol0u2IyhIDJkD3nl+8HvqjOpp26VWJuAGOx4ePQMPkf5S9/Tztj+nTH6 v/z34H6eGas4P1E7ulgeNYwmducneYXJ+1tuSfUJ1GRrZCAX2m1lw/xOv78A4FrG YYhR8XoeeKv2IddrDWZz9usHvVIgh9lq+g3bijquCciKRSOZY97QpIcJPsZYVXt6 lEmHKvWSNlbYdi8Na4sLuP1lldRFgt1DyG1mR7GxSkZR1xqj394= =NRL5 -----END PGP SIGNATURE----- --------------w2jX9EQ8noszfJBA0M7Mruub--