This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[patch] alloca is dangerous to users health


Reply-To: ddsinc09@ix.netcom.com


After spending more time than I want to admit pouring
over my fixincl code and finding no trouble on my GCC/EGCS
compiled systems, I have now found a memory leak for most
other systems.  This program calls regexec tens or hundreds
of thousands of times.  Every time from the same place in
the stack.  regexec will, on some systems, call malloc.
However, it will only free that memory if the alloca
emulation routine is called from a spot further down in
the stack.  That never happens.  Oops.

Suggested rule:  Never use alloca.  It is not efficient
enough to be worth it.  Never ever.  Don't.


So, is this patch putrid enough to ship?
Index: fixincl.c
===================================================================
RCS file: /cvs/egcs/egcs/gcc/fixinc/fixincl.c,v
retrieving revision 1.6
diff -C5 -r1.6 fixincl.c
*** fixincl.c   1999/04/26 10:38:38     1.6
--- fixincl.c   1999/04/29 05:08:31
***************
*** 302,311 ****
--- 302,314 ----
        else if (pz_data = load_file (pz_file_name),
                (pz_data != (char *) NULL))
          {
            if (strstr (pz_data, gnu_lib_mark) == (char *) NULL)
              process (pz_data, pz_file_name);
            free ((void *) pz_data);
+ #         ifdef C_ALLOCA
+           alloca (0);
+ #         endif
          }
      }
  
    return EXIT_SUCCESS;
  }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]