Re: newbie: local buf = ffi.new("char[?]", "HELLO WORLD") fails PROBABLY SOLVED

  • From: Patrick Masotta <masottaus@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 21 Jun 2012 06:27:28 -0700 (PDT)

thanks for the answer, I see your point but if you are using a VLA different 
than a char[?] you shouldn't been using length=0 anyway...
The mod only add a correct interpretation for the case you provide a
null ended string as initial value.. without my mod if you set length=0
you will also crash...



--- On Thu, 6/21/12, Paulo Matias <syscoder@xxxxxxxxx> wrote:

> From: Paulo Matias <syscoder@xxxxxxxxx>
> Subject: Re: newbie: local buf = ffi.new("char[?]", "HELLO WORLD") fails 
> PROBABLY SOLVED
> To: luajit@xxxxxxxxxxxxx
> Date: Thursday, June 21, 2012, 5:56 AM
> On Thu, Jun 21, 2012 at 9:41 AM,
> Patrick Masotta <masottaus@xxxxxxxxx>
> wrote:
> > I just wanted to avoid the LUA string creation.
> > To me it does not make much sense creating a LUA string
> to define a C string...
> 
> The Lua string will be created anyway. Perhaps you are
> thinking about
> 'ffi.new' as if it were a special LuaJIT statement, with
> some special
> behavior embedded directly into the parser. No, it isn't any
> special
> statement. It's just an ordinary Lua function call, like
> 'ipairs' or
> 'math.sin'.
> 
> > ...
> >  if ((info & CTF_VLA)) {
> >    o++;
> >    sz = lj_ctype_vlsize(cts, ct,
> (CTSize)ffi_checkint(L, 2));
> > +   {//patmod
> > +   if(sz==0)
> > +     {void *sp = NULL;
> > +      sp = ffi_checkptr(L, 3, CTID_P_CVOID);
> > +      if (sp)
> > +       sz = strlen((char*)sp)+1;
> > +      //else
> > +      //log error condition
> > +      }
> > +    }//patmod
> >  }
> 
> Perhaps this would crash if you try to allocate a VLA with
> zero
> elements initialized to something which isn't a string. It
> would try
> to run strlen on the data and would potentially crash if it
> didn't
> contain any '\0' bytes.
> 
> I'm personally against any such a patch. Just stay with
> Duncan Cross
> suggestion (creating the 'cstring' function) if you need to
> modify the
> string or with Peter Cawley suggestion (casting to const
> char*) if you
> don't need to modify the string.
> 
>

Other related posts: