>From 02ddf11b990acb9e418c3c36236c3cfcc22f59b9 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 24 Sep 2015 08:31:07 -0700 Subject: [PATCH] Fix problems caught by static checking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Most of these can be caught by configuring with --enable-gcc-warnings. Problem reported by Pavel Raiskup in: http://lists.gnu.org/archive/html/bug-tar/2015-09/msg00001.html * src/buffer.c (format_total_stats): Prefer pointer to const when it’s OK to use const. (default_total_format): Now const. * src/buffer.c (default_total_format): * src/exclist.c (excfile_head, excfile_tail, vcs_ignore_files): * src/misc.c (namebuf_add_dir, namebuf_finish): * src/tar.c (verbatim_files_from_option, option_set_in_cl) (optloc_eq, set_old_files_option): Now static. * src/common.h: Adjust to match the other changes described here. * src/exclist.c (info_cleanup_exclist): * src/tar.c (argp_program_version, argp_program_bug_address): Remove; unused. (parse_default_options): Define via prototype instead of old style. (parse_default_options, decode_options): Fill out incomplete initializers. --- src/buffer.c | 12 +++++------ src/common.h | 8 +------- src/exclist.c | 62 ++++++++++++++++---------------------------------------- src/misc.c | 6 ++++-- src/tar.c | 65 ++++++++++++++++++++++++++++++----------------------------- 5 files changed, 61 insertions(+), 92 deletions(-) diff --git a/src/buffer.c b/src/buffer.c index 1a96595..17c4753 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -498,7 +498,7 @@ print_stats (FILE *fp, const char *text, tarlong numbytes) char abbr[LONGEST_HUMAN_READABLE + 1]; char rate[LONGEST_HUMAN_READABLE + 1]; int n = 0; - + int human_opts = human_autoscale | human_base_1024 | human_SI | human_B; if (text && text[0]) @@ -515,12 +515,12 @@ print_stats (FILE *fp, const char *text, tarlong numbytes) before each data item (bytes read, written, deleted, in that order). EOR is a delimiter to output after each item (used only if deleting from the archive), EOL is a delimiter to add at the end of the output - line. */ + line. */ int -format_total_stats (FILE *fp, const char **formats, int eor, int eol) +format_total_stats (FILE *fp, char const *const *formats, int eor, int eol) { int n; - + switch (subcommand_option) { case CREATE_SUBCOMMAND: @@ -539,7 +539,7 @@ format_total_stats (FILE *fp, const char **formats, int eor, int eol) fputc (eor, fp); n++; - + n += print_stats (fp, formats[TF_WRITE], prev_written + bytes_written); @@ -573,7 +573,7 @@ format_total_stats (FILE *fp, const char **formats, int eor, int eol) return n; } -const char *default_total_format[] = { +static char const *const default_total_format[] = { N_("Total bytes read"), /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */ N_("Total bytes written"), diff --git a/src/common.h b/src/common.h index b2f9c71..a75e114 100644 --- a/src/common.h +++ b/src/common.h @@ -386,9 +386,6 @@ GLOBAL dev_t root_device; /* Unquote filenames */ GLOBAL bool unquote_option; -/* Treat file names read from -T input verbatim */ -GLOBAL bool verbatim_files_from_option; - GLOBAL int savedir_sort_order; /* Show file or archive names after transformation. @@ -456,7 +453,7 @@ void set_start_time (void); #define TF_READ 0 #define TF_WRITE 1 #define TF_DELETED 2 -int format_total_stats (FILE *fp, const char **formats, int eor, int eol); +int format_total_stats (FILE *fp, char const *const *formats, int eor, int eol); void print_total_stats (void); void mv_begin_write (const char *file_name, off_t totsize, off_t sizeleft); @@ -623,8 +620,6 @@ typedef struct namebuf *namebuf_t; namebuf_t namebuf_create (const char *dir); void namebuf_free (namebuf_t buf); char *namebuf_name (namebuf_t buf, const char *name); -void namebuf_add_dir (namebuf_t buf, const char *name); -char *namebuf_finish (namebuf_t buf); const char *tar_dirname (void); @@ -953,7 +948,6 @@ extern void (*fatal_exit_hook) (void); void excfile_add (const char *name, int flags); void info_attach_exclist (struct tar_stat_info *dir); -void info_cleanup_exclist (struct tar_stat_info *dir); void info_free_exclist (struct tar_stat_info *dir); bool excluded_name (char const *name, struct tar_stat_info *st); void exclude_vcs_ignores (void); diff --git a/src/exclist.c b/src/exclist.c index 7cccc74..c24a00c 100644 --- a/src/exclist.c +++ b/src/exclist.c @@ -43,7 +43,7 @@ struct excfile char name[1]; }; -struct excfile *excfile_head, *excfile_tail; +static struct excfile *excfile_head, *excfile_tail; void excfile_add (const char *name, int flags) @@ -72,7 +72,7 @@ info_attach_exclist (struct tar_stat_info *dir) struct excfile *file; struct exclist *head = NULL, *tail = NULL, *ent; struct vcs_ignore_file *vcsfile; - + if (dir->exclude_list) return; for (file = excfile_head; file; file = file->next) @@ -102,7 +102,7 @@ info_attach_exclist (struct tar_stat_info *dir) if (vcsfile->initfn) vcsfile->data = vcsfile->initfn (vcsfile->data); - + if (add_exclude_fp (vcsfile->addfn, ex, fp, EXCLUDE_WILDCARDS|EXCLUDE_ANCHORED, '\n', vcsfile->data)) @@ -111,7 +111,7 @@ info_attach_exclist (struct tar_stat_info *dir) FATAL_ERROR ((0, e, "%s", quotearg_colon (file->name))); } fclose (fp); - + ent = xmalloc (sizeof (*ent)); ent->excluded = ex; ent->flags = file->flags == EXCL_DEFAULT @@ -130,34 +130,6 @@ info_attach_exclist (struct tar_stat_info *dir) } void -info_cleanup_exclist (struct tar_stat_info *dir) -{ - struct exclist *ep = dir->exclude_list; - - while (ep) - { - struct exclist *next = ep->next; - - if (ep->flags & EXCL_NON_RECURSIVE) - { - - /* Remove the entry */ - if (ep->prev) - ep->prev->next = ep->next; - else - dir->exclude_list = ep->next; - - if (ep->next) - ep->next->prev = ep->prev; - - free_exclude (ep->excluded); - free (ep); - } - ep = next; - } -} - -void info_free_exclist (struct tar_stat_info *dir) { struct exclist *ep = dir->exclude_list; @@ -172,7 +144,7 @@ info_free_exclist (struct tar_stat_info *dir) dir->exclude_list = NULL; } - + /* Return nonzero if file NAME is excluded. */ bool @@ -183,7 +155,7 @@ excluded_name (char const *name, struct tar_stat_info *st) char *bname = NULL; bool result; int nr = 0; - + name += FILE_SYSTEM_PREFIX_LEN (name); /* Try global exclusion list first */ @@ -192,7 +164,7 @@ excluded_name (char const *name, struct tar_stat_info *st) if (!st) return false; - + for (result = false; st && !result; st = st->parent, nr = EXCL_NON_RECURSIVE) { for (ep = st->exclude_list; ep; ep = ep->next) @@ -201,7 +173,7 @@ excluded_name (char const *name, struct tar_stat_info *st) continue; if ((result = excluded_file_name (ep->excluded, name))) break; - + if (!rname) { rname = name; @@ -229,8 +201,8 @@ cvs_addfn (struct exclude *ex, char const *pattern, int options, void *data) { struct wordsplit ws; size_t i; - - if (wordsplit (pattern, &ws, + + if (wordsplit (pattern, &ws, WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_SQUEEZE_DELIMS)) return; for (i = 0; i < ws.ws_wordc; i++) @@ -280,20 +252,20 @@ hg_initfn (void *data) { int *hgopt; static int hg_options; - + if (!data) hgopt = &hg_options; *hgopt = EXCLUDE_REGEX; return hgopt; } - + static void hg_addfn (struct exclude *ex, char const *pattern, int options, void *data) { int *hgopt = data; size_t len; - + while (isspace (*pattern)) ++pattern; if (*pattern == 0 || *pattern == '#') @@ -318,27 +290,27 @@ hg_addfn (struct exclude *ex, char const *pattern, int options, void *data) --len; p = xmalloc (len+1); - memcpy (p, pattern, len); + memcpy (p, pattern, len); p[len] = 0; pattern = p; exclude_add_pattern_buffer (ex, p); options |= FNM_LEADING_DIR|EXCLUDE_ALLOC; } - + add_exclude (ex, pattern, ((*hgopt == EXCLUDE_REGEX) ? (options & ~EXCLUDE_WILDCARDS) : (options & ~EXCLUDE_REGEX)) | *hgopt); } -struct vcs_ignore_file vcs_ignore_files[] = { +static struct vcs_ignore_file vcs_ignore_files[] = { { ".cvsignore", EXCL_NON_RECURSIVE, cvs_addfn, NULL, NULL }, { ".gitignore", 0, git_addfn, NULL, NULL }, { ".bzrignore", 0, bzr_addfn, NULL, NULL }, { ".hgignore", 0, hg_addfn, hg_initfn , NULL }, { NULL, 0, git_addfn, NULL, NULL } }; - + static struct vcs_ignore_file * get_vcs_ignore_file (const char *name) { diff --git a/src/misc.c b/src/misc.c index d263c07..4cbfa42 100644 --- a/src/misc.c +++ b/src/misc.c @@ -29,6 +29,8 @@ # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0 #endif +static void namebuf_add_dir (namebuf_t, char const *); +static char *namebuf_finish (namebuf_t); static const char *tar_getcdpath (int); @@ -1202,7 +1204,7 @@ namebuf_name (namebuf_t buf, const char *name) return buf->buffer; } -void +static void namebuf_add_dir (namebuf_t buf, const char *name) { static char dirsep[] = { DIRECTORY_SEPARATOR, 0 }; @@ -1215,7 +1217,7 @@ namebuf_add_dir (namebuf_t buf, const char *name) buf->dir_length += strlen (name); } -char * +static char * namebuf_finish (namebuf_t buf) { char *res = buf->buffer; diff --git a/src/tar.c b/src/tar.c index 0036a18..0a5e2ee 100644 --- a/src/tar.c +++ b/src/tar.c @@ -74,6 +74,9 @@ static int check_links_option; /* Number of allocated tape drive names. */ static size_t allocated_archive_names; +/* Treat file names read from -T input verbatim */ +static bool verbatim_files_from_option; + /* Miscellaneous. */ @@ -376,8 +379,6 @@ enum XATTR_INCLUDE }; -const char *argp_program_version = "tar (" PACKAGE_NAME ") " VERSION; -const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">"; static char const doc[] = N_("\ GNU 'tar' saves many files together into a single tape or disk archive, \ and can restore individual files from the archive.\n\ @@ -746,7 +747,7 @@ static struct argp_option options[] = { " for FILE itself"), GRID+1 }, {"exclude-ignore", EXCLUDE_IGNORE_OPTION, N_("FILE"), 0, N_("read exclude patterns for each directory from FILE, if it exists"), - GRID+1 }, + GRID+1 }, {"exclude-ignore-recursive", EXCLUDE_IGNORE_RECURSIVE_OPTION, N_("FILE"), 0, N_("read exclude patterns for each directory and its subdirectories " "from FILE, if it exists"), GRID+1 }, @@ -1027,8 +1028,8 @@ enum option_class OC_COMPRESS, /* Compress options: -JjZz, -I, etc. */ OC_OCCURRENCE, /* --occurrence */ OC_LISTED_INCREMENTAL, /* --listed-incremental */ - OC_NEWER, /* --newer, --newer-mtime, --after-date */ - OC_VERIFY, /* --verify */ + OC_NEWER, /* --newer, --newer-mtime, --after-date */ + OC_VERIFY, /* --verify */ OC_STARTING_FILE, /* --starting-file */ OC_SAME_ORDER, /* --same-order */ OC_ONE_TOP_LEVEL, /* --one-top-level */ @@ -1080,7 +1081,7 @@ optloc_lookup (int id) } /* Return true if the latest occurrence of option ID was in the command line */ -int +static int option_set_in_cl (int id) { struct option_locus *loc = optloc_lookup (id); @@ -1090,7 +1091,7 @@ option_set_in_cl (int id) } /* Compare two option locations */ -int +static int optloc_eq (struct option_locus *a, struct option_locus *b) { if (a->source != b->source) @@ -1302,7 +1303,7 @@ tar_help_filter (int key, const char *text, void *input) case LZOP_OPTION: s = xasprintf (_("filter the archive through %s"), LZOP_PROGRAM); - + case 'J': s = xasprintf (_("filter the archive through %s"), XZ_PROGRAM); break; @@ -1458,11 +1459,11 @@ static int sort_mode_flag[] = { ARGMATCH_VERIFY (sort_mode_arg, sort_mode_flag); -void +static void set_old_files_option (int code, struct option_locus *loc) { struct option_locus *prev; - static char *code_to_opt[] = { + static char const *const code_to_opt[] = { "--overwrite-dir", "--no-overwrite-dir", "--overwrite", @@ -1475,7 +1476,7 @@ set_old_files_option (int code, struct option_locus *loc) prev = optloc_save (OC_OLD_FILES, loc); if (prev && optloc_eq (loc, prev) && code != old_files_option) option_conflict_error (code_to_opt[code], code_to_opt[old_files_option]); - + old_files_option = code; } @@ -1483,7 +1484,7 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) { struct tar_args *args = state->input; - + switch (key) { case ARGP_KEY_ARG: @@ -1927,7 +1928,7 @@ parse_opt (int key, char *arg, struct argp_state *state) case EXCLUDE_IGNORE_RECURSIVE_OPTION: excfile_add (arg, EXCL_RECURSIVE); break; - + case EXCLUDE_TAG_OPTION: add_exclusion_tag (arg, exclusion_tag_contents, NULL); break; @@ -1947,7 +1948,7 @@ parse_opt (int key, char *arg, struct argp_state *state) case EXCLUDE_VCS_IGNORES_OPTION: exclude_vcs_ignores (); break; - + case FORCE_LOCAL_OPTION: force_local_option = true; break; @@ -2299,7 +2300,7 @@ parse_opt (int key, char *arg, struct argp_state *state) case NO_VERBATIM_FILES_FROM_OPTION: verbatim_files_from_option = false; break; - + case WARNING_OPTION: set_warning_option (arg); break; @@ -2375,7 +2376,7 @@ parse_opt (int key, char *arg, struct argp_state *state) else if (args->loc->source == OPTS_ENVIRON) error (0, 0, _("error parsing %s"), args->loc->name); exit (EX_USAGE); - + default: return ARGP_ERR_UNKNOWN; } @@ -2457,16 +2458,16 @@ more_options (int argc, char **argv, struct option_locus *loc) if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER|ARGP_NO_EXIT, &idx, &args)) abort (); /* shouldn't happen */ if (loc->source == OPTS_ENVIRON && args.input_files) - USAGE_ERROR ((0, 0, _("non-option arguments in %s"), loc->name)); + USAGE_ERROR ((0, 0, _("non-option arguments in %s"), loc->name)); } -void -parse_default_options () +static void +parse_default_options (void) { char *opts = getenv ("TAR_OPTIONS"); struct wordsplit ws; - struct option_locus loc = { OPTS_ENVIRON, "TAR_OPTIONS", 0 }; - + struct option_locus loc = { OPTS_ENVIRON, "TAR_OPTIONS", 0, 0 }; + if (!opts) return; @@ -2479,7 +2480,7 @@ parse_default_options () ws.ws_wordv[0] = program_invocation_short_name; more_options (ws.ws_offs + ws.ws_wordc, ws.ws_wordv, &loc); } - + wordsplit_free (&ws); } @@ -2487,8 +2488,8 @@ static void decode_options (int argc, char **argv) { int idx; - struct option_locus loc = { OPTS_COMMAND_LINE }; - + struct option_locus loc = { OPTS_COMMAND_LINE, 0, 0, 0 }; + argp_version_setup ("tar", tar_authors); /* Set some default option values. */ @@ -2508,7 +2509,7 @@ decode_options (int argc, char **argv) blocking_factor = DEFAULT_BLOCKING; record_size = DEFAULT_BLOCKING * BLOCKSIZE; excluded = new_exclude (); - + newer_mtime_option.tv_sec = TYPE_MINIMUM (time_t); newer_mtime_option.tv_nsec = -1; recursion_option = FNM_LEADING_DIR; @@ -2517,7 +2518,7 @@ decode_options (int argc, char **argv) tar_sparse_minor = 0; savedir_sort_order = SAVEDIR_SORT_NONE; - + owner_option = -1; owner_name_option = NULL; group_option = -1; group_name_option = NULL; @@ -2657,7 +2658,7 @@ decode_options (int argc, char **argv) occurrence_option = 0; } } - + if (archive_names == 0) { /* If no archive file name given, try TAPE from the environment, or @@ -2687,7 +2688,7 @@ decode_options (int argc, char **argv) else memset (&newer_mtime_option, 0, sizeof (newer_mtime_option)); } - + if (incremental_level != -1 && !listed_incremental_option) WARN ((0, 0, _("--level is meaningless without --listed-incremental"))); @@ -2790,7 +2791,7 @@ decode_options (int argc, char **argv) if (one_top_level_option) { char *base; - + if (absolute_names_option) { struct option_locus *one_top_level_loc = @@ -2805,7 +2806,7 @@ decode_options (int argc, char **argv) else one_top_level_option = false; } - + if (one_top_level_option && !one_top_level_dir) { /* If the user wants to guarantee that everything is under one @@ -2953,7 +2954,7 @@ main (int argc, char **argv) exit_failure = TAREXIT_FAILURE; exit_status = TAREXIT_SUCCESS; error_hook = checkpoint_flush_actions; - + filename_terminator = '\n'; set_quoting_style (0, DEFAULT_QUOTING_STYLE); @@ -3028,7 +3029,7 @@ main (int argc, char **argv) } checkpoint_finish (); - + if (totals_option) print_total_stats (); -- 2.1.0