Re: git: 4da237aee328 - main - alloca.3: Add entry about defining VLAs in same block as alloca() to BUGS
- Reply: Cy Schubert : "Re: git: 4da237aee328 - main - alloca.3: Add entry about defining VLAs in same block as alloca() to BUGS"
- In reply to: Aymeric Wibo : "git: 4da237aee328 - main - alloca.3: Add entry about defining VLAs in same block as alloca() to BUGS"
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 13 Mar 2026 13:13:26 UTC
Hi Aymeric,
Am Fri, Mar 13, 2026 at 01:10:41PM +0000 schrieb Aymeric Wibo:
> Avoid
> .Fn alloca
> with large unbounded allocations.
> +.Pp
> +The use of C99 variable-length arrays and
> +.Fn alloca
> +in the same function will cause the lifetime of
> +.Fn alloca Ns 's
> +storage to be limited to the block containing the
> +.Fn alloca .
> +For example, in the following snippet,
> +.Va p Ns 's
> +lifetime does not extend outside of the block, whereas it would've if
> +.Va vla
> +hadn't been defined or had been defined as a fixed-length array:
> +.Bd -literal -offset indent
> +char *p;
> +{
> + const int n = 100;
> + int vla[n];
> + p = alloca(32);
> + strcpy(p, "Hello, world!");
> + printf("Inside: %s\\n", p); /* Valid. */
> +}
> +printf("Outside: %s\\n", p); /* Undefined. */
> +.Ed
I am unsure if we should document the behaviour of mixing VLAs and
alloca() in the same function as being defined, as that binds us to
support it in the future. I would be a lot more comfortable just
documenting that behaviour is undefined if the two are combined in
one function.
Yours,
Robert Clausecker
--
() ascii ribbon campaign - for an encoding-agnostic world
/\ - against html email - against proprietary attachments