Skip to content

Commit d68cd2e

Browse files
committed
fix(diagram): set an empty PlantUML operand id when there is none
dg_puml_operand set *label up front but *id only on its success paths, so an opener without its closer (`Alice ->: x`, `[-> Alice: x`, `class [Unclosed`) returned with *id unwritten. dg_puml_relation and dg_puml_decl then read an uninitialized dg_str_t and passed it to dg_element, which crashed the index worker on some stacks. The operand now starts with an empty id, so those lines take the existing `!lid.n` / `!rid.n` exits and declarations make no element. Fixes DeusData#2818 Signed-off-by: Rodion Kazennov <nefayran@gmail.com>
1 parent 3be5a02 commit d68cd2e

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎internal/cbm/diagram.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,9 +1225,11 @@ static bool dg_word_at(dg_str_t s, const char *word) {
12251225
}
12261226

12271227
/* An operand of a PlantUML line: `[Comp]`, `(use case)`, `:actor:`, `"text"`,
1228-
* an id, or `[*]`; its id in *id; the position after it (i when none). */
1228+
* an id, or `[*]`; its id in *id; the position after it (i and an empty *id
1229+
* when none, as for the diagram edge in `A ->: x` or `[-> A: x`). */
12291230
static size_t dg_puml_operand(dg_str_t s, size_t i, dg_str_t *id, dg_str_t *label) {
12301231
i = dg_skip_ws(s, i);
1232+
*id = dg_s(NULL, 0);
12311233
*label = dg_s(NULL, 0);
12321234
if (i + CBM_SZ_3 <= s.n && memcmp(s.p + i, "[*]", CBM_SZ_3) == 0) {
12331235
*id = dg_s(s.p + i, CBM_SZ_3);

‎tests/test_diagrams.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,36 @@ TEST(diagrams_plantuml) {
329329
PASS();
330330
}
331331

332+
/* #2818: a sequence message to or from the diagram edge (`A ->: x`, `A <-: x`,
333+
* `[-> A: x`) and a declaration with an unclosed bracket have no operand id;
334+
* they used to read an uninitialized one and crash the index worker. */
335+
TEST(diagrams_plantuml_edge_messages) {
336+
CBMFileResult *r = dg_file("@startuml\n"
337+
"participant Alice\n"
338+
"Alice ->: out\n"
339+
"Alice <-: in\n"
340+
"[-> Alice: from the edge\n"
341+
"Alice -> Bob : hello\n"
342+
"class [Unclosed\n"
343+
"@enduml\n",
344+
"docs/edge.puml");
345+
ASSERT_NOT_NULL(r);
346+
ASSERT_NOT_NULL(dg_def(r, "edge#diagram-1.Alice"));
347+
ASSERT_NOT_NULL(dg_def(r, "edge#diagram-1.Bob"));
348+
ASSERT_EQ(r->diagram_arrows.count, 1);
349+
ASSERT_EQ(dg_arrows(r, "#diagram-1.Alice", "#diagram-1.Bob", "message", "hello"), 1);
350+
int elements = 0;
351+
for (int i = 0; i < r->defs.count; i++) {
352+
const char *label = r->defs.items[i].label;
353+
if (label && strcmp(label, "DiagramElement") == 0) {
354+
elements++;
355+
}
356+
}
357+
ASSERT_EQ(elements, 2);
358+
cbm_free_result(r);
359+
PASS();
360+
}
361+
332362
TEST(diagrams_dot) {
333363
CBMFileResult *r =
334364
dg_file("digraph G {\n"
@@ -1101,6 +1131,7 @@ SUITE(diagrams) {
11011131
RUN_TEST(diagrams_mermaid_kinds);
11021132
RUN_TEST(diagrams_other_fences);
11031133
RUN_TEST(diagrams_plantuml);
1134+
RUN_TEST(diagrams_plantuml_edge_messages);
11041135
RUN_TEST(diagrams_dot);
11051136
RUN_TEST(diagrams_d2);
11061137
RUN_TEST(diagrams_drawio);

0 commit comments

Comments
 (0)