bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

No recursion limit in functions


From: Frederik Meerwaldt
Subject: No recursion limit in functions
Date: Tue, 2 Dec 2003 18:34:06 +0100

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dear Bash Developer Team,

we (cray-cyber.org) found out a bug in bash (all versions, including the current patches which are available for download - occures on all platforms), which causes it to crash.
Bash has no recursion limit in functions, so
        function xxx { xxx; }
        xxx
Causes bash to consume more and more memory until it dies (Illegal Instruction, Segmentation Fault, etc. - the signal heavily depends on the platform). I wrote a little "fix", which is to limit the recursion depth of functions to, for example 20:

*** ../bash-2.05b/execute_cmd.c Mon Mar 18 19:24:22 2002
- --- execute_cmd.c       Tue Dec  2 18:13:39 2003
***************
*** 223,228 ****
- --- 223,231 ----
     environment. */
  int subshell_environment;

+ /* Current recursion depth of a single function - limited to 20 */
+ int funct_recdepth=0;
+
  /* Currently-executing shell function. */
  SHELL_VAR *this_shell_function;

***************
*** 2853,2858 ****
- --- 2856,2869 ----
    COMMAND *tc, *fc;
    char *debug_trap, *error_trap;

+   if (funct_recdepth > 19) {
+ fprintf(stderr, "Maximum function recursive depth of 20 reached - - aborti
ng...\n");
+       fflush(stderr);
+       return(1);
+   }
+   else
+       funct_recdepth++;
+
    USE_VAR(fc);

    tc = (COMMAND *)copy_command (function_cell (var));
***************
*** 2944,2949 ****
- --- 2955,2961 ----
    if (variable_context == 0 || this_shell_function == 0)
      make_funcname_visible (0);

+   funct_recdepth--;
    return (result);
  }

Whether this really makes sense to limit it the number to a hardcoded number of recursive functions is not sure....
Perhaps a higher number such as 50 or 100 would make more sense.
However, in our opinion, such a recursion limit is necessary because a single user can use excessive memory and cause the process to hang or crash without it.

Greetings from Germany,
        Freddy
- -- Freddy Meerwaldt (address@hidden)
http://www.freddym.org  <-- find my PGP Key here
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (Darwin)

iD8DBQE/zM0Qc6nyuk7sJRIRAnX6AKCz6oeFnaf4Xxufy/ryXyCOZVm4lgCgnuBm
pHrPJ4cVZTeuBLwDHaviHBU=
=2qNN
-----END PGP SIGNATURE-----





reply via email to

[Prev in Thread] Current Thread [Next in Thread]