[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: luaL_loadbuffer API cannot deal with shebang
- From: Francisco Olarte <folarte@...>
- Date: Thu, 23 May 2019 11:25:57 +0200
Baozeng:
On Thu, May 23, 2019 at 6:46 AM Baozeng <sploving1@gmail.com> wrote:
> Could we add some check in luaL_loadbuffer like the following?
> size_t ignore_sz = 3;
> if (size >= 2 && buff[0] == '#' && buff[1] == '!') {
> while(ignore_sz <= size && buff[ignore_sz-1] != '\n') {
> ignore_sz ++;
> }
> if(ignore_sz >= size) return -1;
> else {
> size = size - ignore_sz;
> buff = buff + ignore_sz;
> }
> }
I would not do that, it could break some things, and doing the same
thing as load_file, which skips a potential BOM and a 1st line
STARTING WITH A SINGLE '#' ( not with a shebang, it skips every "shell
like comment" ) is pretty easy with a wrapper:
/** Could be better named loadbufferx_like_loadfile_does ;-> */
int luaL_loadbufferx_skip (lua_State *L,
const char *buff,
size_t sz,
const char *name,
const char *mode)
{
const char BOM[]={0xEF,0xBB,0xBF};
if (sz>=sizeof(BOM) && memcmp(buff, p, sizeof(BOM))==0) {
buf+=sizeof(BOM);
sz+=sizeof(BOM);
}
if (sz>0 && *buff=='#') {
do {
--sz;
++buf;
} while(sz>0 && *buff!='\n');
if (sz>0) { /* exit due to newline. */
--sz;
++buf;
}
}
/* let base function decide what to do with empty buffer if sz==0. */
return luaL_loadbufferx(L, buff,sz, name,mode);
}
( UNTESTED, JUST A DRAFT ).
Francisco Olarte.