Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F139560688
D50273.id155207.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
6 KB
Referenced Files
None
Subscribers
None
D50273.id155207.diff
View Options
diff --git a/libexec/flua/modules/lposix.c b/libexec/flua/modules/lposix.c
--- 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 @@
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 @@
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,14 +87,12 @@
static int
lua_chown(lua_State *L)
{
- int n;
const char *path;
uid_t owner = (uid_t) -1;
gid_t group = (gid_t) -1;
- 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);
@@ -139,11 +141,9 @@
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) {
@@ -168,11 +168,9 @@
static int
lua_dup2(lua_State *L)
{
- int error, oldd, newd, n;
+ int error, oldd, newd;
- n = lua_gettop(L);
- luaL_argcheck(L, n == 2, n > 2 ? 3 : n,
- "dup2 takes exactly two arguments");
+ enforce_max_args(L, 2);
oldd = luaL_checkinteger(L, 1);
if (oldd < 0) {
@@ -204,14 +202,11 @@
static int
lua_execp(lua_State *L)
{
- int n, argc;
+ int argc;
const char *file;
const char **argv;
- n = lua_gettop(L);
- luaL_argcheck(L, n == 2, n > 2 ? 3 : n,
- "execp takes exactly two arguments");
-
+ enforce_max_args(L, 2);
file = luaL_checkstring(L, 1);
luaL_checktype(L, 2, LUA_TTABLE);
@@ -249,14 +244,13 @@
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);
@@ -266,10 +260,9 @@
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) {
@@ -299,11 +292,9 @@
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);
@@ -322,10 +313,8 @@
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) {
@@ -342,10 +331,8 @@
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);
}
@@ -353,10 +340,9 @@
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) {
@@ -377,12 +363,9 @@
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);
@@ -423,10 +406,8 @@
{
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);
@@ -447,17 +428,12 @@
{
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);
@@ -499,13 +475,9 @@
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) {
@@ -515,13 +487,11 @@
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);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 14, 12:47 PM (12 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
26959287
Default Alt Text
D50273.id155207.diff (6 KB)
Attached To
Mode
D50273: flua: clean up lposix argument checking
Attached
Detach File
Event Timeline
Log In to Comment