From 6a74b06024a501b8840250082c0cb7198ee0c54b Mon Sep 17 00:00:00 2001 From: Jan-Oliver Wagner Date: Fri, 6 Sep 2019 13:15:38 +0200 Subject: [PATCH 1/5] Add nvti_add_tag() for adding a single tag to tags. So far it was only possible to set the entire tags string. Now it is possible to append a single tag. --- base/nvti.c | 40 ++++++++++++++++++++++++++++++++++++++++ base/nvti.h | 2 ++ 2 files changed, 42 insertions(+) diff --git a/base/nvti.c b/base/nvti.c index b05278d4e..12d430e3f 100644 --- a/base/nvti.c +++ b/base/nvti.c @@ -1057,6 +1057,46 @@ nvti_set_solution_type (nvti_t *n, const gchar *solution_type) return (0); } +/** + * @brief Add a tag to the NVT tags. + * + * @param n The NVT Info structure. + * + * @param name The tag name. A copy will be created from this. + * @param value The tag value. A copy will be created from this. + * + * @return 0 for success. Anything else indicates an error. + */ +int +nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value) +{ + char *new; + + if (!n) + return (-1); + + if (!name || !name[0]) + return (-2); + + if (!value || !value[0]) + return (-3); + + if (n->tag) + { + new = g_strconcat (n->tag, "|", name, "=", value, NULL); + g_free (n->tag); + } + else + new = g_strconcat (name, "=", value, NULL); + + if (new && new[0]) + n->tag = new; + else + n->tag = NULL; + + return (0); +} + /** * @brief Set the tags of a NVT. * diff --git a/base/nvti.h b/base/nvti.h index 1c8b668de..781f36b09 100644 --- a/base/nvti.h +++ b/base/nvti.h @@ -156,6 +156,8 @@ nvti_set_solution (nvti_t *, const gchar *); int nvti_set_solution_type (nvti_t *, const gchar *); int +nvti_add_tag (nvti_t *, const gchar *, const gchar *); +int nvti_set_tag (nvti_t *, const gchar *); int nvti_set_cvss_base (nvti_t *, const gchar *); From e6467e141d9a5d6d12adec646b1af716ae74048f Mon Sep 17 00:00:00 2001 From: Jan-Oliver Wagner Date: Fri, 6 Sep 2019 15:38:48 +0200 Subject: [PATCH 2/5] Avoid variable name "new". Apparently using a variable named "new" causes trouble to the code formatting routines. --- base/nvti.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/base/nvti.c b/base/nvti.c index 12d430e3f..a75795f55 100644 --- a/base/nvti.c +++ b/base/nvti.c @@ -1070,7 +1070,7 @@ nvti_set_solution_type (nvti_t *n, const gchar *solution_type) int nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value) { - char *new; + char *newtag; if (!n) return (-1); @@ -1083,14 +1083,14 @@ nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value) if (n->tag) { - new = g_strconcat (n->tag, "|", name, "=", value, NULL); + newtag = g_strconcat (n->tag, "|", name, "=", value, NULL); g_free (n->tag); } else - new = g_strconcat (name, "=", value, NULL); + newtag = g_strconcat (name, "=", value, NULL); - if (new && new[0]) - n->tag = new; + if (newtag && newtag[0]) + n->tag = newtag; else n->tag = NULL; From c5b4d77c08c6b68c7e3ca9f22e4bd77f51b15cb2 Mon Sep 17 00:00:00 2001 From: Jan-Oliver Wagner Date: Sat, 7 Sep 2019 17:54:44 +0200 Subject: [PATCH 3/5] Add corresponding CHANGELOG entry. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6f140de3..6247a664b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - A new data model for unified handling of cross references in the NVT meta data as been added. All previous API elements to handle cve, bid, xref werehas been removed. [#225](https://github.com/greenbone/gvm-libs/pull/225) [#232](https://github.com/greenbone/gvm-libs/pull/232). - A function to get an osp scan status and a enum type for the different status [#259](https://github.com/greenbone/gvm-libs/pull/259) - API functions for NVTI to handle timestamps [#261](https://github.com/greenbone/gvm-libs/pull/261) +- API function for NVTI to add a single tag [#263](https://github.com/greenbone/gvm-libs/pull/263) ### Changed - Change the default path to the redis socket to /run/redis/redis.sock [#256](https://github.com/greenbone/gvm-libs/pull/256) - Handle EAI_AGAIN in gvm_host_reverse_lookup() IPv6 case and function refactor. [#229](https://github.com/greenbone/gvm-libs/pull/229) From d707048219ae79f8125d2998e0d2c3e3f431a92e Mon Sep 17 00:00:00 2001 From: Jan-Oliver Wagner Date: Sun, 8 Sep 2019 10:28:06 +0200 Subject: [PATCH 4/5] Rework nvti_add_tag. Fix the types and condense the setting of the tag. --- base/nvti.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/base/nvti.c b/base/nvti.c index a75795f55..c1513a449 100644 --- a/base/nvti.c +++ b/base/nvti.c @@ -1060,8 +1060,7 @@ nvti_set_solution_type (nvti_t *n, const gchar *solution_type) /** * @brief Add a tag to the NVT tags. * - * @param n The NVT Info structure. - * + * @param n The NVT Info structure. * @param name The tag name. A copy will be created from this. * @param value The tag value. A copy will be created from this. * @@ -1070,7 +1069,7 @@ nvti_set_solution_type (nvti_t *n, const gchar *solution_type) int nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value) { - char *newtag; + gchar *newtag; if (!n) return (-1); @@ -1085,14 +1084,10 @@ nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value) { newtag = g_strconcat (n->tag, "|", name, "=", value, NULL); g_free (n->tag); + n->tag = newtag; } else - newtag = g_strconcat (name, "=", value, NULL); - - if (newtag && newtag[0]) - n->tag = newtag; - else - n->tag = NULL; + n->tag = g_strconcat (name, "=", value, NULL); return (0); } From 27d8fd2ee57417bf7a6812c28983d7dcbb7e8b49 Mon Sep 17 00:00:00 2001 From: Jan-Oliver Wagner Date: Sun, 8 Sep 2019 14:04:09 +0200 Subject: [PATCH 5/5] Move declaration to block where used. --- base/nvti.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/nvti.c b/base/nvti.c index c1513a449..c9eeaea89 100644 --- a/base/nvti.c +++ b/base/nvti.c @@ -1061,7 +1061,9 @@ nvti_set_solution_type (nvti_t *n, const gchar *solution_type) * @brief Add a tag to the NVT tags. * * @param n The NVT Info structure. + * * @param name The tag name. A copy will be created from this. + * * @param value The tag value. A copy will be created from this. * * @return 0 for success. Anything else indicates an error. @@ -1069,8 +1071,6 @@ nvti_set_solution_type (nvti_t *n, const gchar *solution_type) int nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value) { - gchar *newtag; - if (!n) return (-1); @@ -1082,6 +1082,8 @@ nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value) if (n->tag) { + gchar *newtag; + newtag = g_strconcat (n->tag, "|", name, "=", value, NULL); g_free (n->tag); n->tag = newtag;