Fix some issues found by 'parfait' static analysis scanner.
Even if these are not 'real' bugs, the changes look like good programming.

Please review.

Tim
From aa34caf27a2d6a4bf245aa4b94518b0ee47d648b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim Rühsen?= <tim.rueh...@gmx.de>
Date: Mon, 1 Dec 2014 17:02:33 +0100
Subject: [PATCH] Fix issues reported by static code analysis tool 'parfait'

Closes: #41235
Reported-by: Jiri Kukacka
---
 src/ChangeLog |  8 ++++++++
 src/connect.c |  2 +-
 src/iri.c     | 11 +++++++----
 src/url.c     | 29 ++++++++++++++++-------------
 src/warc.c    |  5 ++++-
 5 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 920d822..97a895e 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,11 @@
+2014-12-01  Tim Ruehsen <tim.rueh...@gmx.de>
+
+	* connect.c, iri.c, url.c, warc.c: Fix issues reported
+	by static code analysis tool 'parfait'.
+
+	Closes: #41235
+	Reported-by: Jiri Kukacka
+
 2014-11-29  Tim Ruehsen <tim.rueh...@gmx.de>

 	* utils.h: xfree() sets argument to NULL after freeing,
diff --git a/src/connect.c b/src/connect.c
index 727d6a6..661a6ba 100644
--- a/src/connect.c
+++ b/src/connect.c
@@ -844,7 +844,7 @@ void *
 fd_transport_context (int fd)
 {
   struct transport_info *info = hash_table_get (transport_map, (void *)(intptr_t) fd);
-  return info->ctx;
+  return info ? info->ctx : NULL;
 }

 /* When fd_read/fd_write are called multiple times in a loop, they should
diff --git a/src/iri.c b/src/iri.c
index 849b101..0686f30 100644
--- a/src/iri.c
+++ b/src/iri.c
@@ -329,10 +329,13 @@ struct iri *iri_dup (const struct iri *src)
 void
 iri_free (struct iri *i)
 {
-  xfree (i->uri_encoding);
-  xfree (i->content_encoding);
-  xfree (i->orig_url);
-  xfree (i);
+  if (i)
+    {
+      xfree (i->uri_encoding);
+      xfree (i->content_encoding);
+      xfree (i->orig_url);
+      xfree (i);
+    }
 }

 /* Set uri_encoding of struct iri i. If a remote encoding was specified, use
diff --git a/src/url.c b/src/url.c
index bc91d4e..466ee77 100644
--- a/src/url.c
+++ b/src/url.c
@@ -575,8 +575,8 @@ rewrite_shorthand_url (const char *url)
         goto http;

       /* Turn "foo.bar.com:path" to "ftp://foo.bar.com/path";. */
-      ret = aprintf ("ftp://%s";, url);
-      ret[6 + (p - url)] = '/';
+      if ((ret = aprintf ("ftp://%s";, url)) != NULL)
+        ret[6 + (p - url)] = '/';
     }
   else
     {
@@ -1173,20 +1173,23 @@ url_set_file (struct url *url, const char *newfile)
 void
 url_free (struct url *url)
 {
-  xfree (url->host);
-  xfree (url->path);
-  xfree (url->url);
+  if (url)
+    {
+      xfree (url->host);
+      xfree (url->path);
+      xfree (url->url);

-  xfree (url->params);
-  xfree (url->query);
-  xfree (url->fragment);
-  xfree (url->user);
-  xfree (url->passwd);
+      xfree (url->params);
+      xfree (url->query);
+      xfree (url->fragment);
+      xfree (url->user);
+      xfree (url->passwd);

-  xfree (url->dir);
-  xfree (url->file);
+      xfree (url->dir);
+      xfree (url->file);

-  xfree (url);
+      xfree (url);
+    }
 }

 /* Create all the necessary directories for PATH (a file).  Calls
diff --git a/src/warc.c b/src/warc.c
index e2ad09b..5bdda1b 100644
--- a/src/warc.c
+++ b/src/warc.c
@@ -1201,7 +1201,10 @@ warc_tempfile (void)

 #if !O_TEMPORARY
   if (unlink (filename) < 0)
-    return NULL;
+    {
+      close(fd);
+      return NULL;
+    }
 #endif

   return fdopen (fd, "wb+");
--
2.1.3

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to