Commit 99aa93c7 authored by Jaromil's avatar Jaromil
Browse files

Fix limit for loaded file to be MAX_FILE and increased to 64KiB

Also removed unuseful string tests: safe_string is now a local
function used internally, just checking for NULL or empty string.

There is still a problem when loading binary files where \0 may occur:
they get truncated when instantiated as Lua's string types. Should be
made octets directly.
parent c6bcfb2e
......@@ -28,6 +28,7 @@
#include <jutils.h>
#include <zenroom.h>
#include <zen_error.h>
#include <lua_functions.h>
#ifdef __EMSCRIPTEN__
......@@ -73,16 +74,22 @@ void load_file(char *dst, FILE *fd) {
chunk = MAX_STRING;
if( offset+MAX_STRING>MAX_FILE )
chunk = MAX_FILE-offset-1;
bytes = fread(&dst[offset],sizeof(char),chunk,fd);
if(!chunk) {
warning(0, "File too big, truncated at maximum supported size");
break; }
bytes = fread(&dst[offset],1,chunk,fd);
if(!bytes) {
if(feof(fd)) {
if((fd!=stdin) && (long)offset!=file_size)
if((fd!=stdin) && (long)offset!=file_size) {
warning(0, "Incomplete file read (%u of %u bytes)",
offset, file_size);
else
} else {
func(0, "EOF after %u bytes",offset);
break; }
}
dst[offset] = '\0';
break;
}
if(ferror(fd)) {
error(0, "Error in %s: %s",__func__,strerror(errno));
fclose(fd);
......@@ -94,7 +101,7 @@ void load_file(char *dst, FILE *fd) {
act(0, "loaded file (%u bytes)", offset);
}
char *safe_string(char *str) {
static char *safe_string(char *str, int max) {
int i, length = 0;
if(!str) {
warning(0, "NULL string detected");
......@@ -102,24 +109,11 @@ char *safe_string(char *str) {
if(str[0]=='\0') {
warning(0, "empty string detected");
return NULL; }
while (length < MAX_STRING && str[length] != '\0') ++length;
if (length == MAX_STRING)
warning(0, "maximum size string detected (may be truncated) at address %p",str);
for (i = 0; i < length; ++i) {
if (!isprint(str[i]) && !isspace(str[i])) {
error(0, "unprintable character (ASCII %d) at position %d",
(unsigned char)str[i], i);
return NULL;
}
}
return(str);
return(str);
}
void zen_setenv(lua_State *L, char *key, char *val) {
lua_pushstring(L, val);
lua_pushstring(L, safe_string(val, MAX_FILE) );
lua_setglobal(L, key);
}
......@@ -134,7 +128,7 @@ int zen_add_package(lua_State *L, char *name, lua_CFunction func) {
void zen_add_function(lua_State *L,
lua_CFunction func,
const char *func_name) {
if (!L || !func || !func_name) return;
if (!L || !func || !func_name) return;
lua_pushcfunction(L, func);
lua_setglobal(L, func_name);
}
......
......@@ -87,7 +87,6 @@ extern int umm_integrity_check();
// prototypes from lua_functions.c
extern void load_file(char *dst, FILE *fd);
extern char *safe_string(char *str);
extern void zen_setenv(lua_State *L, char *key, char *val);
extern void zen_add_function(lua_State *L, lua_CFunction func,
const char *func_name);
......@@ -159,16 +158,14 @@ zenroom_t *zen_init(const char *conf,
zen_require_override(L,1);
// load arguments if present
if(data) // avoid errors on NULL args
if(safe_string(data)) {
func(L, "declaring global: DATA");
zen_setenv(L,"DATA",data);
}
if(keys)
if(safe_string(keys)) {
func(L, "declaring global: KEYS");
zen_setenv(L,"KEYS",keys);
}
if(data) {
func(L, "declaring global: DATA");
zen_setenv(L,"DATA",data);
}
if(keys) {
func(L, "declaring global: KEYS");
zen_setenv(L,"KEYS",keys);
}
return(Z);
}
......
......@@ -83,7 +83,7 @@ int zen_exec_script(zenroom_t *Z, const char *script);
void zen_teardown(zenroom_t *zenroom);
#define UMM_HEAP (64*1024) // 64KiB (masked with 0x7fff)
#define MAX_FILE (64*512) // load max 32KiB files
#define MAX_FILE (64*1024) // load max 64KiB files
#define MAX_STRING 4097 // max 4KiB strings
#define MAX_OCTET 2049 // max 2KiB octets
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment