Re: Dereferencing a null pointer to an array, then converting to pointer to first element

Alejandro Colomar via Gcc-help <[email protected]>
Newsgroups gmane.comp.gcc.help
Message-ID <kpgcc6o7ohozamy2oz276rmgigosnu4hj6xhjdhdyyvqeaatgk@y56mmd6mk44a>
Hi Basile,

On Tue, Nov 18, 2025 at 10:25:54AM +0100, Basile Starynkevitch wrote:
> On Tue, 2025-11-18 at 10:16 +0100, Alejandro Colomar via Gcc-help wrote:
> > Hi!
> > 
> > The wording of the ISO C standard seems to say that the following
> > program has undefined behavior:
> > 
> > 	alx@devuan:~/tmp$ cat null.c 
> > 	#include <stddef.h>
> > 
> > 	int
> > 	main(void)
> > 	{
> > 		int  (*null)[42] = NULL;
> > 		int  *p = *null;
> > 	}
> 
> You could try using frama-c.com (an open source static C analyzer). I believe it would catch the error.

Hmmm, I'll try that.  Sadly, it seems I'm unable to install it at the
moment.

	$ sudo apt-get install frama-c-base
	Reading package lists... Done
	Building dependency tree... Done
	Reading state information... Done
	Solving dependencies... Error!
	Some packages could not be installed. This may mean that you have
	requested an impossible situation or if you are using the unstable
	distribution that some required packages have not yet been created
	or been moved out of Incoming.
	The following information may help to resolve the situation:

	The following packages have unmet dependencies:
	 frama-c-base : Depends: libcairo2-ocaml-7q5f6 but it is not installable
			Depends: libfindlib-ocaml-0ne56 but it is not installable
			Depends: liblablgtk3-ocaml-hnx11 but it is not installable
			Depends: liblablgtksourceview3-ocaml-51fc1 but it is not installable
			Depends: libppx-deriving-ocaml-wl826 but it is not installable
			Depends: libstdlib-ocaml-7l663 but it is not installable
			Depends: libyojson-ocaml-g43y1 but it is not installable
			Depends: libzarith-ocaml-mtsj7 but it is not installable
			Depends: libocamlgraph-ocaml-dev but it is not going to be installed
			Depends: libppx-import-ocaml but it is not going to be installed
			Recommends: alt-ergo but it is not installable
			Recommends: why3 but it is not going to be installed
	E: Unable to satisfy dependencies. Reached two conflicting decisions:
	   1. frama-c-base:amd64=20220511-manganese-5+b13 is selected for install
	   2. frama-c-base:amd64 Depends libfindlib-ocaml-0ne56
	      but none of the choices are installable:
	      [no choices]

> 
> However, catching such bugs is in general impossible. See also the https://en.wikipedia.org/wiki/Coccinelle_(software)

But is it really a bug?  Is this UB in GNU C?  Or does GNU C support
this as an (undocumented) extension?  Because this would be useful --if
supported--, for example here:

	#define typeas(T)           typeof((T){})
	#define MALLOCARRAY(n, T)   MALLOCARRAY_(n, typeas(T))
	#define MALLOCARRAY_(n, T)                                    \
	({                                                            \
		size_t  n_ = n;                                       \
	                                                              \
		(T (*)[n_]){reallocarray(NULL, n_, sizeof(T))};       \
	})

	#define MALLOC(n, T)  (*MALLOCARRAY(n, T))

where MALLOCARRAY() can be used when you want a pointer to an array, and
MALLOC() when you want just a pointer.

	int (*ap)[42] = MALLOCARRAY(42, int);

	int *p = MALLOC(7, int);

If this is not supported in GNU C, then MALLOC() needs to be defined
from scratch:

	#define MALLOC(n, T)   MALLOC_(n, typeas(T))
	#define MALLOC_(n, T)  ((T *){reallocarray(NULL, n, sizeof(T))})

So, I'm wondering if I can reuse MALLOCARRAY() for defining MALLOC().


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmkcQD0ACgkQ64mZXMKQ
wqmIMQ/6Ak6OBCrac+LWUh9NquCEGt2GbBMBNT3HACXeC1hUShMihJCqYWZSgqEL
Y8C7RDjH9y94J4plATUbJQwn3mUjZjvQzbL+JPDv+NPqxYygC5O6dYEtSF3ogrn2
olJ+j4OVrjKandPzTZCpqG65IhKvtNIM1oh6dW289U5RVjYWwxV9TJ1K+jw5KZA+
ulhHRNPQd+PAUQzIFuCfHylEpWXIec2I4YKpSsI3NBGsRV6TYN0UO3uK+bVgoAMX
EbJMTqDZbalsoUZYKTudFJ15IEiS3W/k91YvIMtkhC6A6vPFGthYbzd8NR8AqEfU
BZw5mcV0DhcG0uDaSatN8hBbt11E/q2lJMUgI49CE+OhtR6Fh6gz2hfF2JWTEW3R
ehJl4ooUQqZVytdwOgHT06bDForQYex2B5eayJ9CFaQwjcjC8s/NW4JyPPUFduxL
iSf+RAqNYiRCCamWLaZA7m3pA6aPJ6ttQ9K7lCO0ZXOdVNpv63VQswur0H5ymNij
XxEpyLWMfnr40tNSyn99XbTg4etkybOJVYAkCoT90IJEcIArDJQb1Vv4vqnHZNd1
oqlTdlGIXkpl4vIQGgsvTfPjq1t5lxjPeQiJ0eyyWrJDfLmvueqELQ9ZE5YCTUDP
zh2pk4hWw9D5YuwVzrb0jh8t7rw25REuZCYutETIiCHUSc01eMM=
=KRW5
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.