Krolyxon
Commit 0f6cb93

Branches

Refs →

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\);
 }
Author
Markus Elfring
Date
2014-08-21 20:51 +0200
Committer
Bert Münnich · 2014-08-22 19:24 +0200
Parent
75a6656
Commit
0f6cb93a0910de6e142971b81d3717c26d94e860

+10 −19

3 files changed

  1. M main.c +1 −2
  2. M thumbs.c +3 −6
  3. M util.c +6 −11
M main.c View
@@ -810,8 +810,7 @@ int main(int argc, char **argv)
810810 filename[len-1] = '\0';
811811 check_add_file(filename);
812812 }
813− if (filename != NULL)
814− free(filename);
813+ free(filename);
815814 }
816815
817816 for (i = 0; i < options->filecnt; i++) {
M thumbs.c View
@@ -176,8 +176,7 @@ void tns_init(tns_t *tns, const fileinfo_t *files, int cnt, int *sel, win_t *win
176176 dsuffix = "/.cache";
177177 }
178178 if (homedir != NULL) {
179− if (cache_dir != NULL)
180− free(cache_dir);
179+ free(cache_dir);
181180 len = strlen(homedir) + strlen(dsuffix) + 6;
182181 cache_dir = (char*) s_malloc(len);
183182 snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
@@ -204,10 +203,8 @@ void tns_free(tns_t *tns)
204203 tns->thumbs = NULL;
205204 }
206205
207− if (cache_dir != NULL) {
208− free(cache_dir);
209− cache_dir = NULL;
210− }
206+ free(cache_dir);
207+ cache_dir = NULL;
211208 }
212209
213210 bool tns_load(tns_t *tns, int n, bool force)
M util.c View
@@ -198,18 +198,13 @@ char* absolute_path(const char *filename)
198198 goto end;
199199
200200 error:
201− if (path != NULL) {
202− free(path);
203− path = NULL;
204− }
201+ free(path);
202+ path = NULL;
205203
206204 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);
213208
214209 return path;
215210 }
@@ -254,7 +249,7 @@ int r_closedir(r_dir_t *rdir)
254249 rdir->dir = NULL;
255250 }
256251
257− if (rdir->d != 0 && rdir->name != NULL) {
252+ if (rdir->d != 0) {
258253 free(rdir->name);
259254 rdir->name = NULL;
260255 }