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

**URL:** <https://discourse.nodered.org/t/im-not-sure-what-im-seeing-looks-like-a-buffer-overrun-but-i-dont-get-why/60415>\
**Category:** General\
**Created:** [27 March 2022 18:06 UTC](https://discourse.nodered.org/t/im-not-sure-what-im-seeing-looks-like-a-buffer-overrun-but-i-dont-get-why/60415 "2022-03-27T18:06:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cthierman](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cthierman/32/57627_2.png) [@cthierman](https://discourse.nodered.org/u/cthierman)\
**Post date:** [27 March 2022 18:06 UTC](https://discourse.nodered.org/t/im-not-sure-what-im-seeing-looks-like-a-buffer-overrun-but-i-dont-get-why/60415/1 "2022-03-27T18:06:52Z")

</div>

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.

```auto
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:

```auto
[
    {
        "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?

---

<div class="post-metadata">

**Author:** ![cthierman](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cthierman/32/57627_2.png) [@cthierman](https://discourse.nodered.org/u/cthierman)\
**Post date:** [27 March 2022 18:13 UTC](https://discourse.nodered.org/t/im-not-sure-what-im-seeing-looks-like-a-buffer-overrun-but-i-dont-get-why/60415/2 "2022-03-27T18:13:06Z")

</div>

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 ) ?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 March 2022 20:00 UTC](https://discourse.nodered.org/t/im-not-sure-what-im-seeing-looks-like-a-buffer-overrun-but-i-dont-get-why/60415/3 "2022-03-27T20:00:57Z")

</div>

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

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 March 2022 20:56 UTC](https://discourse.nodered.org/t/im-not-sure-what-im-seeing-looks-like-a-buffer-overrun-but-i-dont-get-why/60415/4 "2022-03-27T20:56:18Z")

</div>

The modern way to iterate an array is to use `Array.forEach()`. [JavaScript Array forEach() Method](https://www.w3schools.com/jsref/jsref_foreach.asp)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 May 2022 20:57 UTC](https://discourse.nodered.org/t/im-not-sure-what-im-seeing-looks-like-a-buffer-overrun-but-i-dont-get-why/60415/5 "2022-05-26T20:57:12Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
