From nobody Fri Aug 29 17:03:52 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4cD4Nn0SLjz65rYr; Fri, 29 Aug 2025 17:03:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cD4Nm6Ypnz3F7J; Fri, 29 Aug 2025 17:03:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1756487032; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=SG7yyU2hTFnuvGpgWO9SNKNsuR+DITsGoeM2wg49zdU=; b=lPUZCrYC8Yp0PaVonFCDnDW85H3xsMhkbMVvWDvZJHSGna4jlGAZ0E+C14m8kcTJky9Ue9 3OV7EPpGMP+nMnaC1nxQmOi8Uz9hzA70FvoJ9+HTbwYulZ7/5mhKASJCw3BIvRJHnoaw29 sZ5q46bRz0ofwLwrEzNKPHq+h10JReXf3LBdzPYCe7EDDoXQhXvGyXZ6jbYpz6O2ADg1Gy NjgyibWzk8FloarkXoddZJwa+EuWW8IMUpv0a3a7HVf/67Apac+1NSr22MpiBS4AeFJRIk 5/if8Eft2Eq6j5G8M7tlQ1b/D1JDGo1vhgRx608QClAlMtDiC6ITyEI4jkQcQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1756487032; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=SG7yyU2hTFnuvGpgWO9SNKNsuR+DITsGoeM2wg49zdU=; b=MDyqbK3ren482GL3dski8z0Rad6JbcpqSiKCrPV34DXnli8JJBPPjkFWMy/cDYB8KUFc2K Ch4qJfcPPlegDq1V+28PzA62zIWLICxldKfsbvtJhMiE1iBtpktRaw96VX7sKdDZXf0CnK SE3dDlmdQ9unxy3RswmU8CNfYJnN/leigtSmHvH6vi94oi+B0VzlxMzt/QM/c9ZzCfCxff qiQpSbZjrGPN9IsItqkuyeoK+r2rf6EXYHK+NVbFvmkvVB2zLZI5d/to2doP0Lyo0WrZ0o l29NVuAjo5eMDiBXY5ipBVafTtMZG9s4O+fVHWzT3j1x6NevXp7SZV3N7Xt8uw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1756487032; a=rsa-sha256; cv=none; b=Zml0bTbWu7Vv+b7NrySdpC9pdR53UBJYbTljqBhqHoMB/+jOaWOYkKbJ4QKTUfWIuyGqlF 14Qwq4/+CX4cetsSB+HXDGOuWScP01eXYel+8RtqC6eXotzYogq8ogNr3hBKkC6WYJO+8E QHm+dLjMOtzJ+WS2ge3wNr2k23QP5egnmlCKOD5R+TeMiYzLM4Gwy6d+kmmI+yUpU4ZWsB NEEhtnWKQzbRt3ktolsTX+DdE1bahenmFdaFO4sLgeB26xUlmO/pOVK3NkrGq592NaKOhx NgWZdj1Y06cdU8IYN1oHj9zEVm/BjtXMehNTsBTA+G3DWon2GUtbTUU7JuRA9g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4cD4Nm63jxznxk; Fri, 29 Aug 2025 17:03:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 57TH3qKV006050; Fri, 29 Aug 2025 17:03:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 57TH3qei006047; Fri, 29 Aug 2025 17:03:52 GMT (envelope-from git) Date: Fri, 29 Aug 2025 17:03:52 GMT Message-Id: <202508291703.57TH3qei006047@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: b0e655bc5c64 - stable/14 - flua: clean up lposix argument checking List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: b0e655bc5c646dd588aef5f92345213316259946 Auto-Submitted: auto-generated The branch stable/14 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=b0e655bc5c646dd588aef5f92345213316259946 commit b0e655bc5c646dd588aef5f92345213316259946 Author: Isaac Freund AuthorDate: 2025-05-09 14:29:37 +0000 Commit: Ed Maste CommitDate: 2025-08-29 15:06:47 +0000 flua: clean up lposix argument checking The key insight here is that the luaL_check*() and luaL_opt*() functions will happily take indexes that are larger than the stack top and print a useful error message. This means that there is no need to check if too few arguments have been received prior to checking the types of individual arguments. This patch also replaces a couple reimplementations of luaL_opt*() functions with the luaL helpers. References: https://www.lua.org/manual/5.4/manual.html#4.1.2 Reviewed by: emaste, kevans Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D50273 (cherry picked from commit 5437b0ff6d557daf6c35e1307a5737139bc12f9a) --- libexec/flua/modules/lposix.c | 111 ++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 68 deletions(-) diff --git a/libexec/flua/modules/lposix.c b/libexec/flua/modules/lposix.c index 2e8e9bfc389e..47a1354aede9 100644 --- a/libexec/flua/modules/lposix.c +++ b/libexec/flua/modules/lposix.c @@ -21,18 +21,26 @@ #include "lauxlib.h" #include "lposix.h" +static void +enforce_max_args(lua_State *L, int max) +{ + int narg; + + narg = lua_gettop(L); + luaL_argcheck(L, narg <= max, max + 1, "too many arguments"); +} + /* * Minimal implementation of luaposix needed for internal FreeBSD bits. */ static int lua__exit(lua_State *L) { - int code, narg; - - narg = lua_gettop(L); - luaL_argcheck(L, narg == 1, 1, "_exit takes exactly one argument"); + int code; + enforce_max_args(L, 1); code = luaL_checkinteger(L, 1); + _exit(code); } @@ -40,10 +48,8 @@ static int lua_basename(lua_State *L) { char *inpath, *outpath; - int narg; - narg = lua_gettop(L); - luaL_argcheck(L, narg > 0, 1, "at least one argument required"); + enforce_max_args(L, 1); inpath = strdup(luaL_checkstring(L, 1)); if (inpath == NULL) { lua_pushnil(L); @@ -61,15 +67,13 @@ lua_basename(lua_State *L) static int lua_chmod(lua_State *L) { - int n; const char *path; mode_t mode; - n = lua_gettop(L); - luaL_argcheck(L, n == 2, n > 2 ? 3 : n, - "chmod takes exactly two arguments"); + enforce_max_args(L, 2); path = luaL_checkstring(L, 1); mode = (mode_t)luaL_checkinteger(L, 2); + if (chmod(path, mode) == -1) { lua_pushnil(L); lua_pushstring(L, strerror(errno)); @@ -83,15 +87,13 @@ lua_chmod(lua_State *L) static int lua_chown(lua_State *L) { - int n; const char *path; uid_t owner = (uid_t)-1; gid_t group = (gid_t)-1; int error; - n = lua_gettop(L); - luaL_argcheck(L, n > 1, n, - "chown takes at least two arguments"); + enforce_max_args(L, 3); + path = luaL_checkstring(L, 1); if (lua_isinteger(L, 2)) owner = (uid_t)lua_tointeger(L, 2); @@ -148,11 +150,9 @@ lua_chown(lua_State *L) static int lua_pclose(lua_State *L) { - int error, fd, n; + int error, fd; - n = lua_gettop(L); - luaL_argcheck(L, n == 1, 1, - "close takes exactly one argument (fd)"); + enforce_max_args(L, 1); fd = luaL_checkinteger(L, 1); if (fd < 0) { @@ -178,14 +178,13 @@ static int lua_fnmatch(lua_State *L) { const char *pattern, *string; - int flags, n; - - n = lua_gettop(L); - luaL_argcheck(L, n == 2 || n == 3, 4, "need 2 or 3 arguments"); + int flags; + enforce_max_args(L, 3); pattern = luaL_checkstring(L, 1); string = luaL_checkstring(L, 2); flags = luaL_optinteger(L, 3, 0); + lua_pushinteger(L, fnmatch(pattern, string, flags)); return (1); @@ -195,10 +194,9 @@ static int lua_uname(lua_State *L) { struct utsname name; - int error, n; + int error; - n = lua_gettop(L); - luaL_argcheck(L, n == 0, 1, "too many arguments"); + enforce_max_args(L, 0); error = uname(&name); if (error != 0) { @@ -228,11 +226,9 @@ static int lua_dirname(lua_State *L) { char *inpath, *outpath; - int narg; - narg = lua_gettop(L); - luaL_argcheck(L, narg > 0, 1, - "dirname takes at least one argument (path)"); + enforce_max_args(L, 1); + inpath = strdup(luaL_checkstring(L, 1)); if (inpath == NULL) { lua_pushnil(L); @@ -251,10 +247,8 @@ static int lua_fork(lua_State *L) { pid_t pid; - int narg; - narg = lua_gettop(L); - luaL_argcheck(L, narg == 0, 1, "too many arguments"); + enforce_max_args(L, 0); pid = fork(); if (pid < 0) { @@ -271,10 +265,8 @@ lua_fork(lua_State *L) static int lua_getpid(lua_State *L) { - int narg; + enforce_max_args(L, 0); - narg = lua_gettop(L); - luaL_argcheck(L, narg == 0, 1, "too many arguments"); lua_pushinteger(L, getpid()); return (1); } @@ -282,10 +274,9 @@ lua_getpid(lua_State *L) static int lua_pipe(lua_State *L) { - int error, fd[2], narg; + int error, fd[2]; - narg = lua_gettop(L); - luaL_argcheck(L, narg == 0, 1, "too many arguments"); + enforce_max_args(L, 0); error = pipe(fd); if (error != 0) { @@ -306,12 +297,9 @@ lua_read(lua_State *L) char *buf; ssize_t ret; size_t sz; - int error, fd, narg; - - narg = lua_gettop(L); - luaL_argcheck(L, narg == 2, 1, - "read takes exactly two arguments (fd, size)"); + int error, fd; + enforce_max_args(L, 2); fd = luaL_checkinteger(L, 1); sz = luaL_checkinteger(L, 2); @@ -352,10 +340,8 @@ lua_realpath(lua_State *L) { const char *inpath; char *outpath; - int narg; - narg = lua_gettop(L); - luaL_argcheck(L, narg > 0, 1, "at least one argument required"); + enforce_max_args(L, 1); inpath = luaL_checkstring(L, 1); outpath = realpath(inpath, NULL); @@ -376,17 +362,12 @@ lua_wait(lua_State *L) { pid_t pid; int options, status; - int narg; - narg = lua_gettop(L); - - pid = -1; - status = options = 0; - if (narg >= 1 && !lua_isnil(L, 1)) - pid = luaL_checkinteger(L, 1); - if (narg >= 2 && !lua_isnil(L, 2)) - options = luaL_checkinteger(L, 2); + enforce_max_args(L, 2); + pid = luaL_optinteger(L, 1, -1); + options = luaL_optinteger(L, 2, 0); + status = 0; pid = waitpid(pid, &status, options); if (pid < 0) { lua_pushnil(L); @@ -428,13 +409,9 @@ lua_write(lua_State *L) size_t bufsz, sz; ssize_t ret; off_t offset; - int error, fd, narg; + int error, fd; - narg = lua_gettop(L); - luaL_argcheck(L, narg >= 2, 1, - "write takes at least two arguments (fd, buf, sz, off)"); - luaL_argcheck(L, narg <= 4, 5, - "write takes no more than four arguments (fd, buf, sz, off)"); + enforce_max_args(L, 4); fd = luaL_checkinteger(L, 1); if (fd < 0) { @@ -444,13 +421,11 @@ lua_write(lua_State *L) buf = luaL_checkstring(L, 2); - bufsz = sz = lua_rawlen(L, 2); - if (narg >= 3 && !lua_isnil(L, 3)) - sz = luaL_checkinteger(L, 3); + bufsz = lua_rawlen(L, 2); + sz = luaL_optinteger(L, 3, bufsz); + + offset = luaL_optinteger(L, 4, 0); - offset = 0; - if (narg >= 4 && !lua_isnil(L, 4)) - offset = luaL_checkinteger(L, 4); if ((size_t)offset > bufsz || offset + sz > bufsz) { lua_pushnil(L);