sparse: Fix mingw_main() argument number/type errors
PATCH 6/6 sparse: Fix mingw_main() argument number/type errors
Sparse issues 68 errors (two errors for each main() function) such as the following:
SP git.c
git.c:510:5: error: too many arguments for function mingw_main git.c:510:5: error: symbol 'mingw_main' redeclared with different type \
(originally declared at git.c:510) - different argument counts
The errors are caused by the 'main' macro used by the MinGW build to provide a replacement main() function. The original main function is effectively renamed to 'mingw_main' and is called from the new main function. The replacement main is used to execute certain actions common to all git programs on MinGW (e.g. ensure the standard I/O streams are in binary mode).
In order to suppress the errors, we change the macro to include the parameters in the declaration of the mingw_main function.
Unfortunately, this change provokes both sparse and gcc to complain about 9 calls to mingw_main(), such as the following:
CC git.o
git.c: In function 'main':
git.c:510: warning: passing argument 2 of 'mingw_main' from \
incompatible pointer type
git.c:510: note: expected 'const char **' but argument is of \
type 'char **'
In order to suppress these warnings, since both of the main functions need to be declared with the same prototype, we change the declaration of the 9 main functions, thus:
int main(int argc, char **argv)
--- compat/mingw.h | 4 ++-- credential-store.c | 4 ++-- fast-import.c | 4 ++-- git.c | 3 ++- remote-testsvn.c | 2 +- test-chmtime.c | 2 +- test-index-version.c | 2 +- test-mergesort.c | 2 +- test-parse-options.c | 4 ++-- test-subprocess.c | 4 ++-- 10 files changed, 16 insertions(+), 15 deletions(-)
compat/mingw.h +2 -2
@@ -346,8 +346,8 @@ extern CRITICAL_SECTION pinfo_cs;*/ #define main (c,v) dummy_decl_mingw_main(); \ -static int mingw_main(); \ -int main(int argc, const char **argv) \ +static int mingw_main(c,v); \ +int main(int argc, char **argv) \ { \ extern CRITICAL_SECTION pinfo_cs; \ _fmode = _O_BINARY; \
credential-store.c +2 -2
@@ -114,7 +114,7 @@ static int lookup_credential(const char *fn, struct credential *c)return c->username && c->password; } -int main (int argc,const char **argv) +int main (int argc,char **argv) {const char *const usage[] = {"git credential-store [options] <action>" ,
@@ -131,7 +131,7 @@ int main(int argc, const char **argv)umask (077 ); - argc =parse_options (argc, argv,NULL , options, usage,0 ); + argc =parse_options (argc, (const char **)argv,NULL , options, usage,0 );if (argc !=1 )usage_with_options (usage, options); op = argv[0 ];
fast-import.c +2 -2
@@ -297,7 +297,7 @@ static int failure;static FILE *pack_edges;static unsigned int show_stats =1 ;static int global_argc; -static const char **global_argv; +static char **global_argv;/* Memory pools */ static size_t mem_pool_alloc =2 *1024 *1024 -sizeof (struct mem_pool );
@@ -3347,7 +3347,7 @@ static void parse_argv(void)read_marks (); } -int main (int argc,const char **argv) +int main (int argc,char **argv) {unsigned int i;
git.c +2 -1
@@ -507,8 +507,9 @@ static int run_argv(int *argcp, const char ***argv) } -int main (int argc,const char **argv) +int main (int argc,char **av) { +const char **argv = (const char **) av;const char *cmd; startup_info = &git_startup_info;
remote-testsvn.c +1 -1
@@ -286,7 +286,7 @@ static int do_command(struct strbuf *line)return 0 ; } -int main (int argc,const char **argv) +int main (int argc,char **argv) {struct strbuf buf =STRBUF_INIT , url_sb =STRBUF_INIT , private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,
test-chmtime.c +1 -1
@@ -56,7 +56,7 @@ static int timespec_arg(const char *arg, long int *set_time, int *set_eq)return 1 ; } -int main (int argc,const char *argv[]) +int main (int argc,char *argv[]) {static int verbose;
test-index-version.c +1 -1
@@ -1,6 +1,6 @@#include "cache.h" -int main (int argc,const char **argv) +int main (int argc,char **argv) {struct cache_header hdr;int version;
test-mergesort.c +1 -1
@@ -22,7 +22,7 @@ static int compare_strings(const void *a, const void *b)return strcmp (x->text, y->text); } -int main (int argc,const char **argv) +int main (int argc,char **argv) {struct line *line, *p =NULL , *lines =NULL ;struct strbuf sb =STRBUF_INIT ;
test-parse-options.c +2 -2
@@ -29,7 +29,7 @@ static int number_callback(const struct option *opt, const char *arg, int unset)return 0 ; } -int main (int argc,const char **argv) +int main (int argc,char **argv) {const char *prefix ="prefix/" ;const char *usage[] = {
@@ -81,7 +81,7 @@ int main(int argc, const char **argv) };int i; - argc =parse_options (argc, argv, prefix, options, usage,0 ); + argc =parse_options (argc, (const char **)argv, prefix, options, usage,0 );printf ("boolean: %d\n" , boolean);printf ("integer: %u\n" , integer);
test-subprocess.c +2 -2
@@ -1,7 +1,7 @@#include "cache.h" #include "run-command.h" -int main (int argc,const char **argv) +int main (int argc,char **argv) {struct child_process cp;int nogit =0 ;
@@ -15,6 +15,6 @@ int main(int argc, const char **argv) }memset (&cp,0 ,sizeof (cp)); cp.git_cmd =1 ; - cp.argv = argv +1 ; + cp.argv = (const char **)argv +1 ;return run_command (&cp); }
Signature · 1 line
1.8.2
Am 27.04.2013 21:19, schrieb Ramsay Jones:
Quoted 5 lines
In order to suppress these warnings, since both of the main functions need to be declared with the same prototype, we change the declaration of the 9 main functions, thus:
int
main(int argc, char **argv)
Why not take the opposite direction with a patch like this? It's quick and dirty and based on v1.8.1.msysgit.1, as that was the version I had lying around here, but you get the idea.
René
--- compat/mingw.h | 4 ++-- daemon.c | 16 ++++++++-------- http-backend.c | 2 +- imap-send.c | 2 +- sh-i18n--envsubst.c | 2 +- shell.c | 2 +- show-index.c | 2 +- test-ctype.c | 2 +- test-date.c | 8 ++++---- test-delta.c | 2 +- test-dump-cache-tree.c | 2 +- test-genrandom.c | 2 +- test-line-buffer.c | 2 +- test-match-trees.c | 2 +- test-mktemp.c | 2 +- test-path-utils.c | 4 ++-- test-regex.c | 2 +- test-revision-walking.c | 2 +- test-run-command.c | 2 +- test-scrap-cache-tree.c | 2 +- test-sha1.c | 2 +- test-sigchain.c | 2 +- test-string-list.c | 2 +- test-svn-fe.c | 4 ++-- upload-pack.c | 6 +++--- 25 files changed, 40 insertions(+), 40 deletions(-)
compat/mingw.h +2 -2
@@ -452,11 +452,11 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen);void mingw_startup ();#define main (c,v) dummy_decl_mingw_main(); \ -static int mingw_main(); \ +static int mingw_main(int, const char **); \ int main(int argc, const char **argv) \ { \ mingw_startup(); \ - return mingw_main(__argc, __argv); \ + return mingw_main(argc, argv); \ } \ static int mingw_main(c,v)
daemon.c +8 -8
@@ -36,15 +36,15 @@ static const char daemon_usage[] =" [<directory>...]" ;/* List of acceptable pathname prefixes */ -static char **ok_paths; +static const char **ok_paths;static int strict_paths;/* If this is set, git-daemon-export-ok is not required */ static int export_all_trees;/* Take all paths relative to this one if non-NULL */ -static char *base_path; -static char *interpolated_path; +static const char *base_path; +static const char *interpolated_path;static int base_path_relaxed;/* Flag indicating client sent extra args. */
@@ -196,7 +196,7 @@ static const char *path_ok(char *directory) }if ( ok_paths && *ok_paths ) { -char **pp; +const char **pp;int pathlen =strlen (path);/* The validation is done on the paths after enter_repo
@@ -257,7 +257,7 @@ static int daemon_error(const char *dir, const char *msg)return -1 ; } -static char *access_hook; +static const char *access_hook;static int run_access_hook (struct daemon_service *service,const char *dir,const char *path) {
@@ -739,7 +739,7 @@ static void check_dead_children(void) cradle = &blanket->next; } -static char **cld_argv; +static const char **cld_argv;static void handle (int incoming,struct sockaddr *addr,socklen_t addrlen) {struct child_process cld = {NULL };
@@ -1164,7 +1164,7 @@ static int serve(struct string_list *listen_addr, int listen_port,return service_loop (&socklist); } -int main (int argc,char **argv) +int main (int argc,const char **argv) {int listen_port =0 ;struct string_list listen_addr =STRING_LIST_INIT_NODUP ;
@@ -1179,7 +1179,7 @@ int main(int argc, char **argv)git_extract_argv0_path (argv[0 ]);for (i =1 ; i < argc; i++) { -char *arg = argv[i]; +const char *arg = argv[i];if (!prefixcmp (arg,"--listen=" )) {string_list_append (&listen_addr,xstrdup_tolower (arg +9 ));
http-backend.c +1 -1
@@ -533,7 +533,7 @@ static struct service_cmd { {"POST", "/git-receive-pack$", service_rpc} }; -int main (int argc,char **argv) +int main (int argc,const char **argv) {char *method =getenv ("REQUEST_METHOD" );char *dir;
imap-send.c +1 -1
@@ -1502,7 +1502,7 @@ static int git_imap_config(const char *key, const char *val, void *cb)return 0 ; } -int main (int argc,char **argv) +int main (int argc,const char **argv) {struct msg_data all_msgs, msg;struct store *ctx =NULL ;
sh-i18n--envsubst.c +1 -1
@@ -64,7 +64,7 @@ static void note_variables (const char *string);static void subst_from_stdin (void );int -main (int argc,char *argv[]) +main (int argc,const char *argv[]) {/* Default values for command line options. */ /* unsigned short int show_variables = 0; */
shell.c +1 -1
@@ -129,7 +129,7 @@ static struct commands { { NULL }, }; -int main (int argc,char **argv) +int main (int argc,const char **argv) {char *prog;const char **user_argv;
show-index.c +1 -1
@@ -4,7 +4,7 @@static const char show_index_usage[] ="git show-index < <packed archive index>" ; -int main (int argc,char **argv) +int main (int argc,const char **argv) {int i;unsigned nr;
test-ctype.c +1 -1
@@ -28,7 +28,7 @@ static int is_in(const char *s, int ch)#define LOWER "abcdefghijklmnopqrstuvwxyz"#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ" -int main (int argc,char **argv) +int main (int argc,const char **argv) {TEST_CLASS (isdigit,DIGIT );TEST_CLASS (isspace," \n\r\t" );
test-date.c +4 -4
@@ -5,7 +5,7 @@ static const char *usage_msg = "\n"" test-date parse [date]...\n" " test-date approxidate [date]...\n" ; -static void show_dates (char **argv,struct timeval *now) +static void show_dates (const char **argv,struct timeval *now) {struct strbuf buf =STRBUF_INIT ;
@@ -17,7 +17,7 @@ static void show_dates(char **argv, struct timeval *now)strbuf_release (&buf); } -static void parse_dates (char **argv,struct timeval *now) +static void parse_dates (const char **argv,struct timeval *now) {for (; *argv; argv++) {char result[100 ];
@@ -34,7 +34,7 @@ static void parse_dates(char **argv, struct timeval *now) } } -static void parse_approxidate (char **argv,struct timeval *now) +static void parse_approxidate (const char **argv,struct timeval *now) {for (; *argv; argv++) {time_t t;
@@ -43,7 +43,7 @@ static void parse_approxidate(char **argv, struct timeval *now) } } -int main (int argc,char **argv) +int main (int argc,const char **argv) {struct timeval now;const char *x;
test-delta.c +1 -1
@@ -15,7 +15,7 @@static const char usage_str[] ="test-delta (-d|-p) <from_file> <data_file> <out_file>" ; -int main (int argc,char *argv[]) +int main (int argc,const char *argv[]) {int fd;struct stat st;
test-dump-cache-tree.c +1 -1
@@ -54,7 +54,7 @@ static int dump_cache_tree(struct cache_tree *it,return errs; } -int main (int ac,char **av) +int main (int ac,const char **av) {struct cache_tree *another =cache_tree ();if (read_cache () <0 )
test-genrandom.c +1 -1
@@ -6,7 +6,7 @@#include "git-compat-util.h" -int main (int argc,char *argv[]) +int main (int argc,const char *argv[]) {unsigned long count, next =0 ;unsigned char *c;
test-line-buffer.c +1 -1
@@ -50,7 +50,7 @@ static void handle_line(const char *line, struct line_buffer *stdin_buf)handle_command (line, arg +1 , stdin_buf); } -int main (int argc,char *argv[]) +int main (int argc,const char *argv[]) {struct line_buffer stdin_buf =LINE_BUFFER_INIT ;struct line_buffer file_buf =LINE_BUFFER_INIT ;
test-match-trees.c +1 -1
@@ -1,7 +1,7 @@#include "cache.h" #include "tree.h" -int main (int ac,char **av) +int main (int ac,const char **av) {unsigned char hash1[20 ], hash2[20 ], shifted[20 ];struct tree *one, *two;
test-mktemp.c +1 -1
@@ -3,7 +3,7 @@*/ #include "git-compat-util.h" -int main (int argc,char *argv[]) +int main (int argc,const char *argv[]) {if (argc !=2 )usage ("Expected 1 parameter defining the temporary file template" );
test-path-utils.c +2 -2
@@ -28,7 +28,7 @@ static int normalize_ceiling_entry(struct string_list_item *item, void *unused)return 1 ; } -int main (int argc,char **argv) +int main (int argc,const char **argv) {if (argc ==3 && !strcmp (argv[1 ],"normalize_path_copy" )) {char *buf =xmalloc (PATH_MAX +1 );
@@ -85,7 +85,7 @@ int main(int argc, char **argv) }if (argc >=4 && !strcmp (argv[1 ],"prefix_path" )) { -char *prefix = argv[2 ]; +const char *prefix = argv[2 ];int prefix_len =strlen (prefix);int nongit_ok;setup_git_directory_gently (&nongit_ok);
test-regex.c +1 -1
@@ -1,6 +1,6 @@#include <git-compat-util.h> -int main (int argc,char **argv) +int main (int argc,const char **argv) {char *pat ="[^={} \t]+" ;char *str ="={}\nfred" ;
test-revision-walking.c +1 -1
@@ -45,7 +45,7 @@ static int run_revision_walk(void)return got_revision; } -int main (int argc,char **argv) +int main (int argc,const char **argv) {if (argc <2 )return 1 ;
test-run-command.c +1 -1
@@ -13,7 +13,7 @@#include <string.h> #include <errno.h> -int main (int argc,char **argv) +int main (int argc,const char **argv) {struct child_process proc;
test-scrap-cache-tree.c +1 -1
@@ -4,7 +4,7 @@static struct lock_file index_lock; -int main (int ac,char **av) +int main (int ac,const char **av) {int fd =hold_locked_index (&index_lock,1 );if (read_cache () <0 )
test-sha1.c +1 -1
@@ -1,6 +1,6 @@#include "cache.h" -int main (int ac,char **av) +int main (int ac,const char **av) {git_SHA_CTX ctx;unsigned char sha1[20 ];
test-sigchain.c +1 -1
@@ -13,7 +13,7 @@ X(two) X(three)#undef X -int main (int argc,char **argv) { +int main (int argc,const char **argv) {sigchain_push (SIGTERM , one);sigchain_push (SIGTERM , two);sigchain_push (SIGTERM , three);
test-string-list.c +1 -1
@@ -41,7 +41,7 @@ static int prefix_cb(struct string_list_item *item, void *cb_data)return !prefixcmp (item->string, prefix); } -int main (int argc,char **argv) +int main (int argc,const char **argv) {if (argc ==5 && !strcmp (argv[1 ],"split" )) {struct string_list list =STRING_LIST_INIT_DUP ;
test-svn-fe.c +2 -2
@@ -11,7 +11,7 @@static const char test_svnfe_usage[] ="test-svn-fe (<dumpfile> | [-d] <preimage> <delta> <len>)" ; -static int apply_delta (int argc,char *argv[]) +static int apply_delta (int argc,const char *argv[]) {struct line_buffer preimage =LINE_BUFFER_INIT ;struct line_buffer delta =LINE_BUFFER_INIT ;
@@ -35,7 +35,7 @@ static int apply_delta(int argc, char *argv[])return 0 ; } -int main (int argc,char *argv[]) +int main (int argc,const char *argv[]) {if (argc ==2 ) {if (svndump_init (argv[1 ]))
upload-pack.c +3 -3
@@ -780,9 +780,9 @@ static void upload_pack(void) } } -int main (int argc,char **argv) +int main (int argc,const char **argv) { -char *dir; +const char *dir;int i;int strict =0 ;
@@ -793,7 +793,7 @@ int main(int argc, char **argv) read_replace_refs =0 ;for (i =1 ; i < argc; i++) { -char *arg = argv[i]; +const char *arg = argv[i];if (arg[0 ] !='-' )break ;
compat/mingw.h int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen); · line 456
Quoted 13 lines · René Scharfe
Why not take the opposite direction with a patch like this?
...compat/mingw.h +1
-1@@ -452,11 +452,11 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen);void mingw_startup ();#define main (c,v) dummy_decl_mingw_main(); \ -static int mingw_main(); \ +static int mingw_main(int, const char **); \ int main(int argc, const char **argv) \
But traditionally main is declared like
int main(int argc, char *argv[]);
without const, no?
Am 28.04.2013 21:31, schrieb Junio C Hamano:
Quoted 18 lines
compat/mingw.h int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen); · line 456
Quoted 13 lines · René Scharfe
Why not take the opposite direction with a patch like this?
...compat/mingw.h +1
-1@@ -452,11 +452,11 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen);void mingw_startup ();#define main (c,v) dummy_decl_mingw_main(); \ -static int mingw_main(); \ +static int mingw_main(int, const char **); \ int main(int argc, const char **argv) \But traditionally main is declared like
Quoted 3 lines
int main(int argc, char *argv[]);without const, no?
Yes, http://c-faq.com/ansi/maindecl.html and basically everybody else agree. Now that I actually think about it, the only benefit of declaring argv constant I can find is that the const'ness could easily spread to other variables and function arguments where it may actually matter. So please ignore my interjection. Or perhaps it might be worth mentioning in the commit message that removal of that "const" improves the code's standard compliance.
René
Quoted 23 lines · René Scharfe
Am 28.04.2013 21:31, schrieb Junio C Hamano:
Quoted 18 lines
compat/mingw.h int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen); · line 456
Quoted 13 lines · René Scharfe
Why not take the opposite direction with a patch like this?
...compat/mingw.h +1
-1@@ -452,11 +452,11 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen);void mingw_startup ();#define main (c,v) dummy_decl_mingw_main(); \ -static int mingw_main(); \ +static int mingw_main(int, const char **); \ int main(int argc, const char **argv) \But traditionally main is declared like
Quoted 3 lines
int main(int argc, char *argv[]);without const, no?
Quoted 7 lines
Yes, http://c-faq.com/ansi/maindecl.html and basically everybody else agree. Now that I actually think about it, the only benefit of declaring argv constant I can find is that the const'ness could easily spread to other variables and function arguments where it may actually matter. So please ignore my interjection. Or perhaps it might be worth mentioning in the commit message that removal of that "const" improves the code's standard compliance.
Hmm, well, strictly speaking, I can't say that is true! (see previous email). It is certainly true that it more closely follows the *spirit* of the standard, if not the letter of the law.
ATB,
Ramsay Jones
Quoted 10 lines · René Scharfe
Am 27.04.2013 21:19, schrieb Ramsay Jones:
Quoted 5 lines
In order to suppress these warnings, since both of the main functions need to be declared with the same prototype, we change the declaration of the 9 main functions, thus:
int
main(int argc, char **argv)Why not take the opposite direction with a patch like this? It's quick and dirty and based on v1.8.1.msysgit.1, as that was the version I had lying around here, but you get the idea.
The main reason (99%) was patch size. I only noticed a "recent trend" to declare the main() with a 'const char **argv' parameter because, over the years, I've had to keep adding to this patch. (Having said that, I suspect it has only been about four additions in four years).
The other 1% is simply that it just looks wrong! (yeah, this new 'const' thingamajig has it's uses, but still ... :-D ).
Just FYI, the C99 standard has this to say:
c99 standard 5.1.2.2.1 "Program startup"
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared);
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;^9 or in some other implementation-defined manner.
[note 9: Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char **argv, and so on.]
...
- The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
That "... or in some other implementation-defined manner." tends to make this text somewhat weak!
ATB,
Ramsay Jones