Re: [RFA] Avoid recursivly defined user functions.

Michael Snyder
2002-04-11T20:55:50Z

Don Howard wrote:
>
> On Tue, 9 Apr 2002, Don Howard wrote:
>
> > On Tue, 9 Apr 2002, Paul Hilfinger wrote:
> >
> > >
> > > > How about something even simpler - track user command depth at runtime?
> > > > Set an absurd limit, like 1024 deep, if we can handle that in a
> > > > normal-sized stack limit. Then complain if we hit it at runtime.
> > >
> > > I completely agree with this. After all, if you were willing to
> > > contemplate outlawing recursion altogether, you certainly won't LOSE
> > > anything by Daniel's approach, and you gain everything you wanted in
> > > the first place---to avoid crashing GDB.
> > >
> > > Paul
> > >
> >
> >
> > I like this approach, also.

So do I. ;-)

> >
>
> 2002-04-11 Don Howard <dhoward@redhat.com>
>
> * cli/cli-cmds.c (init_cli_cmds): Add new user settable value:
> max_user_call_depth.
> (init_cmd_lists): Initialize the new value;
> * cli/cli-script.c (execute_user_command): Limit the call depth of
> user defined commands. This avoids a core-dump when user commands
> are infinitly recursive.
>
> Index: cli/cli-cmds.c
> ===================================================================
> RCS file: /cvs/src/src/gdb/cli/cli-cmds.c,v
> retrieving revision 1.18
> diff -p -u -w -r1.18 cli-cmds.c
> --- cli/cli-cmds.c 28 Mar 2002 01:35:56 -0000 1.18
> +++ cli/cli-cmds.c 11 Apr 2002 17:30:58 -0000
> @@ -80,6 +80,9 @@ static void shell_escape (char *, int);
>
> void apropos_command (char *, int);
>
> +/* Limit the call depth of user-defined commands */
> +int max_user_call_depth;
> +
> /* Define all cmd_list_elements. */
>
> /* Chain containing all defined commands. */
> @@ -606,6 +609,8 @@ show_debug (char *args, int from_tty)
> void
> init_cmd_lists (void)
> {
> + max_user_call_depth = 1024;
> +
> cmdlist = NULL;
> infolist = NULL;
> enablelist = NULL;
> @@ -823,4 +828,11 @@ With no arguments, run an inferior shell
> Argument is the name of the user defined command.\n\
> With no argument, show definitions of all user defined commands.", &showlist);
> add_com ("apropos", class_support, apropos_command, "Search for commands matching a REGEXP");
> +
> + add_show_from_set (
> + add_set_cmd ("max_user_call_depth", no_class, var_integer,
> + (char *) &max_user_call_depth,
> + "Set the max call depth for user-defined commands.\n",
> + &setlist),
> + &showlist);
> }
> Index: cli/cli-script.c
> ===================================================================
> RCS file: /cvs/src/src/gdb/cli/cli-script.c,v
> retrieving revision 1.11
> diff -p -u -w -r1.11 cli-script.c
> --- cli/cli-script.c 17 Mar 2002 19:53:39 -0000 1.11
> +++ cli/cli-script.c 11 Apr 2002 17:30:59 -0000
> @@ -247,6 +247,15 @@ execute_cmd_post_hook (struct cmd_list_e
> }
>
> /* Execute the command in CMD. */
> +void
> +do_restore_user_call_depth (void * call_depth)
> +{
> + int * depth = call_depth;
> + /* We will be returning_to_top_level() at this point, so we want to
> + reset our depth. */
> + (*depth) = 0;
> +}
> +
>
> void
> execute_user_command (struct cmd_list_element *c, char *args)
> @@ -254,6 +263,8 @@ execute_user_command (struct cmd_list_el
> register struct command_line *cmdlines;
> struct cleanup *old_chain;
> enum command_control_type ret;
> + static int user_call_depth = 0;
> + extern int max_user_call_depth;
>
> old_chain = setup_user_args (args);
>
> @@ -262,6 +273,11 @@ execute_user_command (struct cmd_list_el
> /* Null command */
> return;
>
> + if (user_call_depth++ > max_user_call_depth)
> + error ("Max user call depth exceeded -- command aborted\n");
> +
> + old_chain = make_cleanup (do_restore_user_call_depth, &user_call_depth);
> +
> /* Set the instream to 0, indicating execution of a
> user-defined function. */
> old_chain = make_cleanup (do_restore_instream_cleanup, instream);
> @@ -277,6 +293,8 @@ execute_user_command (struct cmd_list_el
> cmdlines = cmdlines->next;
> }
> do_cleanups (old_chain);
> +
> + user_call_depth--;
> }
>
> enum command_control_type
>
> --
> dhoward@redhat.com
> gdb engineering