From 8ef5d30ab82a8287aab1ff216cfc94118cbec896 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sat, 2 Jan 2016 19:41:17 +0100 Subject: [PATCH] Fix cppcheck warnings/suggestions --- ccache.c | 23 +++++++++++++---------- ccache.h | 1 - cleanup.c | 11 +++++------ compopt.c | 11 +---------- compopt.h | 1 - execute.c | 1 + exitfn.c | 5 +++-- lockfile.c | 3 ++- manifest.c | 17 ++++++----------- stats.c | 5 ++--- util.c | 31 ++++++++++--------------------- 11 files changed, 43 insertions(+), 66 deletions(-) diff --git a/ccache.c b/ccache.c index 621f67808..ee03e6e85 100644 --- a/ccache.c +++ b/ccache.c @@ -2,7 +2,7 @@ * ccache -- a fast C/C++ compiler cache * * Copyright (C) 2002-2007 Andrew Tridgell - * Copyright (C) 2009-2015 Joel Rosdahl + * Copyright (C) 2009-2016 Joel Rosdahl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -638,7 +638,7 @@ ignore: static char * make_relative_path(char *path) { - char *relpath, *canon_path, *path_suffix = NULL; + char *canon_path, *path_suffix = NULL; struct stat st; if (str_eq(conf->base_dir, "") || !str_startswith(path, conf->base_dir)) { @@ -666,6 +666,7 @@ make_relative_path(char *path) canon_path = x_realpath(path); if (canon_path) { + char *relpath; free(path); relpath = get_relative_path(get_current_working_dir(), canon_path); free(canon_path); @@ -907,11 +908,10 @@ void update_manifest_file(void) static void to_cache(struct args *args) { - char *tmp_stdout, *tmp_stderr, *tmp_aux, *tmp_cov; + char *tmp_stdout, *tmp_stderr, *tmp_cov; char *tmp_dwo = NULL; struct stat st; int status, tmp_stdout_fd, tmp_stderr_fd; - FILE *f; tmp_stdout = format("%s.tmp.stdout", cached_obj); tmp_stdout_fd = create_tmp_fd(&tmp_stdout); @@ -919,6 +919,7 @@ to_cache(struct args *args) tmp_stderr_fd = create_tmp_fd(&tmp_stderr); if (generating_coverage) { + char *tmp_aux; /* gcc has some funny rule about max extension length */ if (strlen(get_extension(output_obj)) < 6) { tmp_aux = remove_extension(output_obj); @@ -1108,9 +1109,8 @@ to_cache(struct args *args) if (generating_coverage) { /* gcc won't generate notes if there is no code */ if (stat(tmp_cov, &st) != 0 && errno == ENOENT) { + FILE *f = fopen(cached_cov, "wb"); cc_log("Creating placeholder: %s", cached_cov); - - f = fopen(cached_cov, "wb"); if (!f) { cc_log("Failed to create %s: %s", cached_cov, strerror(errno)); stats_update(STATS_ERROR); @@ -1372,7 +1372,7 @@ calculate_common_hash(struct args *args, struct mdfour *hash) { struct stat st; char *p; - const char *full_path = args->argv[0]; + const char *full_path; #ifdef _WIN32 const char *ext; char full_path_win_ext[MAX_PATH + 1] = {0}; @@ -1392,6 +1392,8 @@ calculate_common_hash(struct args *args, struct mdfour *hash) add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext, args->argv[0]); full_path = full_path_win_ext; +#else + full_path = args->argv[0]; #endif if (x_stat(full_path, &st) != 0) { @@ -1425,7 +1427,6 @@ calculate_common_hash(struct args *args, struct mdfour *hash) /* Possibly hash the coverage data file path. */ if (generating_coverage && profile_arcs) { - char *gcda_path; char *dir = dirname(output_obj); if (profile_dir) { dir = x_strdup(profile_dir); @@ -1435,6 +1436,7 @@ calculate_common_hash(struct args *args, struct mdfour *hash) dir = real_dir; } if (dir) { + char *gcda_path; char *base_name = basename(output_obj); p = remove_extension(base_name); free(base_name); @@ -1482,9 +1484,7 @@ static struct file_hash * calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode) { int i; - char *manifest_name; struct stat st; - int result; struct file_hash *object_hash = NULL; char *p; @@ -1643,6 +1643,9 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode) } if (direct_mode) { + char *manifest_name; + int result; + /* Hash environment variables that affect the preprocessor output. */ const char **p; const char *envvars[] = { diff --git a/ccache.h b/ccache.h index 1354423c4..ca96d3b20 100644 --- a/ccache.h +++ b/ccache.h @@ -155,7 +155,6 @@ char *strtok_r(char *str, const char *delim, char **saveptr); #endif int create_tmp_fd(char **fname); FILE *create_tmp_file(char **fname, const char *mode); -void create_empty_tmp_file(char **fname); const char *get_home_directory(void); char *get_cwd(void); bool same_executable_name(const char *s1, const char *s2); diff --git a/cleanup.c b/cleanup.c index df945e7b6..6eadaa443 100644 --- a/cleanup.c +++ b/cleanup.c @@ -1,6 +1,6 @@ /* * Copyright (C) 2002-2006 Andrew Tridgell - * Copyright (C) 2009-2015 Joel Rosdahl + * Copyright (C) 2009-2016 Joel Rosdahl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -134,7 +134,6 @@ static void sort_and_clean(void) { unsigned i; - const char *ext; char *last_base = x_strdup(""); if (num_files > 1) { @@ -144,6 +143,8 @@ sort_and_clean(void) /* delete enough files to bring us below the threshold */ for (i = 0; i < num_files; i++) { + const char *ext; + if ((cache_size_threshold == 0 || cache_size <= cache_size_threshold) && (files_in_cache_threshold == 0 @@ -227,11 +228,10 @@ cleanup_dir(struct conf *conf, const char *dir) /* cleanup in all cache subdirs */ void cleanup_all(struct conf *conf) { - char *dname; int i; for (i = 0; i <= 0xF; i++) { - dname = format("%s/%1x", conf->cache_dir, i); + char *dname = format("%s/%1x", conf->cache_dir, i); cleanup_dir(conf, dname); free(dname); } @@ -259,11 +259,10 @@ static void wipe_fn(const char *fname, struct stat *st) /* wipe all cached files in all subdirs */ void wipe_all(struct conf *conf) { - char *dname; int i; for (i = 0; i <= 0xF; i++) { - dname = format("%s/%1x", conf->cache_dir, i); + char *dname = format("%s/%1x", conf->cache_dir, i); traverse(dname, wipe_fn); free(dname); } diff --git a/compopt.c b/compopt.c index 86dd416ad..87166df4e 100644 --- a/compopt.c +++ b/compopt.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2015 Joel Rosdahl + * Copyright (C) 2010-2016 Joel Rosdahl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -182,15 +182,6 @@ compopt_takes_arg(const char *option) return co && (co->type & TAKES_ARG); } -/* Determines if argument takes a concatentated argument by comparing prefixes. - */ -bool -compopt_takes_concat_arg(const char *option) -{ - const struct compopt *co = find_prefix(option); - return co && (co->type & TAKES_CONCAT_ARG); -} - /* Determines if the prefix of the option matches any option and affects the * preprocessor. */ diff --git a/compopt.h b/compopt.h index 109094b66..882187dbc 100644 --- a/compopt.h +++ b/compopt.h @@ -9,7 +9,6 @@ bool compopt_too_hard(const char *option); bool compopt_too_hard_for_direct_mode(const char *option); bool compopt_takes_path(const char *option); bool compopt_takes_arg(const char *option); -bool compopt_takes_concat_arg(const char *option); bool compopt_prefix_affects_cpp(const char *option); #endif /* CCACHE_COMPOPT_H */ diff --git a/execute.c b/execute.c index 608a8f46f..8681d5fc1 100644 --- a/execute.c +++ b/execute.c @@ -84,6 +84,7 @@ win32argvtos(char *prefix, char **argv) *ptr++ = '\\'; *ptr++ = '"'; *ptr++ = ' '; + /* cppcheck-suppress unreadVariable */ } while ((arg = argv[i++])); ptr[-1] = '\0'; diff --git a/exitfn.c b/exitfn.c index 29b7b9f16..f59617a99 100644 --- a/exitfn.c +++ b/exitfn.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2015 Joel Rosdahl + * Copyright (C) 2010-2016 Joel Rosdahl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -83,9 +83,10 @@ exitfn_add(void (*function)(void *), void *context) void exitfn_call(void) { - struct exit_function *p = exit_functions, *q; + struct exit_function *p = exit_functions; exit_functions = NULL; while (p) { + struct exit_function *q; p->function(p->context); q = p; p = p->next; diff --git a/lockfile.c b/lockfile.c index f4f2028b7..b565e610a 100644 --- a/lockfile.c +++ b/lockfile.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2015 Joel Rosdahl + * Copyright (C) 2010-2016 Joel Rosdahl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -130,6 +130,7 @@ lockfile_acquire(const char *path, unsigned staleness_limit) } free(content); content = x_readlink(lockfile); + /* cppcheck-suppress nullPointer - false positive */ if (!content) { if (errno == ENOENT) { /* diff --git a/manifest.c b/manifest.c index 43d0def7e..54d04bd87 100644 --- a/manifest.c +++ b/manifest.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2015 Joel Rosdahl + * Copyright (C) 2009-2016 Joel Rosdahl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -380,17 +380,14 @@ verify_object(struct conf *conf, struct manifest *mf, struct object *obj, struct hashtable *stated_files, struct hashtable *hashed_files) { uint32_t i; - struct file_info *fi; struct file_hash *actual; - struct file_stats *st; struct mdfour hash; int result; - char *path; for (i = 0; i < obj->n_file_info_indexes; i++) { - fi = &mf->file_infos[obj->file_info_indexes[i]]; - path = mf->files[fi->index]; - st = hashtable_search(hashed_files, path); + struct file_info *fi = &mf->file_infos[obj->file_info_indexes[i]]; + char *path = mf->files[fi->index]; + struct file_stats *st = hashtable_search(hashed_files, path); if (!st) { struct stat file_stat; if (x_stat(path, &file_stat) != 0) { @@ -555,8 +552,6 @@ add_file_info_indexes(uint32_t *indexes, uint32_t size, { struct hashtable_itr *iter; uint32_t i; - char *path; - struct file_hash *file_hash; struct hashtable *mf_files; /* path --> index */ struct hashtable *mf_file_infos; /* struct file_info --> index */ @@ -569,8 +564,8 @@ add_file_info_indexes(uint32_t *indexes, uint32_t size, iter = hashtable_iterator(included_files); i = 0; do { - path = hashtable_iterator_key(iter); - file_hash = hashtable_iterator_value(iter); + char *path = hashtable_iterator_key(iter); + struct file_hash *file_hash = hashtable_iterator_value(iter); indexes[i] = get_file_hash_index(mf, path, file_hash, mf_files, mf_file_infos); i++; diff --git a/stats.c b/stats.c index 2d35ec9e8..5f217657a 100644 --- a/stats.c +++ b/stats.c @@ -1,6 +1,6 @@ /* * Copyright (C) 2002-2004 Andrew Tridgell - * Copyright (C) 2009-2015 Joel Rosdahl + * Copyright (C) 2009-2016 Joel Rosdahl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -110,11 +110,10 @@ parse_stats(struct counters *counters, const char *buf) size_t i = 0; const char *p; char *p2; - long val; p = buf; while (true) { - val = strtol(p, &p2, 10); + long val = strtol(p, &p2, 10); if (p2 == p) { break; } diff --git a/util.c b/util.c index a2ba7151d..6c89edae9 100644 --- a/util.c +++ b/util.c @@ -1,6 +1,6 @@ /* * Copyright (C) 2002 Andrew Tridgell - * Copyright (C) 2009-2015 Joel Rosdahl + * Copyright (C) 2009-2016 Joel Rosdahl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -66,12 +66,12 @@ static void log_prefix(bool log_updated_time) { #ifdef HAVE_GETTIMEOFDAY - char timestamp[100]; - struct timeval tv; - struct tm *tm; static char prefix[200]; if (log_updated_time) { + char timestamp[100]; + struct tm *tm; + struct timeval tv; gettimeofday(&tv, NULL); #ifdef __MINGW64_VERSION_MAJOR tm = localtime((time_t *)&tv.tv_sec); @@ -219,9 +219,9 @@ copy_fd(int fd_in, int fd_out) } while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) { - ssize_t count, written = 0; + ssize_t written = 0; do { - count = write(fd_out, buf + written, n - written); + ssize_t count = write(fd_out, buf + written, n - written); if (count == -1) { if (errno != EAGAIN && errno != EINTR) { fatal("Failed to copy fd"); @@ -327,10 +327,9 @@ copy_file(const char *src, const char *dest, int compress_level) if (compress_level > 0) { written = gzwrite(gz_out, buf, n); } else { - ssize_t count; written = 0; do { - count = write(fd_out, buf + written, n - written); + ssize_t count = write(fd_out, buf + written, n - written); if (count == -1 && errno != EINTR) { saved_errno = errno; break; @@ -627,7 +626,7 @@ format_hash_as_string(const unsigned char *hash, int size) sprintf(&ret[i*2], "%02x", (unsigned) hash[i]); } if (size >= 0) { - sprintf(&ret[i*2], "-%u", size); + sprintf(&ret[i*2], "-%d", size); } return ret; @@ -1086,7 +1085,7 @@ x_realpath(const char *path) { long maxlen = path_max(path); char *ret, *p; -#ifdef _WIN32 +#if !defined(HAVE_REALPATH) && defined(_WIN32) HANDLE path_handle; #endif @@ -1215,16 +1214,6 @@ create_tmp_file(char **fname, const char *mode) return file; } -/* - * Create an empty temporary file. *fname will be reallocated and set to the - * resulting filename. - */ -void -create_empty_tmp_file(char **fname) -{ - close(create_tmp_fd(fname)); -} - /* * Return current user's home directory, or NULL if it can't be determined. */ @@ -1340,7 +1329,6 @@ get_relative_path(const char *from, const char *to) { size_t common_prefix_len; int i; - const char *p; char *result; assert(from && is_absolute_path(from)); @@ -1360,6 +1348,7 @@ get_relative_path(const char *from, const char *to) result = x_strdup(""); common_prefix_len = common_dir_prefix_length(from, to); if (common_prefix_len > 0 || !str_eq(from, "/")) { + const char *p; for (p = from + common_prefix_len; *p; p++) { if (*p == '/') { reformat(&result, "../%s", result); -- 2.47.2