Fix some constness errors in fetch-pack
v2 cover Fix some constness errors in fetch-pack
From: Michael Haggerty <[email protected]>
Fix some constness errors that I noticed while reading the code in builtin/fetch-pack.c.
This version differs from v1 as follows:
* cmd_fetch_pack() is not made to free the memory that it allocates.
* The second patch is broken into separate pieces to make each piece more readable.
Michael Haggerty (4):
cmd_fetch_pack(): declare dest to be const cmd_fetch_pack(): handle non-option arguments outside of the loop cmd_fetch_pack(): combine the loop termination conditions cmd_fetch_pack(): respect constness of argv parameter
builtin/fetch-pack.c | 145 ++++++++++++++++++++++++-------------------------- 1 file changed, 71 insertions(+), 74 deletions(-)
Signature · 1 line
1.7.10
PATCH v2 1/4 cmd_fetch_pack(): declare dest to be const
From: Michael Haggerty <[email protected]>
There is no need for it to be non-const, and this avoids the need for casting away the constness of an argv element.
--- builtin/fetch-pack.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
builtin/fetch-pack.c +4 -3
@@ -901,7 +901,8 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) {int i, ret, nr_heads;struct ref *ref =NULL ; -char *dest =NULL , **heads; +const char *dest =NULL ; +char **heads;int fd[2 ];char *pack_lockfile =NULL ;char **pack_lockfile_ptr =NULL ;
@@ -971,7 +972,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) }usage (fetch_pack_usage); } - dest = (char *)arg; + dest = arg; heads = (char **)(argv + i +1 ); nr_heads = argc - i -1 ;break ;
@@ -1018,7 +1019,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) fd[0 ] =0 ; fd[1 ] =1 ; }else { - conn =git_connect (fd, (char *)dest, args.uploadpack, + conn =git_connect (fd, dest, args.uploadpack, args.verbose ?CONNECT_VERBOSE :0 ); }
Signature · 1 line
1.7.10
PATCH v2 2/4 cmd_fetch_pack(): handle non-option arguments outside of the loop
In reply to [email protected]From: Michael Haggerty <[email protected]>
This makes it more obvious that the code is always executed unless there is an error, and that the first initialization of nr_heads is unnecessary.
--- builtin/fetch-pack.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
builtin/fetch-pack.c +7 -5
@@ -910,7 +910,6 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)packet_trace_identity ("fetch-pack" ); - nr_heads =0 ; heads =NULL ;for (i =1 ; i < argc; i++) {const char *arg = argv[i];
@@ -972,14 +971,17 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) }usage (fetch_pack_usage); } - dest = arg; - heads = (char **)(argv + i +1 ); - nr_heads = argc - i -1 ;break ; } -if (!dest) + +if (i < argc) + dest = argv[i++]; +else usage (fetch_pack_usage); + heads = (char **)(argv + i); + nr_heads = argc - i; +if (args.stdin_refs) {/* * Copy refs from cmdline to new growable list, then
Signature · 1 line
1.7.10
PATCH v2 3/4 cmd_fetch_pack(): combine the loop termination conditions
In reply to [email protected]From: Michael Haggerty <[email protected]>
If an argument that does not start with '-' is found, the loop is terminated. So move that check into the for-loop condition.
--- builtin/fetch-pack.c | 113 ++++++++++++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 58 deletions(-)
builtin/fetch-pack.c +55 -58
@@ -911,67 +911,64 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)packet_trace_identity ("fetch-pack" ); heads =NULL ; -for (i =1 ; i < argc; i++) { +for (i =1 ; i < argc && *argv[i] =='-' ; i++) {const char *arg = argv[i]; -if (*arg =='-' ) { -if (!prefixcmp (arg,"--upload-pack=" )) { - args.uploadpack = arg +14 ; -continue ; - } -if (!prefixcmp (arg,"--exec=" )) { - args.uploadpack = arg +7 ; -continue ; - } -if (!strcmp ("--quiet" , arg) || !strcmp ("-q" , arg)) { - args.quiet =1 ; -continue ; - } -if (!strcmp ("--keep" , arg) || !strcmp ("-k" , arg)) { - args.lock_pack = args.keep_pack; - args.keep_pack =1 ; -continue ; - } -if (!strcmp ("--thin" , arg)) { - args.use_thin_pack =1 ; -continue ; - } -if (!strcmp ("--include-tag" , arg)) { - args.include_tag =1 ; -continue ; - } -if (!strcmp ("--all" , arg)) { - args.fetch_all =1 ; -continue ; - } -if (!strcmp ("--stdin" , arg)) { - args.stdin_refs =1 ; -continue ; - } -if (!strcmp ("-v" , arg)) { - args.verbose =1 ; -continue ; - } -if (!prefixcmp (arg,"--depth=" )) { - args.depth =strtol (arg +8 ,NULL ,0 ); -continue ; - } -if (!strcmp ("--no-progress" , arg)) { - args.no_progress =1 ; -continue ; - } -if (!strcmp ("--stateless-rpc" , arg)) { - args.stateless_rpc =1 ; -continue ; - } -if (!strcmp ("--lock-pack" , arg)) { - args.lock_pack =1 ; - pack_lockfile_ptr = &pack_lockfile; -continue ; - } -usage (fetch_pack_usage); +if (!prefixcmp (arg,"--upload-pack=" )) { + args.uploadpack = arg +14 ; +continue ; + } +if (!prefixcmp (arg,"--exec=" )) { + args.uploadpack = arg +7 ; +continue ; + } +if (!strcmp ("--quiet" , arg) || !strcmp ("-q" , arg)) { + args.quiet =1 ; +continue ; + } +if (!strcmp ("--keep" , arg) || !strcmp ("-k" , arg)) { + args.lock_pack = args.keep_pack; + args.keep_pack =1 ; +continue ; + } +if (!strcmp ("--thin" , arg)) { + args.use_thin_pack =1 ; +continue ; + } +if (!strcmp ("--include-tag" , arg)) { + args.include_tag =1 ; +continue ; + } +if (!strcmp ("--all" , arg)) { + args.fetch_all =1 ; +continue ; } -break ; +if (!strcmp ("--stdin" , arg)) { + args.stdin_refs =1 ; +continue ; + } +if (!strcmp ("-v" , arg)) { + args.verbose =1 ; +continue ; + } +if (!prefixcmp (arg,"--depth=" )) { + args.depth =strtol (arg +8 ,NULL ,0 ); +continue ; + } +if (!strcmp ("--no-progress" , arg)) { + args.no_progress =1 ; +continue ; + } +if (!strcmp ("--stateless-rpc" , arg)) { + args.stateless_rpc =1 ; +continue ; + } +if (!strcmp ("--lock-pack" , arg)) { + args.lock_pack =1 ; + pack_lockfile_ptr = &pack_lockfile; +continue ; + } +usage (fetch_pack_usage); }if (i < argc)
Signature · 1 line
1.7.10
PATCH v2 4/4 cmd_fetch_pack(): respect constness of argv parameter
In reply to [email protected]From: Michael Haggerty <[email protected]>
The old code cast away the constness of the strings passed to the function in argument argv[], which could result in their being modified by filter_refs(). Fix by copying reference names from argv and putting them into our own array (similarly to how refnames passed to stdin were already handled).
---
This change results in heads being left set to NULL if nr_heads==0, but all of the code paths are OK with this.
builtin/fetch-pack.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
builtin/fetch-pack.c +10 -13
@@ -899,10 +899,11 @@ static void fetch_pack_setup(void)int cmd_fetch_pack (int argc,const char **argv,const char *prefix) { -int i, ret, nr_heads; +int i, ret;struct ref *ref =NULL ;const char *dest =NULL ; -char **heads; +int alloc_heads =0 , nr_heads =0 ; +char **heads =NULL ;int fd[2 ];char *pack_lockfile =NULL ;char **pack_lockfile_ptr =NULL ;
@@ -910,7 +911,6 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)packet_trace_identity ("fetch-pack" ); - heads =NULL ;for (i =1 ; i < argc && *argv[i] =='-' ; i++) {const char *arg = argv[i];
@@ -976,17 +976,14 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)else usage (fetch_pack_usage); - heads = (char **)(argv + i); - nr_heads = argc - i; - +/* +* Copy refs from cmdline to growable list, then append any +* refs from the standard input: +*/ +ALLOC_GROW (heads, argc - i, alloc_heads); +for (; i < argc; i++) + heads[nr_heads++] =xstrdup (argv[i]);if (args.stdin_refs) { -/* -* Copy refs from cmdline to new growable list, then -* append the refs from the standard input. -*/ -int alloc_heads = nr_heads; -int size = nr_heads *sizeof (*heads); - heads =memcpy (xmalloc (size), heads, size);if (args.stateless_rpc) {/* in stateless RPC mode we use pkt-line to read * from stdin, until we get a flush packet
Signature · 1 line
1.7.10