Forums » Bugs

Lua - pcall() and xpcall() can't trap errors in unspickle()

Nov 10, 2023 draugath link
I recently discovered that `pcall()` and `xpcall()` can't trap and recover from errors caused by `unspickle()`.

If the string passed to `unspickle()` meets certain criteria, it errors out with a message similar to the following, with the string at fault following "constants:"

    invalid spickle string. [string "return {{..."]:4864514: main function has more than 65536 constants:

You can use the following code to create a string that will cause `unspickle()` to error.

    unspickle(string.rep('{},', 100000))

Interestingly, I've had strings that are much longer than this example work just fine. The main issue is that regardless of when it errors, `pcall()` and `xpcall()` fail to trap the error.
Dec 07, 2023 raybondo link
An optional second argument has been added to unspickle() that is a boolean to tell it to throw errors so they can be caught by x/pcall.

So: unspickle(string,true) will pass any errors to x/pcall.