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

  • From: Patrick Masotta <masottaus@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sat, 30 Jun 2012 07:35:01 -0700 (PDT)

I just got a better patch for correctly taking 
local buf = ffi.new("char[?]",0, "HELLO WORLD")
where 0 means "please take the length from the init string"

file: lib_ffi.c
function : LJLIB_CF(ffi_new)    LJLIB_REC(.)

...
  if ((info & CTF_VLA)) {
    o++;
    sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));    
+       {//patmod
+       if(sz==0)
+               {
+               TValue *o = L->base + 3-1; //L->base + narg-1;
+               if (tvisstr(o)) 
+                       {
+                       GCstr *str = strV(o);
+                       sz = str->len+1;
+                       }
+               }
+       }//patmod
  }
...


This patch kicks in if size is declared as 0 and the init parameter is now a 
LUA string. (it does not depend on strlen())

local test = ffi.new("char[?]", 0, "12345\0xyz");
mr.gui.Log(0,"test size= %d", ffi.sizeof(test));
 
>Test size= 10

it has not been extensively tested yet but so far it works ok


Other related posts: