I'm not sure what I'm seeing, looks like a buffer overrun... but I don't get why

Node-red version: 2.2.2
node -v
v14.19.1
npm -v
6.14.16

I think I'm running into a buffer overrun. But maybe I'm just not seeing my error.

Needed to use node-red to process some data out of GnuRadio, the data comes out a tcp connection port 52002. Although the output comes in the form of an array of hex values. It seems when I iterate through that array, either it's iterating beyond the array, or the string I concatenate together gets overrun when printing....

Or maybe I'm just not seeing the error. First I will show my debug output, followed by a copy of the nodes doing this.

3/27/2022, 11:48:52 AMnode: 223d4eaa19d12748
function : (warn)
582
3/27/2022, 11:48:52 AMnode: 22de1b2b2ce37f16
test : msg : Object
object
topic: "test"
payload: buffer[582]
_session: object
_msgid: "3fa7693e12b5bc9c"
3/27/2022, 11:48:53 AMnode: c2e25aad1db43efc
msg.payload : string[28495]
"011001010100010110110111011010101011101100110100100101101001110101010100111011101101110101010100110101010111010110010101001000100110101001010101010100101010110101001010110101010101010110110111110101111001110101000111010101011010111001001111001010101001101001101011010100101011010110001010011010100101010101000101101011010000010110101101001010101010100101101001011001010110110101100110010111010110100110010101000111011101001010101010010001101010101101011010101101010111001010101010010110101001010101010100101010110101010100011010101011011010001111001101100101010110100101010101110110function readBigUInt64LE(offset = 0) {↵  validateNumber(offset, 'offset');↵  const first = this[offset];↵  const last = this[offset + 7];↵  if (first === undefined || last === undefined)↵    boundsError(offset, this.length - 8);↵↵  const lo = first +↵    this[++offset] * 2 ** 8 +↵    this[++offset] * 2 ** 16 +↵    this[++offset] * 2 ** 24;↵↵  const hi = this[++offset] +↵    this[++offset] * 2 ** 8 +↵    this[++offs..."

Now here is the set of nodes:

[
    {
        "id": "2e09b7ad84698bc1",
        "type": "tcp in",
        "z": "84ffd9bc4c469b5a",
        "name": "",
        "server": "client",
        "host": "localhost",
        "port": "52002",
        "datamode": "stream",
        "datatype": "buffer",
        "newline": "",
        "topic": "test",
        "base64": false,
        "tls": "",
        "x": 350,
        "y": 380,
        "wires": [
            [
                "223d4eaa19d12748",
                "22de1b2b2ce37f16"
            ]
        ]
    },
    {
        "id": "223d4eaa19d12748",
        "type": "function",
        "z": "84ffd9bc4c469b5a",
        "name": "",
        "func": "var str =\"\";\nnode.warn( msg.payload.length )\nfor (let x in msg.payload ) {\n   str += msg.payload[x].toString(); \n}\nreturn {payload: str};",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 620,
        "y": 380,
        "wires": [
            [
                "c2e25aad1db43efc"
            ]
        ]
    },
    {
        "id": "c2e25aad1db43efc",
        "type": "debug",
        "z": "84ffd9bc4c469b5a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 830,
        "y": 380,
        "wires": []
    },
    {
        "id": "22de1b2b2ce37f16",
        "type": "debug",
        "z": "84ffd9bc4c469b5a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 630,
        "y": 460,
        "wires": []
    }
]

Is this a bug in node-red?

So, changed the for loop to be a for ( x=0; x<msg.payload.length; x++ )

and all is fine. So what do I not understand about for (x in array ) ?

for in Will return all property names of the buffer, which is more than you think
try this to see

for (const x in msg.payload ) {
   node.warn(x)
}
return msg;

where as for of will return the values of the buffer
e.g.

for (const x of msg.payload ) {
   str += x.toString();
}
return {payload: str};

The modern way to iterate an array is to use Array.forEach(). JavaScript Array forEach() Method

1 Like