Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8705825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:27:42+00:00 2026-06-13T03:27:42+00:00

Is this macro safe or should I make sure alloca never returns NULL? #define

  • 0

Is this macro safe or should I make sure alloca never returns NULL?

#define DO_COPY(x) strcpy(alloca(strlen((x)) + 1), x)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:27:44+00:00Added an answer on June 13, 2026 at 3:27 am

    If the string is user controlled I’d say that alloca is not safe. The way alloca is implemented in many compilers it doesn’t do any kind of sanity checking on the amount it subtracts from (or adds to if your stack grows that way) the stack pointer. Even with large red zones around the stack it would be relatively easy to make the alloca():ed string to point way outside the stack.

    Especially in a threaded environment the thread stacks can be quite small and close to each other.

    On the linux machines I could test this on it would require a 10MB string to do start scribbling on some other threads stack with this. On MacOS 512kB seems to be enough.

    Here’s a quick hack to see how close you can end up (notice that this doesn’t really tell you much if the stack allocation is done using some randomizing allocator like in OpenBSD or some other system that takes their allocator safety seriously).

    #include <stdlib.h>
    #include <stdio.h>
    #include <pthread.h>
    #include <stdint.h>
    
    void *
    x(void *v)
    {
        int x;
    
        return &x;
    }
    
    int
    main(int argc, char **argv)
    {
        pthread_t ta, tb;
        char *va, *vb;
    
        pthread_create(&ta, NULL, x, NULL);
        pthread_create(&tb, NULL, x, NULL);
    
        pthread_join(ta, (void **)&va);
        pthread_join(tb, (void **)&vb);
    
        printf("diff: %d\n", abs((intptr_t)vb-(intptr_t)va));
    
        return 0;
    }
    

    And here is what strcpy(alloca(strlen(s) + 1), s) gets compiled to:

        movq    %rbx, %rdi
        call    _strlen
        addq    $31, %rax
        andq    $-16, %rax
        subq    %rax, %rsp
        movq    %rsp, %rdi
        movq    %rbx, %rsi
        call    _strcpy
    

    Notice how there’s no sanity checking other than quick alignment before subtracting the return value from strlen (in %rax) from the stack pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is safe if I use this macro in my code? #define my_calloc(x, n) ((x)
This macro should activate tabs in sequence from 1th to 5th and then again.
If you have a macro like this in C: #define SLICE_VERSION 20110614 How to
Can you do something like this with a macro in C ? #define SUPERMACRO(X,Y)
Given this macro: #define SOME_MACRO(ret, f, args) \ typedef ret (*some_func_##f) args; \ static
Given this macro #define MAKE_TYPE(_a, _b, _c, _d) ((_a) | ((_b) << 8) |
When I define this macro: #define SQR(x) x*x Let's say this expression: SQR(a+b) This
I was wondering if someone can explain this macro to me. #define Q_DECLARE_PRIVATE(Class) \
I have seen this macro defined before but never really knew its purpose. Can
I currently have this macro #define bind(OBJECT, ROLE) \ assert( sizeof(*ROLE) == 1 );

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.