diff --git a/util/cpeutils.c b/util/cpeutils.c index a70ff783..24d53634 100644 --- a/util/cpeutils.c +++ b/util/cpeutils.c @@ -783,6 +783,7 @@ get_fs_component (const char *fs_cpe, int index) char *component = NULL; char *c; char *component_start, *component_end; + gboolean escaped; if (!fs_cpe) return NULL; @@ -793,27 +794,38 @@ get_fs_component (const char *fs_cpe, int index) c = (char *) fs_cpe; /* find start of component */ + escaped = FALSE; if (index == 0) component_start = c; else { for (int i = 0; *c != '\0' && i < index; c++) { - if (*c == ':' && c == fs_cpe) - i++; - else if (c > fs_cpe && *c == ':' && *(c - 1) != '\\') + if (*c == ':' && !escaped) i++; + else if (*c == '\\' && !escaped) + escaped = TRUE; + else + escaped = FALSE; } component_start = c; } /* find end of component */ + escaped = FALSE; if (*component_start == '\0') component_end = component_start; else { - for (c = component_start; *c != '\0' && *c != ':'; c++) - ; + for (c = component_start; *c != '\0'; c++) + { + if (*c == ':' && !escaped) + break; + if (*c == '\\' && !escaped) + escaped = TRUE; + else + escaped = FALSE; + } } component_end = c; diff --git a/util/cpeutils_tests.c b/util/cpeutils_tests.c index cd97e39d..6e4d2cb9 100644 --- a/util/cpeutils_tests.c +++ b/util/cpeutils_tests.c @@ -212,6 +212,43 @@ Ensure (cpeutils, fs_cpe_to_uri_cpe) "cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win2003~x64~")); g_free (uri_cpe); + fs_cpe = + "cpe:2.3:a:hp:insight_diagnostics:7\\:4.0.1570:-:*:*:online:win2003:x64:*"; + uri_cpe = fs_cpe_to_uri_cpe (fs_cpe); + assert_that ( + uri_cpe, + is_equal_to_string ( + "cpe:/a:hp:insight_diagnostics:7%3A4.0.1570:-:~~online~win2003~x64~")); + g_free (uri_cpe); + + fs_cpe = + "cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:win\\:2003:x64:*"; + uri_cpe = fs_cpe_to_uri_cpe (fs_cpe); + assert_that ( + uri_cpe, + is_equal_to_string ( + "cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win%3A2003~x64~")); + g_free (uri_cpe); + + fs_cpe = "cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:win\\:\\:" + "2003:x64:*"; + uri_cpe = fs_cpe_to_uri_cpe (fs_cpe); + assert_that ( + uri_cpe, + is_equal_to_string ( + "cpe:/" + "a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win%3A%3A2003~x64~")); + g_free (uri_cpe); + + fs_cpe = "cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:" + "win2003\\\\:x64:*"; + uri_cpe = fs_cpe_to_uri_cpe (fs_cpe); + assert_that ( + uri_cpe, + is_equal_to_string ( + "cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win2003%5C~x64~")); + g_free (uri_cpe); + fs_cpe = "This is a ~:SIGNAL:~ test."; uri_cpe = fs_cpe_to_uri_cpe (fs_cpe); g_free (uri_cpe);