]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Fix cppcheck warnings/suggestions
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 2 Jan 2016 18:41:17 +0000 (19:41 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 2 Jan 2016 18:41:17 +0000 (19:41 +0100)
ccache.c
ccache.h
cleanup.c
compopt.c
compopt.h
execute.c
exitfn.c
lockfile.c
manifest.c
stats.c
util.c

index 621f67808f40960c5e407f445e710b264b2146b2..ee03e6e852665bccce33f1300c3131badfbb1668 100644 (file)
--- 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[] = {
index 1354423c4373a1f22940e958faf8da0f95d2bc77..ca96d3b20d0874bac862e8abb6d2b7d413076946 100644 (file)
--- 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);
index df945e7b69f941351a5ea93a17ca2391e41d4aac..6eadaa443df0b8f5d14b9ebcd0ab4f0204fa8f5d 100644 (file)
--- 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);
        }
index 86dd416ad275b3c16d1046d1642e1f42798e990b..87166df4ec2b1f2964dc0bfab6313ca0ad0307ee 100644 (file)
--- 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.
  */
index 109094b66a135352230a461c958789564b4540bb..882187dbc06e1c280f8090910f788c01e9bb5a62 100644 (file)
--- 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 */
index 608a8f46f1bc375a1779075f822f0807041ee7a1..8681d5fc19b9946f5f6188a31befd57962e1d195 100644 (file)
--- 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';
 
index 29b7b9f16a4d00e652c01827de81dd48bd0f011c..f59617a99df4be551962dcbcb9eee265cc929f62 100644 (file)
--- 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;
index f4f2028b7d612ae442d69c40d7484900ecd79a92..b565e610ad509b87f4db626230591dd99dc53620 100644 (file)
@@ -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) {
                                /*
index 43d0def7e8074af53e333b415301c152e9eaca98..54d04bd87a0b57c5c29f937440fbbeb437ff5e35 100644 (file)
@@ -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 2d35ec9e8bdc56edfce0a9f635189a695abb3cc9..5f217657a27a8ffc1fafbcc7fdf1a90c0a5e437d 100644 (file)
--- 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 a2ba7151d8ddb3cce763b40741a4883b0853a799..6c89edae94abf006ab6ccf78060c4edb3dcdf41f 100644 (file)
--- 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);