summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-07-02 10:47:49 +0800
committerOpportunityLiu <[email protected]>2019-07-02 10:47:49 +0800
commit51afeebaa78f2dbb9307f53a2533d68cd5491b33 (patch)
tree35e3bf4f28ea23dc5e5133d9ae406f2f7c6db572 /core/src
parent0261c0e50b59980f08cd8185bf2fb84e7248d104 (diff)
add a lot checks
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/io/file.h23
-rw-r--r--core/src/xmake/io/file___len.c4
-rw-r--r--core/src/xmake/io/file_read.c87
-rw-r--r--core/src/xmake/io/file_write.c28
-rw-r--r--core/src/xmake/io/open.c8
-rw-r--r--core/src/xmake/io/std.c6
-rw-r--r--core/src/xmake/winos/ansi.c33
7 files changed, 141 insertions, 48 deletions
diff --git a/core/src/xmake/io/file.h b/core/src/xmake/io/file.h
index 3d8312b31..8348eb136 100644
--- a/core/src/xmake/io/file.h
+++ b/core/src/xmake/io/file.h
@@ -26,6 +26,9 @@
*/
#include "prefix.h"
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * types
+ */
typedef enum __xm_io_file_type_e
{
XM_IO_FILE_TYPE_FILE = 0, //!< disk file
@@ -61,12 +64,17 @@ typedef struct __xm_io_file
tb_char_t const* path;
} xm_io_file;
-#define xm_io_file_is_file(fileref) (fileref->type == XM_IO_FILE_TYPE_FILE)
-#define xm_io_file_is_closed_file(fileref) (fileref->type == XM_IO_FILE_TYPE_FILE && !(fileref->file_ref))
-#define xm_io_file_is_std(fileref) (fileref->type != XM_IO_FILE_TYPE_FILE)
-#define xm_io_file_is_closed_std(fileref) (fileref->type != XM_IO_FILE_TYPE_FILE && !(fileref->file_ref))
-#define xm_io_file_is_closed(fileref) (xm_io_file_is_closed_file(fileref) || xm_io_file_is_closed_std(fileref))
-#define xm_io_file_is_tty(fileref) (!!(fileref->type & XM_IO_FILE_FLAG_TTY))
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * macros
+ */
+#define xm_io_file_is_file(fileref) (fileref->type == XM_IO_FILE_TYPE_FILE)
+#define xm_io_file_is_std(fileref) (fileref->type != XM_IO_FILE_TYPE_FILE)
+
+#define xm_io_file_is_closed_file(fileref) (fileref->type == XM_IO_FILE_TYPE_FILE && !(fileref->file_ref))
+#define xm_io_file_is_closed_std(fileref) (fileref->type != XM_IO_FILE_TYPE_FILE && !(fileref->file_ref))
+
+#define xm_io_file_is_tty(fileref) (!!(fileref->type & XM_IO_FILE_FLAG_TTY))
+#define xm_io_file_is_closed(fileref) (xm_io_file_is_closed_file(fileref) || xm_io_file_is_closed_std(fileref))
#define xm_io_file_udata "XM_IO_FILE*"
@@ -86,6 +94,9 @@ typedef struct __xm_io_file
#define xm_io_file_error_closed(lua) xm_io_file_error(lua, "file has been closed")
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * interfaces
+ */
static inline xm_io_file* xm_io_newfile(lua_State* lua)
{
tb_assert_and_check_return_val(lua, tb_null);
diff --git a/core/src/xmake/io/file___len.c b/core/src/xmake/io/file___len.c
index 98a715a0e..fa454382e 100644
--- a/core/src/xmake/io/file___len.c
+++ b/core/src/xmake/io/file___len.c
@@ -28,8 +28,8 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
-#include "prefix.h"
#include "file.h"
+#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
@@ -44,7 +44,7 @@ tb_int_t xm_io_file___len(lua_State* lua)
tb_assert_and_check_return_val(lua, 0);
xm_io_file* file = xm_io_getfile(lua);
- if(xm_io_file_is_closed(file))
+ if (xm_io_file_is_closed(file))
xm_io_file_error_closed(lua);
else if (xm_io_file_is_file(file))
{
diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c
index 5727b2b16..754a40fdf 100644
--- a/core/src/xmake/io/file_read.c
+++ b/core/src/xmake/io/file_read.c
@@ -52,6 +52,8 @@
static tb_size_t detect_charset(tb_byte_t const** data_ptr, tb_long_t size)
{
+ tb_assert(data_ptr && *data_ptr);
+
tb_byte_t const* data = *data_ptr;
tb_size_t charset = TB_CHARSET_TYPE_NONE;
@@ -153,6 +155,8 @@ static tb_size_t detect_charset(tb_byte_t const** data_ptr, tb_long_t size)
static tb_byte_t const* find_lf(tb_byte_t const* buf, tb_size_t buflen, tb_size_t encoding)
{
+ tb_assert(buf);
+
tb_byte_t const* bufend = buf + buflen;
tb_bool_t success = tb_false;
switch (encoding)
@@ -219,9 +223,9 @@ static tb_int_t buffer_pushline(luaL_Buffer* buf, xm_io_file* file, tb_char_t co
tb_size_t conlen = tb_strlen(continuation);
tb_buffer_t readdata;
- tb_buffer_init(&readdata);
+ tb_assert_and_check_return_val(tb_buffer_init(&readdata), 0);
tb_buffer_t transdata;
- tb_buffer_init(&transdata);
+ tb_assert_and_check_return_val(tb_buffer_init(&transdata), 0);
// transcode is redundant, write to transdata directly
tb_bool_t notrans = charset == TB_CHARSET_TYPE_UTF8 || binary;
while (1)
@@ -273,12 +277,12 @@ static tb_int_t buffer_pushline(luaL_Buffer* buf, xm_io_file* file, tb_char_t co
#ifdef TB_CONFIG_OS_WINDOWS
else if (charset == XM_IO_FILE_ENCODING_ANSI)
{
- tb_long_t dst_size = 0;
- tb_size_t dst_maxn = len * 3;
- tb_char_t* dst_data = (tb_char_t*)tb_buffer_resize(&transdata, dst_maxn + 1);
- if (dst_data && dst_maxn &&
- (dst_size = xm_mbstoutf8(dst_data, (tb_char_t const*)tb_buffer_data(&readdata), dst_maxn, GetACP())) >=
- 0 &&
+ tb_long_t dst_size = 0;
+ tb_size_t dst_maxn = len * 3;
+ tb_char_t* dst_data = (tb_char_t*)tb_buffer_resize(&transdata, dst_maxn + 1);
+ tb_char_t const* src_data = (tb_char_t const*)tb_buffer_data(&readdata);
+ tb_assert(src_data && dst_data);
+ if (dst_data && dst_maxn && (dst_size = xm_mbstoutf8(dst_data, src_data, dst_maxn, GetACP())) >= 0 &&
dst_size < dst_maxn)
{
len = dst_size;
@@ -293,12 +297,14 @@ static tb_int_t buffer_pushline(luaL_Buffer* buf, xm_io_file* file, tb_char_t co
else
{
// convert string
- tb_long_t dst_size = 0;
- tb_size_t dst_maxn = len * 3;
- tb_byte_t* dst_data = tb_buffer_resize(&transdata, dst_maxn + 1);
+ tb_long_t dst_size = 0;
+ tb_size_t dst_maxn = len * 3;
+ tb_byte_t* dst_data = tb_buffer_resize(&transdata, dst_maxn + 1);
+ tb_byte_t const* src_data = (tb_byte_t const*)tb_buffer_data(&readdata);
+ tb_assert(src_data && dst_data);
if (dst_data && dst_maxn &&
- (dst_size = tb_charset_conv_data(charset, TB_CHARSET_TYPE_UTF8, tb_buffer_data(&readdata), len,
- dst_data, dst_maxn)) >= 0 &&
+ (dst_size = tb_charset_conv_data(charset, TB_CHARSET_TYPE_UTF8, src_data, len, dst_data, dst_maxn)) >=
+ 0 &&
dst_size < dst_maxn)
{
len = dst_size;
@@ -310,6 +316,7 @@ static tb_int_t buffer_pushline(luaL_Buffer* buf, xm_io_file* file, tb_char_t co
}
}
tb_byte_t* buf = tb_buffer_data(&transdata);
+ tb_assert(buf);
if (buf[len - 1] != '\n')
{
// end of file, no lf found
@@ -346,6 +353,8 @@ static tb_int_t buffer_pushline(luaL_Buffer* buf, xm_io_file* file, tb_char_t co
static tb_int_t read_all(lua_State* lua, xm_io_file* file, tb_char_t const* continuation)
{
+ tb_assert(lua && file && continuation);
+
luaL_Buffer sbuf, *buf = &sbuf;
luaL_buffinit(lua, buf);
tb_bool_t has_content = tb_false;
@@ -367,6 +376,8 @@ static tb_int_t read_all(lua_State* lua, xm_io_file* file, tb_char_t const* cont
static tb_int_t read_line(lua_State* lua, xm_io_file* file, tb_char_t const* continuation, tb_bool_t keep_crlf)
{
+ tb_assert(lua && file && continuation);
+
luaL_Buffer sbuf, *buf = &sbuf;
luaL_buffinit(lua, buf);
tb_bool_t has_content = tb_false;
@@ -388,6 +399,8 @@ static tb_int_t read_line(lua_State* lua, xm_io_file* file, tb_char_t const* con
static tb_int_t read_n(lua_State* lua, xm_io_file* file, tb_char_t const* continuation, tb_long_t n)
{
+ tb_assert(lua && file && continuation);
+
if (*continuation != '\0') xm_io_file_error(lua, "continuation is not supported for read number of bytes");
tb_size_t charset = file->encoding;
tb_bool_t binary = charset == XM_IO_FILE_ENCODING_BINARY || charset == XM_IO_FILE_ENCODING_UNKNOWN;
@@ -409,9 +422,10 @@ static tb_int_t read_n(lua_State* lua, xm_io_file* file, tb_char_t const* contin
else
{
tb_buffer_t buf;
- tb_buffer_init(&buf);
- tb_byte_t* bufptr = tb_buffer_resize(&buf, n + 1);
- tb_long_t readsize = tb_file_read(file->file_ref, bufptr, n);
+ tb_assert_and_check_return_val(tb_buffer_init(&buf), 0);
+ tb_byte_t* bufptr = tb_buffer_resize(&buf, n + 1);
+ tb_assert(bufptr);
+ tb_long_t readsize = tb_file_read(file->file_ref, bufptr, n);
if (readsize > 0)
lua_pushlstring(lua, (tb_char_t const*)bufptr, readsize);
else
@@ -424,6 +438,8 @@ static tb_int_t read_n(lua_State* lua, xm_io_file* file, tb_char_t const* contin
static tb_size_t std_buffer_pushline(luaL_Buffer* buf, xm_io_file* file, tb_char_t const* continuation,
tb_bool_t keep_crlf)
{
+ tb_assert(lua && file && continuation);
+
tb_char_t strbuf[8192];
tb_size_t buflen = 0;
tb_size_t result = PL_FAIL;
@@ -472,6 +488,8 @@ static tb_size_t std_buffer_pushline(luaL_Buffer* buf, xm_io_file* file, tb_char
static tb_int_t std_read_line(lua_State* lua, xm_io_file* file, tb_char_t const* continuation, tb_bool_t keep_crlf)
{
+ tb_assert(lua && file && continuation);
+
luaL_Buffer sbuf, *buf = &sbuf;
luaL_buffinit(lua, buf);
tb_bool_t has_content = tb_false;
@@ -493,6 +511,8 @@ static tb_int_t std_read_line(lua_State* lua, xm_io_file* file, tb_char_t const*
static tb_int_t std_read_all(lua_State* lua, xm_io_file* file, tb_char_t const* continuation)
{
+ tb_assert(lua && file && continuation);
+
luaL_Buffer sbuf, *buf = &sbuf;
luaL_buffinit(lua, buf);
tb_bool_t has_content = tb_false;
@@ -514,6 +534,8 @@ static tb_int_t std_read_all(lua_State* lua, xm_io_file* file, tb_char_t const*
static tb_int_t std_read_n(lua_State* lua, xm_io_file* file, tb_char_t const* continuation, tb_long_t n)
{
+ tb_assert(lua && file && continuation);
+
if (*continuation != '\0') xm_io_file_error(lua, "continuation is not supported for std streams");
if (n == 0)
{
@@ -533,20 +555,26 @@ static tb_int_t std_read_n(lua_State* lua, xm_io_file* file, tb_char_t const* co
}
#ifdef TB_CONFIG_OS_WINDOWS
tb_buffer_t readbuf, transbuf;
- tb_buffer_init(&readbuf);
+ tb_assert_and_check_return_val(tb_buffer_init(&readbuf), 0);
tb_wchar_t* readbuf_ptr = (tb_wchar_t*)tb_buffer_resize(&readbuf, (tb_size_t)((n + 1) * sizeof(tb_wchar_t)));
- tb_size_t readcount = fread(readbuf_ptr, sizeof(tb_wchar_t), n, file->std_ref);
- readbuf_ptr[readcount] = L'\0'; // add null termination for tb_wcstombs
- tb_buffer_init(&transbuf);
+ tb_assert(readbuf_ptr);
+
+ tb_size_t readcount = fread(readbuf_ptr, sizeof(tb_wchar_t), n, file->std_ref);
+ readbuf_ptr[readcount] = L'\0'; // add null termination for tb_wcstombs
+ tb_assert_and_check_return_val(tb_buffer_init(&transbuf), 0);
tb_char_t* transbuf_ptr = (tb_char_t*)tb_buffer_resize(&transbuf, (tb_size_t)(n * 3));
- tb_size_t transcount = tb_wcstombs(transbuf_ptr, readbuf_ptr, (tb_size_t)(n * 3));
+ tb_assert(transbuf_ptr);
+
+ tb_size_t transcount = tb_wcstombs(transbuf_ptr, readbuf_ptr, (tb_size_t)(n * 3));
tb_buffer_exit(&readbuf);
lua_pushlstring(lua, transbuf_ptr, transcount);
tb_buffer_exit(&transbuf);
#else
tb_buffer_t buf;
- tb_buffer_init(&buf);
+ tb_assert_and_check_return_val(tb_buffer_init(&buf), 0);
tb_char_t* buf_ptr = (tb_char_t*)tb_buffer_resize(&buf, (tb_size_t)n);
+ tb_assert(buf_ptr);
+
tb_size_t readcount = fread(buf_ptr, sizeof(tb_byte_t), (tb_size_t)n, file->std_ref);
lua_pushlstring(lua, buf_ptr, readcount);
tb_buffer_exit(&buf);
@@ -556,6 +584,8 @@ static tb_int_t std_read_n(lua_State* lua, xm_io_file* file, tb_char_t const* co
static tb_int_t std_read_num(lua_State* lua, xm_io_file* file, tb_char_t const* continuation)
{
+ tb_assert(lua && file && continuation);
+
if (*continuation != '\0') xm_io_file_error(lua, "continuation is not supported for std streams");
tb_double_t d;
#ifdef TB_CONFIG_OS_WINDOWS
@@ -593,11 +623,14 @@ tb_int_t xm_io_file_read(lua_State* lua)
tb_char_t const* mode = luaL_optstring(lua, 2, "l");
tb_char_t const* continuation = luaL_optstring(lua, 3, "");
- tb_assert_and_check_return_val(file && mode && continuation, 0);
+ tb_assert_and_check_return_val(mode && continuation, 0);
tb_long_t count = -1;
if (lua_isnumber(lua, 2))
+ {
count = (tb_long_t)lua_tointeger(lua, 2);
+ if (count < 0) xm_io_file_error(lua, "invalid read size, must be positive nubmber or 0");
+ }
else if (*mode == '*')
mode++;
@@ -629,8 +662,8 @@ tb_int_t xm_io_file_read(lua_State* lua)
case 'a': return read_all(lua, file, continuation);
case 'L': return read_line(lua, file, continuation, tb_true);
case 'n': xm_io_file_error(lua, "read number is not implemented");
- case 'l':
- default: return read_line(lua, file, continuation, tb_false);
+ case 'l': return read_line(lua, file, continuation, tb_false);
+ default: xm_io_file_error(lua, "unknonwn read mode");
}
}
if (count >= 0) return std_read_n(lua, file, continuation, count);
@@ -639,7 +672,7 @@ tb_int_t xm_io_file_read(lua_State* lua)
case 'a': return std_read_all(lua, file, continuation);
case 'L': return std_read_line(lua, file, continuation, tb_true);
case 'n': return std_read_num(lua, file, continuation);
- case 'l':
- default: return std_read_line(lua, file, continuation, tb_false);
+ case 'l': return std_read_line(lua, file, continuation, tb_false);
+ default: xm_io_file_error(lua, "unknonwn read mode");
}
}
diff --git a/core/src/xmake/io/file_write.c b/core/src/xmake/io/file_write.c
index 6fe4620bb..c7a8b74fa 100644
--- a/core/src/xmake/io/file_write.c
+++ b/core/src/xmake/io/file_write.c
@@ -43,14 +43,20 @@
static tb_void_t direct_write(xm_io_file* file, tb_char_t const* data, tb_size_t size)
{
+ tb_assert(file && data);
+
tb_file_writ(file->file_ref, (tb_byte_t const*)data, size);
}
static tb_void_t transcode_write(xm_io_file* file, tb_char_t const* data, tb_size_t size)
{
+ tb_assert(file && data);
+
tb_buffer_t buf;
- tb_buffer_init(&buf);
- tb_byte_t* buf_ptr = tb_buffer_resize(&buf, (size + 1) * 2);
- tb_long_t buf_size = tb_charset_conv_cstr(TB_CHARSET_TYPE_UTF8, file->encoding, data, buf_ptr, size * 2);
+ tb_assert_and_check_return(tb_buffer_init(&buf));
+
+ tb_byte_t* buf_ptr = tb_buffer_resize(&buf, (size + 1) * 2);
+ tb_assert(buf_ptr);
+ tb_long_t buf_size = tb_charset_conv_cstr(TB_CHARSET_TYPE_UTF8, file->encoding, data, buf_ptr, size * 2);
if (buf_size > 0) tb_file_writ(file->file_ref, buf_ptr, (tb_size_t)buf_size);
tb_buffer_exit(&buf);
return;
@@ -58,8 +64,10 @@ static tb_void_t transcode_write(xm_io_file* file, tb_char_t const* data, tb_siz
static tb_void_t std_write(xm_io_file* file, tb_char_t const* data, tb_size_t size)
{
+ tb_assert(file && data);
+
tb_size_t type = (file->type & ~XM_IO_FILE_FLAG_TTY);
- tb_check_return(type != XM_IO_FILE_TYPE_STDIN);
+ tb_assert_and_check_return(type != XM_IO_FILE_TYPE_STDIN);
#ifdef TB_CONFIG_OS_WINDOWS
HANDLE handle = INVALID_HANDLE_VALUE;
@@ -68,7 +76,7 @@ static tb_void_t std_write(xm_io_file* file, tb_char_t const* data, tb_size_t si
case XM_IO_FILE_TYPE_STDOUT: handle = GetStdHandle(STD_OUTPUT_HANDLE); break;
case XM_IO_FILE_TYPE_STDERR: handle = GetStdHandle(STD_ERROR_HANDLE); break;
}
- tb_check_return(handle != INVALID_HANDLE_VALUE);
+ tb_assert_and_check_return(handle != INVALID_HANDLE_VALUE);
// write string to stdout
tb_size_t writ = 0;
@@ -76,12 +84,13 @@ static tb_void_t std_write(xm_io_file* file, tb_char_t const* data, tb_size_t si
// write to the stdout
DWORD real = 0;
tb_buffer_t wbuf;
- tb_buffer_init(&wbuf);
+ tb_assert_and_check_return(tb_buffer_init(&wbuf));
if (xm_io_file_is_tty(file))
{
// write to console
tb_wchar_t* wdata = (tb_wchar_t*)tb_buffer_resize(&wbuf, (size + 1) * 2);
- tb_size_t wsize = tb_mbstowcs(wdata, data, size);
+ tb_assert(wdata);
+ tb_size_t wsize = tb_mbstowcs(wdata, data, size);
while (writ < wsize)
{
if (!WriteConsoleW(handle, wdata + writ, (DWORD)(wsize - writ), &real, tb_null)) break;
@@ -93,7 +102,8 @@ static tb_void_t std_write(xm_io_file* file, tb_char_t const* data, tb_size_t si
{
// write to redirected file
tb_char_t* wdata = (tb_char_t*)tb_buffer_resize(&wbuf, (size + 1) * 4);
- tb_size_t wsize = xm_utf8tombs(wdata, data, size * 4, GetConsoleOutputCP());
+ tb_assert(wdata);
+ tb_size_t wsize = xm_utf8tombs(wdata, data, size * 4, GetConsoleOutputCP());
while (writ < wsize)
{
if (!WriteFile(handle, wdata + writ, (DWORD)(wsize - writ), &real, tb_null)) break;
@@ -130,6 +140,8 @@ tb_int_t xm_io_file_write(lua_State* lua)
{
size_t datasize;
tb_char_t const* data = luaL_checklstring(lua, i, &datasize);
+ if (!datasize) continue;
+ tb_assert(data);
if (xm_io_file_is_std(file))
std_write(file, data, (tb_size_t)datasize);
diff --git a/core/src/xmake/io/open.c b/core/src/xmake/io/open.c
index 7cc506b69..4e02e4f24 100644
--- a/core/src/xmake/io/open.c
+++ b/core/src/xmake/io/open.c
@@ -40,10 +40,12 @@
*/
tb_int_t xm_io_open(lua_State* lua)
{
+ tb_assert_and_check_return_val(lua, 0);
+
tb_char_t const* path = luaL_checkstring(lua, 1);
tb_char_t const* mode = luaL_optstring(lua, 2, "r");
- tb_check_return_val(path && mode, 0);
+ tb_assert_and_check_return_val(path && mode, 0);
tb_size_t tb_mode = TB_FILE_MODE_RW;
switch (mode[0])
@@ -86,7 +88,7 @@ tb_int_t xm_io_open(lua_State* lua)
xm_file->type = XM_IO_FILE_TYPE_FILE;
xm_file->encoding = enc;
xm_file->path = savedfull;
- tb_strcpy(xm_file->name, "file: ");
+ tb_strlcpy(xm_file->name, "file: ", tb_arrayn(xm_file->name));
if (pathlen < tb_arrayn(xm_file->name) - tb_arrayn("file: "))
{
tb_strcat(xm_file->name, full);
@@ -101,7 +103,7 @@ tb_int_t xm_io_open(lua_State* lua)
}
else
{
- tb_free(savedfull);
+ if (savedfull) tb_free(savedfull);
lua_pushnil(lua);
lua_pushliteral(lua, "failed to open file.");
return 2;
diff --git a/core/src/xmake/io/std.c b/core/src/xmake/io/std.c
index 583a8f1f9..0e5ed2e12 100644
--- a/core/src/xmake/io/std.c
+++ b/core/src/xmake/io/std.c
@@ -69,7 +69,7 @@ static tb_size_t xm_isatty(tb_size_t type)
static void xm_init(lua_State* lua, tb_size_t type)
{
- tb_check_return(lua);
+ tb_assert_and_check_return(lua);
tb_char_t const *name, *path;
FILE* fp;
@@ -105,11 +105,13 @@ static void xm_init(lua_State* lua, tb_size_t type)
file->path = path;
file->std_ref = fp;
tb_char_t const* info = xm_io_file_is_tty(file) ? "" : " redirected";
- tb_sprintf(file->name, "file: (%s%s)", name, info);
+ tb_snprintf(file->name, tb_arrayn(file->name), "file: (%s%s)", name, info);
}
tb_int_t xm_io_std(lua_State* lua)
{
+ tb_assert_and_check_return_val(lua, 0);
+
lua_getglobal(lua, "io");
xm_init(lua, XM_IO_FILE_TYPE_STDIN);
xm_init(lua, XM_IO_FILE_TYPE_STDOUT);
diff --git a/core/src/xmake/winos/ansi.c b/core/src/xmake/winos/ansi.c
index 1973fdc82..63ed85d1f 100644
--- a/core/src/xmake/winos/ansi.c
+++ b/core/src/xmake/winos/ansi.c
@@ -39,6 +39,9 @@ static tb_int_t xm_expand_cp(tb_int_t cp);
tb_int_t xm_winos_console_cp(lua_State* lua)
{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
tb_int_t n = lua_gettop(lua);
if (n >= 1)
{
@@ -59,6 +62,9 @@ tb_int_t xm_winos_console_cp(lua_State* lua)
tb_int_t xm_winos_console_output_cp(lua_State* lua)
{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
tb_int_t n = lua_gettop(lua);
if (n >= 1)
{
@@ -79,6 +85,9 @@ tb_int_t xm_winos_console_output_cp(lua_State* lua)
tb_int_t xm_winos_cp_info(lua_State* lua)
{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
tb_int_t n = lua_gettop(lua);
lua_Integer cp = luaL_checkinteger(lua, 1);
luaL_argcheck(lua, cp >= 0 && cp < 65536, 1, "invalid code page");
@@ -130,6 +139,9 @@ tb_int_t xm_winos_cp_info(lua_State* lua)
tb_int_t xm_winos_ansi_cp(lua_State* lua)
{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
tb_uint_t cp = GetACP();
lua_pushinteger(lua, (lua_Integer)cp);
return 1;
@@ -137,6 +149,9 @@ tb_int_t xm_winos_ansi_cp(lua_State* lua)
tb_int_t xm_winos_oem_cp(lua_State* lua)
{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
tb_uint_t cp = GetOEMCP();
lua_pushinteger(lua, (lua_Integer)cp);
return 1;
@@ -144,6 +159,9 @@ tb_int_t xm_winos_oem_cp(lua_State* lua)
tb_int_t xm_winos_mbstoutf8(lua_State* lua)
{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
tb_int_t n = lua_gettop(lua);
if (n == 0)
{
@@ -169,6 +187,9 @@ tb_int_t xm_winos_mbstoutf8(lua_State* lua)
tb_size_t xm_wcstoutf8(tb_char_t* s1, tb_wchar_t const* s2, tb_size_t n)
{
+ // check
+ tb_assert_and_check_return_val(s1 && s2, 0);
+
if (*s2 == 0)
{
if (n > 0) *s1 = 0;
@@ -181,6 +202,9 @@ tb_size_t xm_wcstoutf8(tb_char_t* s1, tb_wchar_t const* s2, tb_size_t n)
tb_size_t xm_utf8towcs(tb_wchar_t* s1, tb_char_t const* s2, tb_size_t n)
{
+ // check
+ tb_assert_and_check_return_val(s1 && s2, 0);
+
if (*s2 == 0)
{
if (n > 0) *s1 = 0;
@@ -193,6 +217,9 @@ tb_size_t xm_utf8towcs(tb_wchar_t* s1, tb_char_t const* s2, tb_size_t n)
tb_size_t xm_mbstoutf8(tb_char_t* s1, tb_char_t const* s2, tb_size_t n, tb_int_t mbs_cp)
{
+ // check
+ tb_assert_and_check_return_val(s1 && s2, 0);
+
if (*s2 == 0)
{
if (n > 0) *s1 = 0;
@@ -209,6 +236,9 @@ tb_size_t xm_mbstoutf8(tb_char_t* s1, tb_char_t const* s2, tb_size_t n, tb_int_t
tb_size_t xm_utf8tombs(tb_char_t* s1, tb_char_t const* s2, tb_size_t n, tb_int_t mbs_cp)
{
+ // check
+ tb_assert_and_check_return_val(s1 && s2, 0);
+
if (*s2 == 0)
{
if (n > 0) *s1 = 0;
@@ -225,6 +255,9 @@ tb_size_t xm_utf8tombs(tb_char_t* s1, tb_char_t const* s2, tb_size_t n, tb_int_t
static tb_int_t xm_expand_cp(tb_int_t cp)
{
+ // check
+ tb_assert_and_check_return_val(cp >= 0 && cp <= 65535, 0);
+
if (cp == CP_OEMCP) return GetOEMCP();
if (cp == CP_ACP) return GetACP();
return cp;