From 39e277be3c05dd13e92766cb8fc8ffc58cefdd53 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Mon, 6 Dec 2010 21:48:56 +0000 Subject: [PATCH] |--------------------- |PatchSet 781 |Date: 2010/10/29 21:11:57 |Author: nicm |Branch: HEAD |Tag: (none) |Log: |We now send argv to the server after parsing it in the client to get the |command, so the client should not modify it. Instead, take a copy. Fixes |parsing command lists, reported by mcbride@. | |Members: | cmd-list.c:1.5->1.6 | cmd.c:1.45->1.46 | tmux.h:1.244->1.245 --- cmd-list.c | 22 +++++++++++++--------- cmd.c | 18 +++++++++++++++++- tmux.h | 3 ++- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/cmd-list.c b/cmd-list.c index 54dbcbfa..b0840e52 100644 --- a/cmd-list.c +++ b/cmd-list.c @@ -1,4 +1,4 @@ -/* $Id: cmd-list.c,v 1.9 2010-07-02 02:43:01 tcunha Exp $ */ +/* $Id: cmd-list.c,v 1.10 2010-12-06 21:48:56 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -29,7 +29,9 @@ cmd_list_parse(int argc, char **argv, char **cause) struct cmd *cmd; int i, lastsplit; size_t arglen, new_argc; - char **new_argv; + char **copy_argv, **new_argv; + + copy_argv = cmd_copy_argv(argc, argv); cmdlist = xmalloc(sizeof *cmdlist); cmdlist->references = 1; @@ -37,18 +39,18 @@ cmd_list_parse(int argc, char **argv, char **cause) lastsplit = 0; for (i = 0; i < argc; i++) { - arglen = strlen(argv[i]); - if (arglen == 0 || argv[i][arglen - 1] != ';') + arglen = strlen(copy_argv[i]); + if (arglen == 0 || copy_argv[i][arglen - 1] != ';') continue; - argv[i][arglen - 1] = '\0'; + copy_argv[i][arglen - 1] = '\0'; - if (arglen > 1 && argv[i][arglen - 2] == '\\') { - argv[i][arglen - 2] = ';'; + if (arglen > 1 && copy_argv[i][arglen - 2] == '\\') { + copy_argv[i][arglen - 2] = ';'; continue; } new_argc = i - lastsplit; - new_argv = argv + lastsplit; + new_argv = copy_argv + lastsplit; if (arglen != 1) new_argc++; @@ -61,16 +63,18 @@ cmd_list_parse(int argc, char **argv, char **cause) } if (lastsplit != argc) { - cmd = cmd_parse(argc - lastsplit, argv + lastsplit, cause); + cmd = cmd_parse(argc - lastsplit, copy_argv + lastsplit, cause); if (cmd == NULL) goto bad; TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry); } + cmd_free_argv(argc, copy_argv); return (cmdlist); bad: cmd_list_free(cmdlist); + cmd_free_argv(argc, copy_argv); return (NULL); } diff --git a/cmd.c b/cmd.c index 288b8ff8..a83fddfc 100644 --- a/cmd.c +++ b/cmd.c @@ -1,4 +1,4 @@ -/* $Id: cmd.c,v 1.144 2010-10-24 01:34:30 tcunha Exp $ */ +/* $Id: cmd.c,v 1.145 2010-12-06 21:48:56 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -165,6 +165,22 @@ cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv) return (0); } +char ** +cmd_copy_argv(int argc, char **argv) +{ + char **new_argv; + int i; + + if (argc == 0) + return (NULL); + new_argv = xcalloc(argc, sizeof *new_argv); + for (i = 0; i < argc; i++) { + if (argv[i] != NULL) + new_argv[i] = xstrdup(argv[i]); + } + return (new_argv); +} + void cmd_free_argv(int argc, char **argv) { diff --git a/tmux.h b/tmux.h index 624694f4..9b851166 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.581 2010-10-24 01:34:30 tcunha Exp $ */ +/* $Id: tmux.h,v 1.582 2010-12-06 21:48:56 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1462,6 +1462,7 @@ const char *cmd_set_option_print( /* cmd.c */ int cmd_pack_argv(int, char **, char *, size_t); int cmd_unpack_argv(char *, size_t, int, char ***); +char **cmd_copy_argv(int, char **); void cmd_free_argv(int, char **); struct cmd *cmd_parse(int, char **, char **); int cmd_exec(struct cmd *, struct cmd_ctx *);