Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix free host segfault #590

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
[#538](https://github.com/greenbone/gvm-libs/pull/538)
- Add function for mqtt init status [#567](https://github.com/greenbone/gvm-libs/pull/567)
- Add function to get the severity_vector, otherwise the cvss_base_vector. [#568](https://github.com/greenbone/gvm-libs/pull/568)
- Add function to duplicate host and vhost objects [#590](https://github.com/greenbone/gvm-libs/pull/590)

### Changed

Expand Down
59 changes: 59 additions & 0 deletions base/hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,28 @@ gvm_vhost_free (gpointer vhost)
g_free (vhost);
}

/**
* @brief Creates a deep copy of a gvm_vhost_t object.
*
* @param vhost source vhost
* @param data dummy for g_slist_copy_deep
* @return gpointer copy of vhost
*/
gpointer
gvm_duplicate_vhost (gconstpointer vhost, gpointer data)
{
(void) (data);
gvm_vhost_t *ret = NULL;

if (!vhost)
return NULL;

ret = gvm_vhost_new (g_strdup (((gvm_vhost_t *) vhost)->value),
g_strdup (((gvm_vhost_t *) vhost)->source));

return ret;
}

/**
* @brief Creates a new gvm_host_t object.
*
Expand Down Expand Up @@ -1986,6 +2008,43 @@ gvm_host_find_in_hosts (const gvm_host_t *host, const struct in6_addr *addr,
return NULL;
}

/**
* @brief Creates a deep copy of a host. gvm_host_free has to be called on it.
*
* @param host source host
* @return gvm_host_t* copy of host
*/
gvm_host_t *
gvm_duplicate_host (gvm_host_t *host)
{
gvm_host_t *ret = NULL;

if (host == NULL)
return NULL;

ret = gvm_host_new ();

ret->type = host->type;
switch (host->type)
{
case HOST_TYPE_NAME:
ret->name = g_strdup (host->name);
break;
case HOST_TYPE_IPV4:
ret->addr.s_addr = host->addr.s_addr;
break;
case HOST_TYPE_IPV6:
ret->addr6.__in6_u = host->addr6.__in6_u;
break;
default:
g_free (ret);
return NULL;
}
ret->vhosts = g_slist_copy_deep (host->vhosts, gvm_duplicate_vhost, NULL);

return ret;
}

/**
* @brief Returns whether a host has an equal host in a hosts collection.
* eg. 192.168.10.1 has an equal in list created from
Expand Down
5 changes: 5 additions & 0 deletions base/hosts.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ gvm_host_add_reverse_lookup (gvm_host_t *);

void gvm_host_free (gpointer);

gpointer gvm_duplicate_vhost (gconstpointer, gpointer);

gvm_host_t *
gvm_duplicate_host (gvm_host_t *);

/* Miscellaneous functions */

gvm_vhost_t *
Expand Down