lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Jun 27, 2014 10:56 PM, "Austin Einter" <austin.einter@gmail.com> wrote:
>
>
> Hi All
> Thanks a lot. Using your recommended means (-DLUA_USE_APICHECK and checking return values of luaL_loadstring, lua_pcall ) , I found the errors.
>
> I am trying to resolve the errors.
>
> Lets say I have a simple function as below.
>
> const char *c_function = "function modify_content_length(msg, length) "
>                          "local offset = string.find(msg, \"\r\n\r\n\", 1, true) "
>                          "return 0 "
>                          "end";
>
> Looks I am getting error for line "local offset = string.find(msg, \"\r\n\r\n\", 1, true) "
>  
> If that line I change to "local offset = string.find(msg, \"someword\", 1, true) " then I do not get error (do not crash, execution completes).
>
> Ideally both statements are similar, in one case I have \r\n\r\n and other case I have someward.
>
> In 1st case I get error "COMPILE: [string "function modify_content_length(msg, length) l..."]:1: unfinished string near '"'"
>
> and other case it executes and not crashed.
>
>
> Kindly help me to understand why this error.
>
> BR
> Austin
>
>
>
> On Sat, Jun 28, 2014 at 2:21 AM, Tim Hill <drtimhill@gmail.com> wrote:
>>
>>
>> On Jun 27, 2014, at 1:46 PM, Coda Highland <chighland@gmail.com> wrote:
>>
>> > On Fri, Jun 27, 2014 at 11:18 AM, Tim Hill <drtimhill@gmail.com> wrote:
>> >> The Lua code is wrong in the first line, where you both attempt to create a
>> >> function by name (“modify_content_length” and also assign it to the variable
>> >> “c_function". You should do one or the other, but not both:
>> >>
>> >> c_function = function(msg, length) …
>> >> function modify_content_length(msg, length)
>> >
>> > Take a look again:
>> >
>> >> const char *c_function = "function modify_content_length(msg, length) "
>> >
>> > c_function is a C variable, not a Lua variable. That particular error
>> > isn't the problem here.
>> >
>> > I'm not sure what the actual problem IS, beyond the observations
>> > already made in this thread, which is why I hadn't chimed in yet.
>> >
>> > /s/ Adam
>> >
>>
>> lol serve me right for squinting at the code on my phone .. asterisk looks too much like double quotes :)
>>
>> —TIm
>>
>>
>

It's because \n and \r are escape sequences in C, as well. So, the compiler translates them to line feed/carriage return, and your Lua code ends up looking like:

local offset = string.find(msg, "

", 1, true)

I.e. the \r and \n are expanded at compile time, producing invalid code.

To fix this, you'd have to double the backslashes, as in: \\r\\n\\r\\n. The C compiler will then expand \\ to \, so Lua will see a literal "\n" rather than a line break.

However, I highly recommend that you instead move the Lua code to a file of its own, and load it at runtime using luaL_loadfile. As you've seen, trying to embed the Lua code within the C source leads to ugly code and subtle bugs.