PLC-lang/rusty

Validate array access in for-loops

Aperta

#1255 aperta il 3 lug 2024

 (0 commenti) (0 reazioni) (0 assegnatari)Rust (71 fork)auto 404
good first issuemedium-priorityvalidation

Metriche repository

Star
 (351 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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.

Guida contributor