Skip to content

Commit 76dda7e

Browse files
authored
Merge pull request DeusData#2827 from nefayran/fix/2818-puml-edge-operand
fix(diagram): set an empty PlantUML operand id when there is none
2 parents 30f1fbf + d514868 commit 76dda7e

2 files changed

Lines changed: 40 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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,42 @@ 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. The
335+
* declaration sits in its own diagram: a `class` line makes a diagram a class
336+
* diagram, where `->` is an arrow rather than a sequence message. */
337+
TEST(diagrams_plantuml_edge_messages) {
338+
CBMFileResult *r = dg_file("@startuml\n"
339+
"participant Alice\n"
340+
"Alice ->: out\n"
341+
"Alice <-: in\n"
342+
"[-> Alice: from the edge\n"
343+
"Alice -> Bob : hello\n"
344+
"@enduml\n"
345+
"@startuml\n"
346+
"class [Unclosed\n"
347+
"@enduml\n",
348+
"docs/edge.puml");
349+
ASSERT_NOT_NULL(r);
350+
ASSERT_STR_EQ(dg_prop(dg_def(r, "edge#diagram-1"), "diagram_kind"), "sequence");
351+
ASSERT_STR_EQ(dg_prop(dg_def(r, "edge#diagram-2"), "diagram_kind"), "class");
352+
ASSERT_NOT_NULL(dg_def(r, "edge#diagram-1.Alice"));
353+
ASSERT_NOT_NULL(dg_def(r, "edge#diagram-1.Bob"));
354+
ASSERT_EQ(r->diagram_arrows.count, 1);
355+
ASSERT_EQ(dg_arrows(r, "#diagram-1.Alice", "#diagram-1.Bob", "message", "hello"), 1);
356+
int elements = 0;
357+
for (int i = 0; i < r->defs.count; i++) {
358+
const char *label = r->defs.items[i].label;
359+
if (label && strcmp(label, "DiagramElement") == 0) {
360+
elements++;
361+
}
362+
}
363+
ASSERT_EQ(elements, 2);
364+
cbm_free_result(r);
365+
PASS();
366+
}
367+
332368
TEST(diagrams_dot) {
333369
CBMFileResult *r =
334370
dg_file("digraph G {\n"
@@ -1101,6 +1137,7 @@ SUITE(diagrams) {
11011137
RUN_TEST(diagrams_mermaid_kinds);
11021138
RUN_TEST(diagrams_other_fences);
11031139
RUN_TEST(diagrams_plantuml);
1140+
RUN_TEST(diagrams_plantuml_edge_messages);
11041141
RUN_TEST(diagrams_dot);
11051142
RUN_TEST(diagrams_d2);
11061143
RUN_TEST(diagrams_drawio);

0 commit comments

Comments
 (0)