Skip to content

ffi.gc is called with the wrong paramter #363

@fengyd2006

Description

@fengyd2006

Recently I met a segment fault issue ,and finally found that when calling get_url_field in turbo httputils.lua, ffi.gc is called with the wrong paramter.
ffi.gc(htpurl, ffi.C.free)
htpurl is a temporary pointer, which goes out of scope and will eventually be collected.
It should be changed to ffi.gc(self.http_parser_url, ffi.C.free) and put it after:
self.http_parser_url = ffi.cast("struct http_parser_url *", htpurl)

function httputil.HTTPParser:parse_url(url)
    if type(url) ~= "string" then
        error("URL parameter is not a string")
    end
    local htpurl = ffi.C.malloc(ffi.sizeof("struct http_parser_url"))
    if htpurl == nil then
        error("Could not allocate memory")
    end
    ffi.gc(htpurl, ffi.C.free)
    self.http_parser_url = ffi.cast("struct http_parser_url *", htpurl)
    local rc = libturbo_parser.http_parser_parse_url(
        url,
        url:len(),
        0,
        self.http_parser_url)
    if rc ~= 0 then
       error("Could not parse URL")
    end
    if not self.url then
        self.url = url
    end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions