aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2020-06-07 02:25:56 +0200
committerKim Alvefur <zash@zash.se>2020-06-07 02:25:56 +0200
commit625ec0a93f194fa152b0cf0b94f60afdee224df9 (patch)
treef547a1c5e6015d76b345f21ea6707f48bc716b26
parentb50db460865f3f0a1b1dbad90ee3bc5fbcec3a8f (diff)
downloadprosody-625ec0a93f194fa152b0cf0b94f60afdee224df9.tar.gz
prosody-625ec0a93f194fa152b0cf0b94f60afdee224df9.zip
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Actually just an alias of pushnil, but it does make it more obvious where the failure conditions are, which is good for readability.
-rw-r--r--util-src/encodings.c41
-rw-r--r--util-src/net.c13
-rw-r--r--util-src/poll.c25
-rw-r--r--util-src/pposix.c25
-rw-r--r--util-src/ringbuffer.c8
-rw-r--r--util-src/windows.c7
6 files changed, 70 insertions, 49 deletions
diff --git a/util-src/encodings.c b/util-src/encodings.c
index 996a6d4c..72264da8 100644
--- a/util-src/encodings.c
+++ b/util-src/encodings.c
@@ -24,6 +24,9 @@
#if (LUA_VERSION_NUM == 501)
#define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
#endif
+#if (LUA_VERSION_NUM < 504)
+#define luaL_pushfail lua_pushnil
+#endif
/***************** BASE64 *****************/
@@ -247,7 +250,7 @@ static int Lutf8_length(lua_State *L) {
size_t len;
if(!check_utf8(L, 1, &len)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushliteral(L, "invalid utf8");
return 2;
}
@@ -286,7 +289,7 @@ static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
input = luaL_checklstring(L, 1, &input_len);
if(input_len >= 1024) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
}
@@ -301,14 +304,14 @@ static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
if(U_FAILURE(err)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
}
prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, flags, NULL, &err);
if(U_FAILURE(err)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
} else {
u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
@@ -316,7 +319,7 @@ static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
if(U_SUCCESS(err) && output_len < 1024) {
lua_pushlstring(L, output, output_len);
} else {
- lua_pushnil(L);
+ luaL_pushfail(L);
}
return 1;
@@ -414,7 +417,7 @@ static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) {
}
if(s == NULL || len >= 1024 || len != strlen(s)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1; /* TODO return error message */
}
@@ -425,7 +428,7 @@ static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) {
lua_pushstring(L, string);
return 1;
} else {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1; /* TODO return error message */
}
}
@@ -464,7 +467,7 @@ static int Lidna_to_ascii(lua_State *L) { /** idna.to_ascii(s) */
u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
if(U_FAILURE(err)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
}
@@ -472,7 +475,7 @@ static int Lidna_to_ascii(lua_State *L) { /** idna.to_ascii(s) */
dest_len = uidna_nameToASCII(icu_idna2008, ustr, ulen, dest, 256, &info, &err);
if(U_FAILURE(err) || info.errors) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
} else {
u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
@@ -480,7 +483,7 @@ static int Lidna_to_ascii(lua_State *L) { /** idna.to_ascii(s) */
if(U_SUCCESS(err) && output_len < 1024) {
lua_pushlstring(L, output, output_len);
} else {
- lua_pushnil(L);
+ luaL_pushfail(L);
}
return 1;
@@ -499,7 +502,7 @@ static int Lidna_to_unicode(lua_State *L) { /** idna.to_unicode(s) */
u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
if(U_FAILURE(err)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
}
@@ -507,7 +510,7 @@ static int Lidna_to_unicode(lua_State *L) { /** idna.to_unicode(s) */
dest_len = uidna_nameToUnicode(icu_idna2008, ustr, ulen, dest, 1024, &info, &err);
if(U_FAILURE(err) || info.errors) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
} else {
u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
@@ -515,7 +518,7 @@ static int Lidna_to_unicode(lua_State *L) { /** idna.to_unicode(s) */
if(U_SUCCESS(err) && output_len < 1024) {
lua_pushlstring(L, output, output_len);
} else {
- lua_pushnil(L);
+ luaL_pushfail(L);
}
return 1;
@@ -534,14 +537,14 @@ static int Lskeleton(lua_State *L) {
u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
if(U_FAILURE(err)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
}
dest_len = uspoof_getSkeleton(icu_spoofcheck, 0, ustr, ulen, dest, 1024, &err);
if(U_FAILURE(err)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
}
@@ -552,7 +555,7 @@ static int Lskeleton(lua_State *L) {
return 1;
}
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
}
@@ -569,7 +572,7 @@ static int Lidna_to_ascii(lua_State *L) { /** idna.to_ascii(s) */
int ret;
if(s == NULL || len != strlen(s)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1; /* TODO return error message */
}
@@ -580,7 +583,7 @@ static int Lidna_to_ascii(lua_State *L) { /** idna.to_ascii(s) */
idn_free(output);
return 1;
} else {
- lua_pushnil(L);
+ luaL_pushfail(L);
idn_free(output);
return 1; /* TODO return error message */
}
@@ -597,7 +600,7 @@ static int Lidna_to_unicode(lua_State *L) { /** idna.to_unicode(s) */
idn_free(output);
return 1;
} else {
- lua_pushnil(L);
+ luaL_pushfail(L);
idn_free(output);
return 1; /* TODO return error message */
}
diff --git a/util-src/net.c b/util-src/net.c
index c3b07815..d786e885 100644
--- a/util-src/net.c
+++ b/util-src/net.c
@@ -33,6 +33,9 @@
#if (LUA_VERSION_NUM == 501)
#define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
#endif
+#if (LUA_VERSION_NUM < 504)
+#define luaL_pushfail lua_pushnil
+#endif
/* Enumerate all locally configured IP addresses */
@@ -59,7 +62,7 @@ static int lc_local_addresses(lua_State *L) {
#ifndef _WIN32
if(getifaddrs(&addr) < 0) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushfstring(L, "getifaddrs failed (%d): %s", errno,
strerror(errno));
return 2;
@@ -141,14 +144,14 @@ static int lc_pton(lua_State *L) {
case -1:
errno_ = errno;
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(errno_));
lua_pushinteger(L, errno_);
return 3;
default:
case 0:
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(EINVAL));
lua_pushinteger(L, EINVAL);
return 3;
@@ -170,7 +173,7 @@ static int lc_ntop(lua_State *L) {
family = AF_INET;
}
else {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(EAFNOSUPPORT));
lua_pushinteger(L, EAFNOSUPPORT);
return 3;
@@ -179,7 +182,7 @@ static int lc_ntop(lua_State *L) {
if(!inet_ntop(family, ipaddr, buf, INET6_ADDRSTRLEN))
{
errno_ = errno;
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(errno_));
lua_pushinteger(L, errno_);
return 3;
diff --git a/util-src/poll.c b/util-src/poll.c
index 21cb9581..6c6a4e63 100644
--- a/util-src/poll.c
+++ b/util-src/poll.c
@@ -37,6 +37,9 @@
#if (LUA_VERSION_NUM == 501)
#define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2)
#endif
+#if (LUA_VERSION_NUM < 504)
+#define luaL_pushfail lua_pushnil
+#endif
/*
* Structure to keep state for each type of API
@@ -67,7 +70,7 @@ static int Ladd(lua_State *L) {
int wantwrite = lua_toboolean(L, 4);
if(fd < 0) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(EBADF));
lua_pushinteger(L, EBADF);
return 3;
@@ -84,7 +87,7 @@ static int Ladd(lua_State *L) {
if(ret < 0) {
ret = errno;
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
@@ -96,14 +99,14 @@ static int Ladd(lua_State *L) {
#else
if(fd > FD_SETSIZE) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(EBADF));
lua_pushinteger(L, EBADF);
return 3;
}
if(FD_ISSET(fd, &state->all)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(EEXIST));
lua_pushinteger(L, EEXIST);
return 3;
@@ -160,7 +163,7 @@ static int Lset(lua_State *L) {
}
else {
ret = errno;
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
@@ -169,7 +172,7 @@ static int Lset(lua_State *L) {
#else
if(!FD_ISSET(fd, &state->all)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
return 3;
@@ -218,7 +221,7 @@ static int Ldel(lua_State *L) {
}
else {
ret = errno;
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
@@ -227,7 +230,7 @@ static int Ldel(lua_State *L) {
#else
if(!FD_ISSET(fd, &state->all)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
return 3;
@@ -314,18 +317,20 @@ static int Lwait(lua_State *L) {
#endif
if(ret == 0) {
+ /* Is this an error? */
lua_pushnil(L);
lua_pushstring(L, "timeout");
return 2;
}
else if(ret < 0 && errno == EINTR) {
+ /* Is this an error? */
lua_pushnil(L);
lua_pushstring(L, "signal");
return 2;
}
else if(ret < 0) {
ret = errno;
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
@@ -399,7 +404,7 @@ static int Lnew(lua_State *L) {
int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if(epoll_fd <= 0) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(errno));
lua_pushinteger(L, errno);
return 3;
diff --git a/util-src/pposix.c b/util-src/pposix.c
index 42b553dd..856905b0 100644
--- a/util-src/pposix.c
+++ b/util-src/pposix.c
@@ -64,6 +64,9 @@
#if (LUA_VERSION_NUM < 503)
#define lua_isinteger(L, n) lua_isnumber(L, n)
#endif
+#if (LUA_VERSION_NUM < 504)
+#define luaL_pushfail lua_pushnil
+#endif
#include <fcntl.h>
#if defined(__linux__)
@@ -413,7 +416,7 @@ static int lc_initgroups(lua_State *L) {
struct passwd *p;
if(!lua_isstring(L, 1)) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, "invalid-username");
return 2;
}
@@ -421,7 +424,7 @@ static int lc_initgroups(lua_State *L) {
p = getpwnam(lua_tostring(L, 1));
if(!p) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, "no-such-user");
return 2;
}
@@ -440,7 +443,7 @@ static int lc_initgroups(lua_State *L) {
break;
default:
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, "invalid-gid");
return 2;
}
@@ -450,17 +453,17 @@ static int lc_initgroups(lua_State *L) {
if(ret) {
switch(errno) {
case ENOMEM:
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, "no-memory");
break;
case EPERM:
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, "permission-denied");
break;
default:
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, "unknown-error");
}
} else {
@@ -672,7 +675,7 @@ static int lc_uname(lua_State *L) {
struct utsname uname_info;
if(uname(&uname_info) != 0) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(errno));
return 2;
}
@@ -702,7 +705,7 @@ static int lc_setenv(lua_State *L) {
/* If the second argument is nil or nothing, unset the var */
if(lua_isnoneornil(L, 2)) {
if(unsetenv(var) != 0) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(errno));
return 2;
}
@@ -714,7 +717,7 @@ static int lc_setenv(lua_State *L) {
value = luaL_checkstring(L, 2);
if(setenv(var, value, 1) != 0) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(errno));
return 2;
}
@@ -776,7 +779,7 @@ static int lc_atomic_append(lua_State *L) {
case ENOSPC: /* No space left */
default: /* Other issues */
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(err));
lua_pushinteger(L, err);
return 3;
@@ -803,7 +806,7 @@ static int lc_atomic_append(lua_State *L) {
return luaL_error(L, "atomic_append() failed in ftruncate(): %s", strerror(errno));
}
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushstring(L, strerror(err));
lua_pushinteger(L, err);
return 3;
diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c
index 07e1096f..007aa2ec 100644
--- a/util-src/ringbuffer.c
+++ b/util-src/ringbuffer.c
@@ -6,6 +6,10 @@
#include <lua.h>
#include <lauxlib.h>
+#if (LUA_VERSION_NUM < 504)
+#define luaL_pushfail lua_pushnil
+#endif
+
typedef struct {
size_t rpos; /* read position */
size_t wpos; /* write position */
@@ -152,7 +156,7 @@ static int rb_read(lua_State *L) {
int peek = lua_toboolean(L, 3);
if(r > b->blen) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
}
@@ -204,7 +208,7 @@ static int rb_write(lua_State *L) {
/* Does `l` bytes fit? */
if((l + b->blen) > b->alen) {
- lua_pushnil(L);
+ luaL_pushfail(L);
return 1;
}
diff --git a/util-src/windows.c b/util-src/windows.c
index 89bec57b..57af79d5 100644
--- a/util-src/windows.c
+++ b/util-src/windows.c
@@ -22,6 +22,9 @@
#if (LUA_VERSION_NUM == 501)
#define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
#endif
+#if (LUA_VERSION_NUM < 504)
+#define luaL_pushfail lua_pushnil
+#endif
static int Lget_nameservers(lua_State *L) {
char stack_buffer[1024]; // stack allocated buffer
@@ -45,14 +48,14 @@ static int Lget_nameservers(lua_State *L) {
return 1;
} else {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushfstring(L, "DnsQueryConfig returned %d", status);
return 2;
}
}
static int lerror(lua_State *L, char *string) {
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_pushfstring(L, "%s: %d", string, GetLastError());
return 2;
}