From 45b6947f49e47a5aff2e2a93a1cca2bc913d869c Mon Sep 17 00:00:00 2001 From: dacctal Date: Sat, 18 Jul 2026 08:40:27 +0000 Subject: fix segfault in get_pkgsrc() with invalid str_fmt args --- .gitignore | 1 + src/build.c | 12 ++++++++---- src/lua_globs.c | 5 ++--- src/pkg_create.c | 8 ++++---- src/pkg_install.c | 8 ++------ src/pkg_remove.c | 5 +---- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 5ff5b5c..d05bf0f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ patches/ demo demo* core* +*.scm diff --git a/src/build.c b/src/build.c index 72b1e40..1974846 100644 --- a/src/build.c +++ b/src/build.c @@ -69,6 +69,7 @@ bool bldit(package_t *pkg) { lua_getglobal(B, "dependencies"); if (!lua_istable(B, -1)) { bldit_isnt_type("dependencies", "table"); + lua_pop(B, 1); } else { lua_pushnil(B); install_dependencies(B); @@ -77,14 +78,14 @@ bool bldit(package_t *pkg) { lua_getglobal(B, "targets"); if (!lua_istable(B, -1)) { bldit_isnt_type("targets", "table"); - lua_pop(B, 3); + lua_pop(B, 1); lua_close(B); + B = NULL; + bldit_loaded = false; return false; } bool target_success = target_build(B, "bldit.lua", &pkg->target); lua_pop(B, 1); - // lua_pop(B, 2); - // lua_close(B); return target_success; } @@ -155,7 +156,10 @@ bool build_loop(package_t *pkg) { bool build(package_t *pkg) { char cwd[MAX_PATH_LEN]; - getcwd(cwd, MAX_PATH_LEN); + if (getcwd(cwd, sizeof(cwd)) == NULL) { + log_error("getcwd failed"); + return false; + } if (!str_equal_cstr(&pkg->src, cwd) && !pkg->is_local) chdir(pkg->src.data); diff --git a/src/lua_globs.c b/src/lua_globs.c index 00049b4..3f8ac2a 100644 --- a/src/lua_globs.c +++ b/src/lua_globs.c @@ -72,7 +72,6 @@ void init_lua_state(void) { config_loaded = true; } -// FIXME: ensure this is called ONCE for clarity void init_bldit_state(void) { if (B != NULL) return; @@ -85,7 +84,6 @@ void init_bldit_state(void) { } lua_pushfstring(B, "%s", inst_dirs.prefix.data); lua_setglobal(B, "prefix"); - lua_pop(B, 1); bldit_loaded = true; } @@ -126,11 +124,12 @@ void bldit_isnt_type(char *variable, char *type) { bool lua_try_function(lua_State *L, char *lua_file, char *fname) { lua_getfield(L, -1, fname); if (!lua_isfunction(L, -1)) { - if (strcmp(lua_file, "bldit.lua")) + if (!strcmp(lua_file, "bldit.lua")) bldit_isnt_type(fname, "function"); else lua_isnt_type(fname, "function"); lua_pop(L, 1); + return false; } else if (lua_pcall(L, 0, 1, 0) != LUA_OK) { if (flags.verbose) log_warn("%s: '%s' function borked: %s", lua_file, fname, diff --git a/src/pkg_create.c b/src/pkg_create.c index de85ae8..5afd1b0 100644 --- a/src/pkg_create.c +++ b/src/pkg_create.c @@ -42,7 +42,7 @@ static str get_destdir(str *cwd, str *arg) { static str get_pkgsrc(package_t pkg) { if (pkg.is_local == 1) - return str_format("%.*s/%.*s", str_fmt(&inst_dirs.src), pkg.name); + return str_format("%.*s/%.*s", str_fmt(&inst_dirs.src), str_fmt(&pkg.name)); else return str_format("%.*s/%.*s/%.*s", str_fmt(&inst_dirs.src), str_fmt(&pkg.name), str_fmt(&pkg.version)); @@ -62,7 +62,7 @@ static void assign_pkg_version(package_t *pkg, str *new_arg_str) { if (str_find_char(&pkg->version, ',')) pkg->version.len--; str_free(&tmp_arg); pkg->name.len -= pkg->version.len + 1; - } else pkg->version = mstr("HEAD"); + } else str_copy_cstr_into(&pkg->version, "HEAD");; } static void assign_pkg_target(package_t *pkg, str *new_arg_str) { @@ -80,8 +80,8 @@ static void assign_pkg_target(package_t *pkg, str *new_arg_str) { str_free(&tmp_arg); pkg->name.len -= pkg->target.len + 1; } else if (!flags.verbose) { - pkg->target = mstr("quiet"); - } else pkg->target = mstr("default"); + str_copy_cstr_into(&pkg->target, "quiet"); + } else str_copy_cstr_into(&pkg->target, "quiet"); } package_t pkg_create(str *arg) { diff --git a/src/pkg_install.c b/src/pkg_install.c index 1f152ca..463bf92 100644 --- a/src/pkg_install.c +++ b/src/pkg_install.c @@ -62,12 +62,10 @@ bool target_install(lua_State *L, char *lua_file, str *target) { lua_pop(L, 1); return false; } - // lua_try_function(L, lua_file, "pre_install"); if (!lua_try_function(L, lua_file, "install")) { lua_pop(L, 1); return false; } - // lua_try_function(L, lua_file, "post_install"); lua_pop(L, 1); return true; } @@ -105,14 +103,12 @@ bool bldit_install(package_t *pkg) { lua_pushfstring(B, "%s", inst_dirs.prefix.data); lua_setglobal(B, "prefix"); lua_getglobal(B, "targets"); - if (!lua_istable(L, -1)) { - // FIXME: change da message + if (!lua_istable(B, -1)) { log_error("targets is not a table in bldit.lua!"); return false; } bool target_success = target_install(B, "bldit.lua", &pkg->target); - lua_pop(B, 2); - lua_close(B); + lua_pop(B, 1); return target_success; } diff --git a/src/pkg_remove.c b/src/pkg_remove.c index 3a0d0b2..20105c2 100644 --- a/src/pkg_remove.c +++ b/src/pkg_remove.c @@ -76,12 +76,10 @@ bool bldit_uninstall(package_t *pkg) { } lua_pushfstring(B, "%s", inst_dirs.prefix.data); lua_setglobal(B, "prefix"); - lua_pop(B, 1); if (!lua_try_table(B, "bldit.lua", "targets")) return false; bool target_success = target_uninstall(B, "bldit.lua", &pkg->target); - lua_pop(B, 2); - lua_close(B); + lua_pop(B, 1); return target_success; } @@ -176,7 +174,6 @@ void pkg_remove(package_t *pkg) { return; } - // TODO: refactor this nftw(pkg->src.data, remove_installed, 64, FTW_PHYS); const char *last_slash = strrchr(pkg->src.data, '/'); char target[MAX_PATH_LEN]; -- cgit v1.2.3