Duet3D / RepRapFirmware

OO C++ RepRap Firmware
GNU General Public License v3.0
944 stars 535 forks source link

[Bug]: Function exists() does not work correctly with array elements. #958

Closed benagricola closed 9 months ago

benagricola commented 9 months ago

Duet Forum Discussion Thread

https://forum.duet3d.com/topic/34967/function-exists-does-not-work-correctly-with-array-elements/2?_=1707983978816

Which Duet products are you using?

Firmware Version

3.5.0-rc.3+101 (STM32)

Duet Web Control Version

3.5.0-rc.3

Are you using a Single Board Computer (RaspberryPi) with your Duet?

Please upload the results of sending M122 in the gcode console.

No response

Please upload the content of your config.g file.

No response

Please upload the content of any other releveant macro files.

; Example on a standard vector
var testVector = { "A","B","C","D" }

; Expected: true, Actual: "C"
echo { exists(var.testVector[2]) }

Details specific to your printer.

N/A

Links to additional info.

No response

What happened?

Expected result Meta gcode function exists() could be called on an array index and identify whether the index exists in the array.

Observed result Meta gcode function exists() simply returns the value of the item at the array index, if valid, or throws an index out of bounds error if not.

Steps to reproduce

; Example on a standard vector
var testVector = { "A","B","C","D" }

; Expected: true, Actual: "C"
echo { exists(var.testVector[2]) }

; On blank tool table, which is only extended up to the highest tool defined
M563 P5 S"Test" R0
echo { exists(tools[5]) } ; Returns tool 5 object
echo { exists(tools[49]) } ; Throws array index out of bounds error if tool 49 is not defined
dc42 commented 9 months ago

I confirmed and fixed in the 3.5-dev source code the bug with exists and indexing into testVector. Thanks for reporting this. I was unable to reproduce the bug with exists and indexing into the tools array. Log of test (read in reverse order):

15/02/2024, 09:57:08    echo {exists(tools[5])}
true
15/02/2024, 09:57:03    echo {exists(tools[4])}
false
15/02/2024, 09:55:45    echo exists(tools[4])
false
15/02/2024, 09:55:41    echo exists(tools[5])
true
15/02/2024, 09:55:33    m563 p5 s"test"
15/02/2024, 09:55:26    m563 p5 s"test" r0
Error: M563: unconfigured spindle
15/02/2024, 09:54:26    echo exists(tools[0])
true
15/02/2024, 09:54:00    echo exists(tools[49])
false
15/02/2024, 09:53:44    echo tools[49]
Error:  line 10 column 6: meta command: array index out of bounds
15/02/2024, 09:53:37    echo tools[0]
{object}