From c9de363126bb34112b76f85268ba1c7d9593135c Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Thu, 8 Jul 2021 10:13:40 +0200 Subject: made stl parsing more robus by checking parent for each layer, added installation of normal checker requirements for linting check --- src/stlfile.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'src/stlfile.c') diff --git a/src/stlfile.c b/src/stlfile.c index c55a2a0..1a963f1 100644 --- a/src/stlfile.c +++ b/src/stlfile.c @@ -61,6 +61,13 @@ stack_ind(struct stack *stack, int search) return -1; } +int +stack_top_eq(struct stack *stack, int cmp) +{ + if (!stack->count) return 0; + return stack->data[stack->count-1] == cmp; +} + int isws(char c) { @@ -131,11 +138,11 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) while ((kw = consume_keyword(&bp))) { switch (kw) { case KW_SOLID_BEGIN: - stack_push(&states, STATE_SOLID); - if (stack_ind(&states, KW_SOLID_BEGIN) != -1) { - FMT_ERR("Multiple nested solids!\n"); + if (states.count) { + FMT_ERR("Improper nesting, solid not top-level in STL\n"); goto fail; } + stack_push(&states, STATE_SOLID); tmp = bp; if (!consume_keyword(&bp) && (arg = consume_arg(&bp, &end))) { @@ -162,11 +169,11 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) } break; case KW_LOOP_BEGIN: - stack_push(&states, STATE_LOOP); - if (stack_ind(&states, KW_LOOP_BEGIN) != -1) { - FMT_ERR("Multiple nested loops!\n"); + if (!stack_top_eq(&states, STATE_FACET)) { + FMT_ERR("Loop parent is not a facet!\n"); goto fail; } + stack_push(&states, STATE_LOOP); break; case KW_LOOP_END: if ((kw = stack_pop(&states)) != STATE_LOOP) { @@ -177,11 +184,11 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) info->loopcount++; break; case KW_FACET_BEGIN: - stack_push(&states, STATE_FACET); - if (stack_ind(&states, KW_LOOP_BEGIN) != -1) { - FMT_ERR("Multiple nested facets!\n"); + if (!stack_top_eq(&states, STATE_SOLID)) { + FMT_ERR("Facet parent is not a solid!\n"); goto fail; } + stack_push(&states, STATE_FACET); for (i = 0; i < 3; i++) { if (!(arg = consume_arg(&bp, &end))) { FMT_ERR("Facet with < 3 args!\n"); @@ -202,6 +209,10 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) } break; case KW_VERTEX: + if (!stack_top_eq(&states, STATE_LOOP)) { + FMT_ERR("Vertex parent is not a loop!\n"); + goto fail; + } for (i = 0; i < 3; i++) { if (!(arg = consume_arg(&bp, &end))) { FMT_ERR("Vertex with < 3 args\n"); -- cgit v1.2.3-71-gd317