From 7e9e90eb56b1aa81f562d0a3029177a7fb7c025d Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Wed, 13 Jul 2022 17:45:31 -0700 Subject: [PATCH] smyrna: fix incorrect use of 'strtok' MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This code was using `strtok` as if it splits based on the single separator passed to it. But `strtok` actually treats the second parameter as a list of character separators. In this change, we rephrase this code to do what its original author appears to have intended. This slightly changes the semantics of this code. But it seems we do not know the exact intent of the original, so this is hoped to match the author’s intention. Gitlab: fixes #2259 --- CHANGELOG.md | 1 + cmd/smyrna/gui/frmobjectui.c | 38 ++++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e67b334c..1bce7141a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - -Tx11: Assertion `xev.xbutton.button >= 1 && xev.xbutton.button <= 5 && "Xlib returned invalid button event"` failed #2256 - missing Perl includes patch #2262 +- smyrna: incorrect tokenization in frmobjectui.c:set_attr_object_type #2259 ## [5.0.0] – 2022-07-07 diff --git a/cmd/smyrna/gui/frmobjectui.c b/cmd/smyrna/gui/frmobjectui.c index 78645c454..1eb3442e9 100644 --- a/cmd/smyrna/gui/frmobjectui.c +++ b/cmd/smyrna/gui/frmobjectui.c @@ -8,6 +8,7 @@ * Contributors: Details at https://graphviz.org *************************************************************************/ +#include #include #include @@ -23,6 +24,8 @@ #include "gvprpipe.h" #include #include +#include +#include static int sel_node; static int sel_edge; @@ -217,30 +220,39 @@ static attr_data_type get_attr_data_type(char c) return attr_alpha; } -static void object_type_helper(char *a, int *t) -{ - if (strcmp(a, "GRAPH") == 0) +static void object_type_helper(strview_t a, int *t) { + if (strview_str_eq(a, "GRAPH")) t[0] = 1; - if (strcmp(a, "CLUSTER") == 0) + if (strview_str_eq(a, "CLUSTER")) t[0] = 1; - if (strcmp(a, "NODE") == 0) + if (strview_str_eq(a, "NODE")) t[1] = 1; - if (strcmp(a, "EDGE") == 0) + if (strview_str_eq(a, "EDGE")) t[2] = 1; - if (strcmp(a, "ANY_ELEMENT") == 0) { + if (strview_str_eq(a, "ANY_ELEMENT")) { t[0] = 1; t[1] = 1; t[2] = 1; } } -static void set_attr_object_type(char *str, int *t) -{ - char *a; - a = strtok(str, " "); +static void set_attr_object_type(const char *str, int *t) { + strview_t a = strview(str, ' '); + object_type_helper(a, t); + while (true) { + const char *start = a.data + a.size; + if (strncmp(start, " or ", strlen(" or ")) != 0) { + break; + } + start += strlen(" or "); + const char *end = strstr(start, " or "); + if (end == NULL) { + a = strview(start, '\0'); + } else { + a = (strview_t){.data = start, .size = (size_t)(end - start)}; + } object_type_helper(a, t); - while ((a = strtok(NULL, " or "))) - object_type_helper(a, t); + } } static attr_t *binarySearch(attr_list * l, char *searchKey) -- 2.40.0