<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Limiting Memory Access of an Array to Within its Bounds</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/75b60c96d5/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/75b60c96d5/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/75b60c96d5/</id><updated>2024-03-06T14:23:54.079000Z</updated><subtitle>Recent posts to Limiting Memory Access of an Array to Within its Bounds</subtitle><entry><title>Limiting Memory Access of an Array to Within its Bounds</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/75b60c96d5/?limit=25#2e4e" rel="alternate"/><published>2024-03-06T14:23:54.079000Z</published><updated>2024-03-06T14:23:54.079000Z</updated><author><name>john-robinson</name><uri>https://forge.codesys.com/u/john-robinson/</uri></author><id>https://forge.codesys.comc84aff09a4dbe9b1119e923a86c742742bd9e35f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Thanks Tim!&lt;br/&gt;
This was exactly the functionality we were looking for. We are already using implicit checks to check for divide by 0 errors, we will add implicit checking for array bounds.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Limiting Memory Access of an Array to Within its Bounds</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/75b60c96d5/?limit=25#394c" rel="alternate"/><published>2024-03-06T13:03:52.049000Z</published><updated>2024-03-06T13:03:52.049000Z</updated><author><name>TimvH</name><uri>https://forge.codesys.com/u/timvh/</uri></author><id>https://forge.codesys.com2b03faaa757f039ec6ce423c2a1f22acdf61747c</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Add the object "POU for Implicit Checks" to your application, then select "Bounds check".&lt;br/&gt;
This will create a function which is automatically called each time an array is write accessed. In the function you can handle "out of bounds" situations. By default it will be limited to its lower or upper bound.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Limiting Memory Access of an Array to Within its Bounds</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/75b60c96d5/?limit=25#5f5a" rel="alternate"/><published>2024-03-05T18:31:39.038000Z</published><updated>2024-03-05T18:31:39.038000Z</updated><author><name>john-robinson</name><uri>https://forge.codesys.com/u/john-robinson/</uri></author><id>https://forge.codesys.com384828533f39dc2b9b4e8349e114ebb2c485b6ca</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Recently we had an issue regarding some simple code to calculate a rolling average. The code indexes from zero to 199 to properly store the current input into a circular buffer which then allows us to calculate a rolling average:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;VAR&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;input_5s&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;REAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;outs_arr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;ARRAY&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;0..199&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;OF&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;REAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;USINT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;END_VAR&lt;/span&gt;
&lt;span class="n"&gt;___&lt;/span&gt;
&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;runs&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;every&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;five&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;calculating&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;rolling&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;average&lt;/span&gt;
&lt;span class="n"&gt;outs_arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input_5s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;OSCAT_BASIC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ARRAY_AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ADR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outs_arr&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SIZEOF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outs_arr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="k"&gt;IF&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SIZEOF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outs_arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;THEN&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;END_IF&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There is a simple bug in this code where the index will be set to 0 when it has surpassed the length of the array in bytes (800 in this case) rather than larger than the number of reals in the array (200). The solution here is simple, replacing &lt;code&gt;i &amp;gt;= SIZEOF(outs_arr)&lt;/code&gt; with &lt;code&gt;i &amp;gt;= SIZEOF(outs_arr)/SIZEOF(outs_arr[0])&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In this example when the index increased to 201 and the line &lt;code&gt;outs_arr[201] := input_5s&lt;/code&gt; was called, codesys arbitrarily wrote to the address in memory that is where outs_arr&lt;span&gt;[201]&lt;/span&gt; would be if the array was that long. I would like to find a way to wrap the codesys array inside of a wrapper class that checks if an input is within the bounds of an array before writing to that value. I know how I would implement that for a specific array, I could create a method or class that takes an input of an array of variable length, ie. &lt;code&gt;ARRAY[*] OF REAL&lt;/code&gt;, but I don't know how to make this for any data type.&lt;/p&gt;
&lt;p&gt;I am wondering if anyone has ever done anything similar to this, or has any better suggestions to ensure that none of the programmers on this application accidentally create code that can arbitrarily write to other locations in memory.&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>