Do not use alloca inside a loop #21

Merged
ryanlerch merged 1 commit from alloca-loops into master 2022-02-11 12:34:45 +00:00
Contributor
No description provided.
First-time contributor

The advise is good, but alloca memory can be deallocated on scope exit if the scope contains a VLA. I don't think compilers diagnose this properly.

The advise is good, but `alloca` memory can be deallocated on scope exit if the scope contains a VLA. I don't think compilers diagnose this properly.
Author
Contributor

From the man page it says:

The  alloca() function allocates size bytes of space in the stack frame of the caller.  This temporary space is automatically freed when the function that called alloca() returns to its caller.

If it is deallocated on scope exit it goes against the description, doesn't it?

From the man page it says: ``` The alloca() function allocates size bytes of space in the stack frame of the caller. This temporary space is automatically freed when the function that called alloca() returns to its caller. ``` If it is deallocated on scope exit it goes against the description, doesn't it?
First-time contributor

Huh, it turns out I misremembered, and according to the GCC documentation, calling alloca inhibits VLA deallocation, not the other way round:

The space for a variable-length array is deallocated as soon as the array name’s scope ends, unless you also use alloca in this scope.

https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

A short test program confirms this:

#include <alloca.h>

void f1 (int *, int *);

void f2 (int n)
{
  for (int i = 0; i < n; ++i)
    {
      int a[n];
      int *b = alloca (sizeof (*b) * n);
      f1 (a, b);
    }
}
Huh, it turns out I misremembered, and according to the GCC documentation, calling `alloca` inhibits VLA deallocation, not the other way round: > The space for a variable-length array is deallocated as soon as the array name’s scope ends, unless you also use `alloca` in this scope. https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html A short test program confirms this: ```c #include <alloca.h> void f1 (int *, int *); void f2 (int n) { for (int i = 0; i < n; ++i) { int a[n]; int *b = alloca (sizeof (*b) * n); f1 (a, b); } } ```
Contributor

@fweimer so we are good to commit this right?

@fweimer so we are good to commit this right?
First-time contributor

@huzaifas It's still not completely accurate.

Maybe add a sentence that discourages mixing VLAs and alloca in the same function?

@huzaifas It's still not completely accurate. Maybe add a sentence that discourages mixing VLAs and `alloca` in the same function?
Author
Contributor

Ok I will add that!

Ok I will add that!
Contributor

@ret2libc ok, i will wait for the change in the patch, before i commit :)

@ret2libc ok, i will wait for the change in the patch, before i commit :)
Author
Contributor

1 new commit added

  • Notify about mixing VLA and alloca
**1 new commit added** * ``Notify about mixing VLA and alloca``
Author
Contributor

@huzaifas @fweimer sentence added about mixing VLA and alloca.

@huzaifas @fweimer sentence added about mixing VLA and alloca.
Author
Contributor

rebased onto 34c100361b

rebased onto 34c100361b0d11ad18d4a2e113d4b97aae770a0d
Contributor

Pull-Request has been merged by huzaifas

Pull-Request has been merged by huzaifas
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: Docs/defensive-coding-guide#21
No description provided.