Fix tcc.load(): home_path variable, pcall handle, metatype idempotency - #3
Open
alitrack wants to merge 1 commit into
Open
Fix tcc.load(): home_path variable, pcall handle, metatype idempotency#3alitrack wants to merge 1 commit into
alitrack wants to merge 1 commit into
Conversation
Three bugs in tcc.load() made it unusable with a home_path and made
repeated calls crash:
1. tcc.home_path was assigned the undefined variable `tccdir` instead of
the `home_path` parameter, silently discarding the provided path.
2. clib = pcall(ffi.load(...)) stored the *boolean* success flag into
tcc.clib instead of the library handle, so tcc.clib.tcc_new() failed
with 'cannot convert bool to struct TCCState *'.
3. ffi.metatype("TCCState", ...) was called unconditionally on every
load; the second call raises 'cannot change a protected metatable'.
Guarded with pcall so tcc.load() is idempotent.
Also fixed tcc.new(): the body referenced `tcc.tccdir` (undefined) and
the parameter `addpaths` (typo, missing underscore), so the home path
setup branch never ran.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
tcc.load()had three bugs that made it fail with a customhome_pathand crash on repeated calls:tcc.home_path = tccdir(undefined variable) — the providedhome_pathwas silently discarded, sotcc.clibfell back toffi.load("tcc")and the home-path branch never worked.clib = pcall(ffi.load(...))—pcallreturns(ok, result); the booleanokwas stored intcc.clibinstead of the library handle. Any subsequenttcc.clib.tcc_new()then failed withcannot convert 'bool' to 'struct TCCState *'.ffi.metatype("TCCState", tcc.State)— LuaJIT metatypes become protected once set; the secondtcc.load()call raisedcannot change a protected metatable. Guarded withpcallsotcc.load()is now idempotent.Also fixed in
tcc.new():tcc.tccdir(undefined) and the parameteraddpaths(typo — the signature isadd_paths), so the home-path setup branch never ran even whenhome_pathwas set correctly.Verification
Tested under LuaJIT 2.1 (inside a DuckDB extension host) with a real
libtcc.sobuild:tcc.load()with the samehome_path— no crash (was: crash on 2nd call)tcc.new()→set_output_type(MEMORY)→compile_string()→relocate()→get_symbol()→ call — all OK