PLC-lang/rusty

Validate array access in for-loops

Open

#1,255 opened on Jul 3, 2024

 (0 comments) (0 reactions) (0 assignees)Rust (71 forks)auto 404
good first issuemedium-priorityvalidation

Repository metrics

Stars
 (351 stars)
PR merge metrics
 (PR metrics pending)

Description

Is your feature request related to a problem? Please describe. for-loops where the start- and step-values are constant-evaluable currently have no validation for array out-of-bounds accesses where the counter variable is used. For example

VAR
  arr : ARRAY[1..5] OF DINT;
END_VAR

FOR idx := 1 TO 10 DO
  (* ... *)
  arr[idx] := idx;
  (* ... *)
END_FOR 

Based on the end-value we are able to warn the user about a possible out-of-bounds access on arr. Possible because early returns or conditions making sure no out-of-bounds access happen could be in the for-loop.

Describe the solution you'd like Return a warning (not an error), indicating of possible out-of-bounds accesses.

Additional context Data-Flow (or some other kind) analysis would make this much easier, but for very easy cases it should be possible without it.

Contributor guide