edubart / nelua-lang

Minimal, efficient, statically-typed and meta-programmable systems programming language heavily inspired by Lua, which compiles to C and native code.
https://nelua.io
MIT License
2k stars 62 forks source link

segfault fault filestream:close() #65

Closed bokunodev closed 3 years ago

bokunodev commented 3 years ago

WARNING: Please, read this note carefully before submitting a new bug:

It is important to realise that this is NOT A SUPPORT FORUM, this is for reproducible BUGS ONLY.

If you need support, have questions, ideas to discuss you should use the Nelua Discussions forum or the Nelua Discord server.

Please, before submitting a new issue verify and check:


Bug description

i made a program to download something from internet example.com and write it to a file. but its always Segmentation fault (core dumped) when i call filestream:close()

Code example

Provide minimal reproduction code to test the bug. Please, format the code properly and try to keep it as simple as possible, just focusing on the experienced bug.

require "modules/curl/curl"
require "io"

print("open a file")
local s:string, i:integer
local file:filestream
file,s,i = io.open("example", "w+")
print(file,s,i)

local function write_func(ptr:cstring, size:csize, nmemb:csize, userdata:pointer):csize
    local b:boolean, s:string, i:integer
    b, s, i = file:write(ptr)
    print("write callback", b, s, i)
    return size*nmemb
end

local client:pointer = curl_easy_init()
curl_easy_setopt(client, CURLOPT_URL, "https://example.com")
curl_easy_setopt(client, CURLOPT_WRITEFUNCTION, write_func)
local response:CURLcode = curl_easy_perform(client)
print(file:flush())
print("closing tmpfile", file:close()) -- always segfault after printing text.
print("curl cleanup")
curl_easy_cleanup(client)

Expected behavior

no error

Environment

Provide relevant information about your environment:

bokunodev commented 3 years ago

its weird, i cant replicate it anymore.

edubart commented 3 years ago

Actually it's an error over a mistake. You made a mistake by trying to close the same file twice, but I tested the code here, and it crashed with the wrong error, I will fix soon, so when this is mistake happens the user can recover from it.