[Openvas-commits] r3275 - in trunk/openvas-manager: . src

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri May 8 11:47:19 CEST 2009


Author: mattm
Date: 2009-05-08 11:47:18 +0200 (Fri, 08 May 2009)
New Revision: 3275

Modified:
   trunk/openvas-manager/ChangeLog
   trunk/openvas-manager/src/manage.c
   trunk/openvas-manager/src/manage.h
   trunk/openvas-manager/src/omp.c
   trunk/openvas-manager/src/otp.c
   trunk/openvas-manager/src/tasks_fs.h
   trunk/openvas-manager/src/tasks_sql.h
Log:
	Ensure that the memory allocated in sql_string is always freed.

	* src/manage.h (task_name, task_comment, task_description)
	(task_start_time, task_end_time, task_attack_state): Drop const from
	return type.

	* src/manage.c (task_name, task_comment, task_description)
	(task_start_time, task_end_time, task_attack_state): Drop const from
	return type.
	(print_tasks, create_report_file, task_preference): Free task string
	elements after use.

	* src/tasks_fs.h (task_comment): New function.
	(task_name, task_description, task_start_time, task_end_time)
	(task_attack_state): Return duplicate of task element.

	* src/tasks_sql.h (append_to_task_string): Free return from sql_string
	after use.
	(task_name, task_comment, task_description, task_start_time)
	(task_end_time, task_attack_state): Drop const from return type.

	* src/omp.c (omp_xml_handle_end_element): Free task string elements after
	use.

	* src/otp.c (save_report): Free task_start_time return after use.


Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2009-05-08 08:21:31 UTC (rev 3274)
+++ trunk/openvas-manager/ChangeLog	2009-05-08 09:47:18 UTC (rev 3275)
@@ -1,5 +1,33 @@
 2009-05-08  Matthew Mundell <matt at mundell.ukfsn.org>
 
+	Ensure that the memory allocated in sql_string is always freed.
+
+	* src/manage.h (task_name, task_comment, task_description)
+	(task_start_time, task_end_time, task_attack_state): Drop const from
+	return type.
+
+	* src/manage.c (task_name, task_comment, task_description)
+	(task_start_time, task_end_time, task_attack_state): Drop const from
+	return type.
+	(print_tasks, create_report_file, task_preference): Free task string
+	elements after use.
+
+	* src/tasks_fs.h (task_comment): New function.
+	(task_name, task_description, task_start_time, task_end_time)
+	(task_attack_state): Return duplicate of task element.
+
+	* src/tasks_sql.h (append_to_task_string): Free return from sql_string
+	after use.
+	(task_name, task_comment, task_description, task_start_time)
+	(task_end_time, task_attack_state): Drop const from return type.
+
+	* src/omp.c (omp_xml_handle_end_element): Free task string elements after
+	use.
+
+	* src/otp.c (save_report): Free task_start_time return after use.
+
+2009-05-08  Matthew Mundell <matt at mundell.ukfsn.org>
+
 	* src/ovas-mngr-comm.c (sendf_to_server): New function.
 
 	* src/ovas-mngr-comm.h (sendf_to_server): New header.

Modified: trunk/openvas-manager/src/manage.c
===================================================================
--- trunk/openvas-manager/src/manage.c	2009-05-08 08:21:31 UTC (rev 3274)
+++ trunk/openvas-manager/src/manage.c	2009-05-08 09:47:18 UTC (rev 3275)
@@ -472,13 +472,17 @@
     {
       do
         {
-          const char* comment = task_comment (index);
-          const char* description = task_description (index);
+          char* comment = task_comment (index);
+          char* description = task_description (index);
+          char* name = task_name (index);
           tracef ("   Task %u: \"%s\" %s\n%s\n\n",
                   task_id (index),
-                  task_name (index),
+                  name,
                   comment ? comment : "",
                   description ? description : "");
+          free (name);
+          free (description);
+          free (comment);
         }
       while (next_task (&iterator, &index));
     }
@@ -512,8 +516,14 @@
   assert (current_report == NULL);
   if (current_report) return -6;
 
-  tracef ("   Saving report (%s) on task %u\n",
-          task_start_time (task), task_id (task));
+#if TRACE
+  {
+    char* start_time = task_start_time (task);
+    tracef ("   Saving report (%s) on task %u\n",
+            task_start_time (task), task_id (task));
+    free (start_time);
+  }
+#endif
 
   if (task_id_string (task, &id)) return -2;
 
@@ -610,8 +620,9 @@
 static char*
 task_preference (task_t task, const char* name)
 {
-  const char* desc = task_description (task);
-  const char* seek;
+  char* desc = task_description (task);
+  char* orig_desc = desc;
+  char* seek;
   while ((seek = strchr (desc, '\n')))
     {
       char* eq = seek
@@ -625,7 +636,10 @@
                   desc);
 #endif
           if (strncmp (desc, name, eq - desc - 1) == 0)
-            return g_strndup (eq + 1, seek ? seek - eq + 1 : strlen (seek));
+            {
+              free (orig_desc);
+              return g_strndup (eq + 1, seek ? seek - eq + 1 : strlen (seek));
+            }
         }
       else if ((seek ? seek - desc > 7 : 1)
                && strncmp (desc, "begin(", 6) == 0)
@@ -650,6 +664,7 @@
       if (seek == NULL) break;
       desc = seek + 1;
     }
+  free (orig_desc);
   return NULL;
 }
 

Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h	2009-05-08 08:21:31 UTC (rev 3274)
+++ trunk/openvas-manager/src/manage.h	2009-05-08 09:47:18 UTC (rev 3275)
@@ -185,13 +185,13 @@
 int
 task_id_string (task_t, /*@out@*/ const char **);
 
-const char*
+char*
 task_name (task_t);
 
-const char*
+char*
 task_comment (task_t);
 
-const char*
+char*
 task_description (task_t);
 
 void
@@ -203,13 +203,13 @@
 void
 set_task_run_status (task_t, task_status_t);
 
-const char*
+char*
 task_start_time (task_t);
 
 void
 set_task_start_time (task_t task, char* time);
 
-const char*
+char*
 task_end_time (task_t);
 
 void
@@ -218,7 +218,7 @@
 unsigned int
 task_report_count (task_t);
 
-const char*
+char*
 task_attack_state (task_t);
 
 void

Modified: trunk/openvas-manager/src/omp.c
===================================================================
--- trunk/openvas-manager/src/omp.c	2009-05-08 08:21:31 UTC (rev 3274)
+++ trunk/openvas-manager/src/omp.c	2009-05-08 09:47:18 UTC (rev 3275)
@@ -1452,8 +1452,9 @@
           {
             gsize out_len;
             guchar* out;
-            out = g_base64_decode (task_description (current_client_task),
-                                   &out_len);
+            char* description = task_description (current_client_task);
+            out = g_base64_decode (description, &out_len);
+            free (description);
             set_task_description (current_client_task, (char*) out, out_len);
             set_client_state (CLIENT_NEW_TASK);
           }
@@ -1537,6 +1538,7 @@
             while (next_task (&iterator, &index))
               {
                 gchar* line;
+                char* name = task_name (index);
                 line = g_strdup_printf ("<task>"
                                         "<task_id>%u</task_id>"
                                         "<identifier>%s</identifier>"
@@ -1550,7 +1552,7 @@
                                         "</messages>"
                                         "</task>",
                                         task_id (index),
-                                        task_name (index),
+                                        name,
                                         task_run_status (index)
                                         == TASK_STATUS_NEW
                                         ? "New"
@@ -1566,7 +1568,7 @@
                                         task_infos_size (index),
                                         task_logs_size (index),
                                         task_notes_size (index));
-                // FIX free line if RESPOND fails
+                free (name);
                 if (send_to_client (line))
                   {
                     g_free (line);

Modified: trunk/openvas-manager/src/otp.c
===================================================================
--- trunk/openvas-manager/src/otp.c	2009-05-08 08:21:31 UTC (rev 3274)
+++ trunk/openvas-manager/src/otp.c	2009-05-08 09:47:18 UTC (rev 3275)
@@ -285,9 +285,13 @@
   assert (current_report != NULL);
   if (current_report == NULL) return -1;
 
-  tracef ("   Saving report (%s) on task %u\n",
-          task_start_time (task),
-          task_id (task));
+#if TRACE
+  {
+    char* start_time = task_start_time (task);
+    tracef ("   Saving report (%s) on task %u\n", start_time, task_id (task));
+    free (start_time);
+  }
+#endif
 
   if (fclose (current_report))
     {

Modified: trunk/openvas-manager/src/tasks_fs.h
===================================================================
--- trunk/openvas-manager/src/tasks_fs.h	2009-05-08 08:21:31 UTC (rev 3274)
+++ trunk/openvas-manager/src/tasks_fs.h	2009-05-08 09:47:18 UTC (rev 3275)
@@ -171,13 +171,26 @@
  *
  * @return Task name.
  */
-const char*
+char*
 task_name (task_t task)
 {
-  return task->name;
+  return g_strdup (task->name);
 }
 
 /**
+ * @brief Return the comment of a task.
+ *
+ * @param[in]  task  Task.
+ *
+ * @return Comment of task.
+ */
+char*
+task_comment (task_t task)
+{
+  return g_strdup (task->comment);
+}
+
+/**
  * @brief Return the description of a task.
  *
  * @param[in]  task  Task.
@@ -187,7 +200,7 @@
 char*
 task_description (task_t task)
 {
-  return task->description;
+  return g_strdup (task->description);
 }
 
 /**
@@ -241,7 +254,7 @@
 char*
 task_start_time (task_t task)
 {
-  return task->start_time;
+  return g_strdup (task->start_time);
 }
 
 /**
@@ -267,7 +280,7 @@
 char*
 task_end_time (task_t task)
 {
-  return task->end_time;
+  return g_strdup (task->end_time);
 }
 
 /**
@@ -306,7 +319,7 @@
 char*
 task_attack_state (task_t task)
 {
-  return task->attack_state;
+  return g_strdup (task->attack_state);
 }
 
 /**

Modified: trunk/openvas-manager/src/tasks_sql.h
===================================================================
--- trunk/openvas-manager/src/tasks_sql.h	2009-05-08 08:21:31 UTC (rev 3274)
+++ trunk/openvas-manager/src/tasks_sql.h	2009-05-08 09:47:18 UTC (rev 3275)
@@ -169,7 +169,7 @@
   return ret;
 }
 
-const unsigned char*
+unsigned char*
 sql_string (unsigned int col, unsigned int row, char* sql, ...)
 {
   sqlite3_stmt* stmt;
@@ -178,8 +178,8 @@
   sql_x (col, row, sql, args, &stmt);
   va_end (args);
   const unsigned char* ret2 = sqlite3_column_text (stmt, col);
-  // FIX probably need to dup ret before finalize
-  // FIX leak
+  /* TODO: For efficiency, save this duplication by adjusting the task
+           interface. */
   const unsigned char* ret = g_strdup (ret2);
   sqlite3_finalize (stmt);
   return ret;
@@ -234,7 +234,7 @@
 void
 append_to_task_string (task_t task, const char* field, const char* value)
 {
-  const unsigned char* current;
+  unsigned char* current;
   current = sql_string (0, 0,
                         "SELECT %s FROM tasks_%s WHERE ROWID = %llu;",
                         field,
@@ -244,6 +244,7 @@
   if (current)
     {
       gchar* new = g_strconcat ((const gchar*) current, value, NULL);
+      free (current);
       quote = sql_quote (new, strlen (new));
       g_free (new);
     }
@@ -426,6 +427,7 @@
                     "SELECT uuid FROM tasks_%s WHERE ROWID = %llu;",
                     current_credentials.username,
                     task);
+  // FIX caller must free
   *id = (const char*) str;
 #else
   *id = g_strdup_printf ("%llu", task);
@@ -440,7 +442,7 @@
  *
  * @return Task name.
  */
-const char*
+char*
 task_name (task_t task)
 {
   return sql_string (0, 0,
@@ -456,7 +458,7 @@
  *
  * @return Comment of task.
  */
-const char*
+char*
 task_comment (task_t task)
 {
   return sql_string (0, 0,
@@ -472,7 +474,7 @@
  *
  * @return Description of task.
  */
-const char*
+char*
 task_description (task_t task)
 {
   return sql_string (0, 0,
@@ -537,7 +539,7 @@
  *
  * @return Task start time.
  */
-const char*
+char*
 task_start_time (task_t task)
 {
   return sql_string (0, 0,
@@ -569,7 +571,7 @@
  *
  * @return Task end time.
  */
-const char*
+char*
 task_end_time (task_t task)
 {
   return sql_string (0, 0,
@@ -617,7 +619,7 @@
  *
  * @return Task attack state.
  */
-const char*
+char*
 task_attack_state (task_t task)
 {
   return sql_string (0, 0,



More information about the Openvas-commits mailing list