nsxiv0f6cb93
Bug #165: Deletion of unnecessary null pointer checks
The function "free" performs input parameter validation.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html
It is therefore not needed to check a passed pointer before this function call.
A corresponding update suggestion was generated by the software "Coccinelle"
from the following semantic patch approach.
http://coccinelle.lip6.fr/
@Remove_unnecessary_pointer_checks1@
expression x;
@@
-if (x != \(0 \| NULL\))
free(x);
@Remove_unnecessary_pointer_checks2@
expression x;
@@
-if (x != \(0 \| NULL\)) {
free(x);
x = \(0 \| NULL\);
-}
@Remove_unnecessary_pointer_checks3@
expression a, b;
@@
-if (a != \(0 \| NULL\) && b != \(0 \| NULL\))
+if (a)
free(b);
@Remove_unnecessary_pointer_checks4@
expression a, b;
@@
-if (a != \(0 \| NULL\) && b != \(0 \| NULL\)) {
+if (a) {
free(b);
b = \(0 \| NULL\);
}
+10 −19
3 files changed
| @@ -810,8 +810,7 @@ int main(int argc, char **argv) | |||
| 810 | 810 | filename[len-1] = '\0'; | |
| 811 | 811 | check_add_file(filename); | |
| 812 | 812 | } | |
| 813 | − | if (filename != NULL) | |
| 814 | − | free(filename); | |
| 813 | + | free(filename); | |
| 815 | 814 | } | |
| 816 | 815 | ||
| 817 | 816 | for (i = 0; i < options->filecnt; i++) { | |
| @@ -176,8 +176,7 @@ void tns_init(tns_t *tns, const fileinfo_t *files, int cnt, int *sel, win_t *win | |||
| 176 | 176 | dsuffix = "/.cache"; | |
| 177 | 177 | } | |
| 178 | 178 | if (homedir != NULL) { | |
| 179 | − | if (cache_dir != NULL) | |
| 180 | − | free(cache_dir); | |
| 179 | + | free(cache_dir); | |
| 181 | 180 | len = strlen(homedir) + strlen(dsuffix) + 6; | |
| 182 | 181 | cache_dir = (char*) s_malloc(len); | |
| 183 | 182 | snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix); | |
| @@ -204,10 +203,8 @@ void tns_free(tns_t *tns) | |||
| 204 | 203 | tns->thumbs = NULL; | |
| 205 | 204 | } | |
| 206 | 205 | ||
| 207 | − | if (cache_dir != NULL) { | |
| 208 | − | free(cache_dir); | |
| 209 | − | cache_dir = NULL; | |
| 210 | − | } | |
| 206 | + | free(cache_dir); | |
| 207 | + | cache_dir = NULL; | |
| 211 | 208 | } | |
| 212 | 209 | ||
| 213 | 210 | bool tns_load(tns_t *tns, int n, bool force) | |
| @@ -198,18 +198,13 @@ char* absolute_path(const char *filename) | |||
| 198 | 198 | goto end; | |
| 199 | 199 | ||
| 200 | 200 | error: | |
| 201 | − | if (path != NULL) { | |
| 202 | − | free(path); | |
| 203 | − | path = NULL; | |
| 204 | − | } | |
| 201 | + | free(path); | |
| 202 | + | path = NULL; | |
| 205 | 203 | ||
| 206 | 204 | end: | |
| 207 | − | if (dirname != NULL) | |
| 208 | − | free(dirname); | |
| 209 | − | if (cwd != NULL) | |
| 210 | − | free(cwd); | |
| 211 | − | if (twd != NULL) | |
| 212 | − | free(twd); | |
| 205 | + | free(dirname); | |
| 206 | + | free(cwd); | |
| 207 | + | free(twd); | |
| 213 | 208 | ||
| 214 | 209 | return path; | |
| 215 | 210 | } | |
| @@ -254,7 +249,7 @@ int r_closedir(r_dir_t *rdir) | |||
| 254 | 249 | rdir->dir = NULL; | |
| 255 | 250 | } | |
| 256 | 251 | ||
| 257 | − | if (rdir->d != 0 && rdir->name != NULL) { | |
| 252 | + | if (rdir->d != 0) { | |
| 258 | 253 | free(rdir->name); | |
| 259 | 254 | rdir->name = NULL; | |
| 260 | 255 | } | |