> On Wed, Jul 28, 2021 at 9:01 PM Martin Tournoij <[email protected]>
> wrote:
> 
> > Hm, the Lua manual
> > <https://www.lua.org/manual/5.4/manual.html#luaL_Buffer> doesn't mention
> > this at all; luaL_buffinit() does use LUAL_BUFFERSIZE initially, but
> > luaL_addlstring() calls prepbuffsize() which grows it as-needed.
> >
> > I mean, if it works then it works so it's fine, but I think it was somehow
> > using the API wrong, unless I completely misunderstood things.
> >
> >
> > The string returned by lua_tolstring() is on the Lua stack
> and using the Lua buffer also modifies the stack. I think using the
> Lua buffer while holding a pointer to the value returned by
> lua_tolstring() leads to this problem.
> 
> The Lua reference manual has the following:
> 
> ==============================================
> Because Lua has garbage collection, there is no guarantee that the
> pointer returned by lua_tolstring will be valid after the corresponding
> Lua value is removed from the stack.
> 
> During its normal operation, a string buffer uses a variable number of
> stack slots. So, while using a buffer, you cannot assume that you know
> where the top of the stack is. You can use the stack between successive
> calls to buffer operations as long as that use is balanced; that is,
> when you call a buffer operation, the stack is at the same level it was
> immediately after the previous buffer operation.
> ==============================================
> 
> I tried using vim_strsave() to create a copy of the string returned
> by lua_tolstring(). This fixes the issue. But this string needs to be
> allocated and freed once per loop iteration. It is more efficient
> to use the grow array to store the string.

So, another way would have been to put the pop outside of the loop,
clean up afterwards.

I prefer using the growarray anyway, having to carefully read Lua
documentation to understand how this works has its disadvantages, and
apparently leads to mistakes.  Well, using a growarray also has this
catch, that a pointer into the data becomes invalid when the data is
reallocated.

-- 
The greatest lies of all time:
  (1) The check is in the mail.
  (2) We have a really challenging assignment for you.
  (3) I love you.
  (4) All bugs have been fixed.
  (5) This won't hurt a bit.
  (6) Honey, I just need to debug this program and be home in 5 minutes.
  (7) I have just sent you an e-mail about that.
  (8) Of course I'll respect you in the morning.
  (9) I'm from the government, and I'm here to help you.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202107300954.16U9s6cs2368638%40masaka.moolenaar.net.

Raspunde prin e-mail lui