{"thread":{"id":"22788","subject":"[PATCH] avoid marking char arrays static when they don't have to","startedAt":"2010-02-24T03:34:41Z","lastAt":"2010-02-24T03:34:41Z","messageCount":1,"participants":["Nicolas Pitre"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"135521","messageId":"alpine.LFD.2.00.1002232231110.1946@xanadu.home","threadId":"22788","inReplyTo":null,"subject":"[PATCH] avoid marking char arrays static when they don't have to","fromName":"Nicolas Pitre","fromEmail":"nico@fluxnic.net","sentAt":"2010-02-24T03:34:41Z","receivedAt":"2010-02-24T03:34:41Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"Many arrays of size PATH_MAX are marked static and therefore enlarge\nthe .bss segment uselessly.\n\nBefore:\n\n   text    data     bss     dec     hex filename\n1076212   21004  309216 1406432  1575e0 git\n\nAfter:\n\n   text    data     bss     dec     hex filename\n1076124   21004  263872 1361000  14c468 git\n\nOnly the trivial cases were modified.  Many still remain.\n\nSigned-off-by: Nicolas Pitre <nico@fluxnic.net>\n---\n\ndiff --git a/builtin-index-pack.c b/builtin-index-pack.c\nindex b4cf8c5..eb7e5b3 100644\n--- a/builtin-index-pack.c\n+++ b/builtin-index-pack.c\n@@ -171,7 +171,7 @@ static const char *open_pack_file(const char *pack_name)\n \tif (from_stdin) {\n \t\tinput_fd = 0;\n \t\tif (!pack_name) {\n-\t\t\tstatic char tmpfile[PATH_MAX];\n+\t\t\tchar tmpfile[PATH_MAX];\n \t\t\toutput_fd = odb_mkstemp(tmpfile, sizeof(tmpfile),\n \t\t\t\t\t\t\"pack/tmp_pack_XXXXXX\");\n \t\t\tpack_name = xstrdup(tmpfile);\ndiff --git a/builtin-init-db.c b/builtin-init-db.c\nindex dd84cae..9ebb6dd 100644\n--- a/builtin-init-db.c\n+++ b/builtin-init-db.c\n@@ -169,7 +169,7 @@ static int create_default_files(const char *template_path)\n {\n \tconst char *git_dir = get_git_dir();\n \tunsigned len = strlen(git_dir);\n-\tstatic char path[PATH_MAX];\n+\tchar path[PATH_MAX];\n \tstruct stat st1;\n \tchar repo_version_string[10];\n \tchar junk[2];\n@@ -443,7 +443,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)\n \t\tusage(init_db_usage[0]);\n \t}\n \tif (is_bare_repository_cfg == 1) {\n-\t\tstatic char git_dir[PATH_MAX+1];\n+\t\tchar git_dir[PATH_MAX+1];\n \n \t\tsetenv(GIT_DIR_ENVIRONMENT,\n \t\t\tgetcwd(git_dir, sizeof(git_dir)), 0);\ndiff --git a/builtin-prune-packed.c b/builtin-prune-packed.c\nindex f9463de..2e98f45 100644\n--- a/builtin-prune-packed.c\n+++ b/builtin-prune-packed.c\n@@ -42,7 +42,7 @@ static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)\n void prune_packed_objects(int opts)\n {\n \tint i;\n-\tstatic char pathname[PATH_MAX];\n+\tchar pathname[PATH_MAX];\n \tconst char *dir = get_object_directory();\n \tint len = strlen(dir);\n \ndiff --git a/builtin-receive-pack.c b/builtin-receive-pack.c\nindex 0559fcc..43914b1 100644\n--- a/builtin-receive-pack.c\n+++ b/builtin-receive-pack.c\n@@ -190,7 +190,6 @@ static int copy_to_sideband(int in, int out, void *arg)\n \n static int run_receive_hook(const char *hook_name)\n {\n-\tstatic char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];\n \tstruct command *cmd;\n \tstruct child_process proc;\n \tstruct async muxer;\n@@ -232,6 +231,7 @@ static int run_receive_hook(const char *hook_name)\n \n \tfor (cmd = commands; cmd; cmd = cmd->next) {\n \t\tif (!cmd->error_string) {\n+\t\t\tchar buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];\n \t\t\tsize_t n = snprintf(buf, sizeof(buf), \"%s %s %s\\n\",\n \t\t\t\tsha1_to_hex(cmd->old_sha1),\n \t\t\t\tsha1_to_hex(cmd->new_sha1),\ndiff --git a/builtin-rev-parse.c b/builtin-rev-parse.c\nindex a8c5043..2819b89 100644\n--- a/builtin-rev-parse.c\n+++ b/builtin-rev-parse.c\n@@ -636,7 +636,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t}\n \t\t\tif (!strcmp(arg, \"--git-dir\")) {\n \t\t\t\tconst char *gitdir = getenv(GIT_DIR_ENVIRONMENT);\n-\t\t\t\tstatic char cwd[PATH_MAX];\n+\t\t\t\tchar cwd[PATH_MAX];\n \t\t\t\tif (gitdir) {\n \t\t\t\t\tputs(gitdir);\n \t\t\t\t\tcontinue;\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex ab65f77..f0b14dd 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -190,7 +190,7 @@ static int do_lstat(const char *file_name, struct stat *buf)\n int mingw_lstat(const char *file_name, struct stat *buf)\n {\n \tint namelen;\n-\tstatic char alt_name[PATH_MAX];\n+\tchar alt_name[PATH_MAX];\n \n \tif (!do_lstat(file_name, buf))\n \t\treturn 0;\ndiff --git a/daemon.c b/daemon.c\nindex 3769b6f..a0e655d 100644\n--- a/daemon.c\n+++ b/daemon.c\n@@ -105,8 +105,8 @@ static void NORETURN daemon_die(const char *err, va_list params)\n \n static char *path_ok(char *directory)\n {\n-\tstatic char rpath[PATH_MAX];\n-\tstatic char interp_path[PATH_MAX];\n+\tchar rpath[PATH_MAX];\n+\tchar interp_path[PATH_MAX];\n \tchar *path;\n \tchar *dir;\n \ndiff --git a/entry.c b/entry.c\nindex 004182c..fabc590 100644\n--- a/entry.c\n+++ b/entry.c\n@@ -194,7 +194,7 @@ static int check_path(const char *path, int len, struct stat *st, int skiplen)\n \n int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)\n {\n-\tstatic char path[PATH_MAX + 1];\n+\tchar path[PATH_MAX + 1];\n \tstruct stat st;\n \tint len = state->base_dir_len;\n \ndiff --git a/fast-import.c b/fast-import.c\nindex 309f2c5..02083f2 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -829,7 +829,7 @@ static struct tree_content *dup_tree_content(struct tree_content *s)\n \n static void start_packfile(void)\n {\n-\tstatic char tmpfile[PATH_MAX];\n+\tchar tmpfile[PATH_MAX];\n \tstruct packed_git *p;\n \tstruct pack_header hdr;\n \tint pack_fd;\n@@ -905,7 +905,7 @@ static char *keep_pack(const char *curr_index_name)\n \n static void unkeep_all_packs(void)\n {\n-\tstatic char name[PATH_MAX];\n+\tchar name[PATH_MAX];\n \tint k;\n \n \tfor (k = 0; k < pack_id; k++) {\ndiff --git a/git.c b/git.c\nindex 61a5408..4483a85 100644\n--- a/git.c\n+++ b/git.c\n@@ -122,7 +122,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)\n \t\t\tif (envchanged)\n \t\t\t\t*envchanged = 1;\n \t\t} else if (!strcmp(cmd, \"--bare\")) {\n-\t\t\tstatic char git_dir[PATH_MAX+1];\n+\t\t\tchar git_dir[PATH_MAX+1];\n \t\t\tis_bare_repository_cfg = 1;\n \t\t\tsetenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);\n \t\t\tif (envchanged)\ndiff --git a/pack-write.c b/pack-write.c\nindex a905ca4..e60f874 100644\n--- a/pack-write.c\n+++ b/pack-write.c\n@@ -43,7 +43,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec\n \t\tsorted_by_sha = list = last = NULL;\n \n \tif (!index_name) {\n-\t\tstatic char tmpfile[PATH_MAX];\n+\t\tchar tmpfile[PATH_MAX];\n \t\tfd = odb_mkstemp(tmpfile, sizeof(tmpfile), \"pack/tmp_idx_XXXXXX\");\n \t\tindex_name = xstrdup(tmpfile);\n \t} else {\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 006321e..ca366eb 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -2286,7 +2286,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,\n \tgit_SHA_CTX c;\n \tunsigned char parano_sha1[20];\n \tchar *filename;\n-\tstatic char tmpfile[PATH_MAX];\n+\tchar tmpfile[PATH_MAX];\n \n \tfilename = sha1_file_name(sha1);\n \tfd = create_tmpfile(tmpfile, sizeof(tmpfile), filename);\ndiff --git a/transport.c b/transport.c\nindex 1a360cf..0b2638a 100644\n--- a/transport.c\n+++ b/transport.c\n@@ -97,7 +97,7 @@ static int read_loose_refs(struct strbuf *path, int name_offset,\n static void insert_packed_refs(const char *packed_refs, struct ref **list)\n {\n \tFILE *f = fopen(packed_refs, \"r\");\n-\tstatic char buffer[PATH_MAX];\n+\tchar buffer[PATH_MAX];\n \n \tif (!f)\n \t\treturn;\n"}]}