Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time parsing 32bit corner-case #846

Open
matteo-cristino opened this issue Apr 5, 2024 · 2 comments
Open

Time parsing 32bit corner-case #846

matteo-cristino opened this issue Apr 5, 2024 · 2 comments

Comments

@matteo-cristino
Copy link
Collaborator

Since time is an integer, it could take values from -2147483648 to 2147483647, but if I try to do

TIME.new(-2147483648)

the following error is returned

Could not read unix timestamp -2.147484e+09

While this error is not returned in the case I use

TIME.new("-2147483648")

This seems due to some conversion from number to string done internally in lua since, as stated in the documentation, both number and strings are seen as string from lua_isstring function (making also the lua_isnumber branching useless)

Zenroom/src/zen_time.c

Lines 74 to 121 in ca02f6c

ztime_t* time_arg(lua_State *L, int n) {
Z(L);
ztime_t *result = (ztime_t*)malloc(sizeof(ztime_t));
if(result == NULL) {
return NULL;
}
void *ud = luaL_testudata(L, n, "zenroom.time");
if(ud) {
*result = *(ztime_t*)ud;
goto end;
}
if(lua_isstring(L, 1)) {
const char* arg = lua_tostring(L, 1);
char *pEnd;
long l_result = strtol(arg, &pEnd, 10);
if(*pEnd) {
free(result);
lerror(L, "Could not read unix timestamp %s", arg);
return NULL;
} else if (l_result < INT_MIN || l_result > INT_MAX) {
free(result);
lerror(L, "Could not read unix timestamp %s out of range", arg);
return NULL;
}
*result = (ztime_t)l_result;
goto end;
}
// number argument, import
if(lua_isnumber(L, 1)) {
lua_Number number = lua_tonumber(L, 1);
*result = (int)number;
goto end;
}
octet *o = o_arg(L, n);
if(o) {
if(o->len != sizeof(ztime_t)) {
free(result);
zerror(L, "Wrong size timestamp %s", __func__);
return NULL;
}
memcpy(result, o->val, sizeof(ztime_t));
o_free(L, o);
goto end;
}
end:
if(result) Z->memcount_times++;
return result;
}

and thus they both pass thorugh the lua_tostring function. I tried also to move the branch with lua_isnumber before the lua_isstring one, but it seemed that after a certain number, lua was not able to read them correctly.

Thus at the moment, if we use numbers in input we are only loosing the time -2147483648 and using strings in input we should not lose anything. Anyway interesting to notice and maybe look further into it in case any other problems with numbers came up.

@jaromil jaromil changed the title Time parsing not fully working Time parsing 32bit corner-case Apr 12, 2024
@jaromil
Copy link
Member

jaromil commented Nov 11, 2024

Is this recently fenced off by fixing the js 32bit time conversion?

@jaromil
Copy link
Member

jaromil commented Dec 23, 2024

Maybe related, found using ASAN

not ok 15 String dictionary with float and time
# (in test file /home/jrml/devel/zenroom/test/zencode/then.bats, line 265)
#   `cat <<EOF | zexe dictionary_string_float_time.zen dictionary_string_float_time.json' failed with status 134
# {
#     "dictionary": {
#         "str": "hello world!",
#         "num": 12345678910,
#         "time": 1702474699
#     }
# }
# Given I have the 'string dictionary' named 'dictionary'
# Then print all data
# Zenroom v4.46.1 - secure crypto language VM
# Zenroom is Copyright (C) 2017-2024 by the Dyne.org foundation
# reading DATA from file: /tmp/bats-run-MORy4w/file/0/dictionary_string_float_time.json
# EOF after 116 bytes
# loaded file (116 bytes)
# configuration: debug=1,rngseed=hex:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
# reading Zencode from file: /tmp/bats-run-MORy4w/test/15/dictionary_string_float_time.zen
# EOF after 76 bytes
# loaded file (76 bytes)
# Direct Zencode execution
# [W]  Zencode is missing version check, please add: rule check version N.N.N
# src/zen_time.c:183:11: runtime error: 1.23457e+10 is outside the range of representable values of type 'int'
#     #0 0x562762adf2b7 in detect_time_value src/zen_time.c:183
#     #1 0x562762b23433 in precallC /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:529
#     #2 0x562762b246e4 in luaD_precall /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:595
#     #3 0x562762bc3e88 in luaV_execute /home/jrml/devel/zenroom/lib/lua54/src/lvm.c:1682
#     #4 0x562762b25504 in ccall /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:637
#     #5 0x562762b2565f in luaD_callnoyield /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:655
#     #6 0x562762b06db7 in f_call /home/jrml/devel/zenroom/lib/lua54/src/lapi.c:1038
#     #7 0x562762b1bfce in luaD_rawrunprotected /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:144
#     #8 0x562762b29068 in luaD_pcall /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:957
#     #9 0x562762b07222 in lua_pcallk /home/jrml/devel/zenroom/lib/lua54/src/lapi.c:1064
#     #10 0x562762bd35d1 in luaB_pcall /home/jrml/devel/zenroom/lib/lua54/src/lbaselib.c:476
#     #11 0x562762b23433 in precallC /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:529
#     #12 0x562762b246e4 in luaD_precall /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:595
#     #13 0x562762bc3e88 in luaV_execute /home/jrml/devel/zenroom/lib/lua54/src/lvm.c:1682
#     #14 0x562762b25504 in ccall /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:637
#     #15 0x562762b2565f in luaD_callnoyield /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:655
#     #16 0x562762b06db7 in f_call /home/jrml/devel/zenroom/lib/lua54/src/lapi.c:1038
#     #17 0x562762b1bfce in luaD_rawrunprotected /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:144
#     #18 0x562762b29068 in luaD_pcall /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:957
#     #19 0x562762b07222 in lua_pcallk /home/jrml/devel/zenroom/lib/lua54/src/lapi.c:1064
#     #20 0x562762bd35d1 in luaB_pcall /home/jrml/devel/zenroom/lib/lua54/src/lbaselib.c:476
#     #21 0x562762b23433 in precallC /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:529
#     #22 0x562762b246e4 in luaD_precall /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:595
#     #23 0x562762bc3e88 in luaV_execute /home/jrml/devel/zenroom/lib/lua54/src/lvm.c:1682
#     #24 0x562762b25504 in ccall /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:637
#     #25 0x562762b2565f in luaD_callnoyield /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:655
#     #26 0x562762b06db7 in f_call /home/jrml/devel/zenroom/lib/lua54/src/lapi.c:1038
#     #27 0x562762b1bfce in luaD_rawrunprotected /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:144
#     #28 0x562762b29068 in luaD_pcall /home/jrml/devel/zenroom/lib/lua54/src/ldo.c:957
#     #29 0x562762b07222 in lua_pcallk /home/jrml/devel/zenroom/lib/lua54/src/lapi.c:1064
#     #30 0x562762a55aa7 in zen_exec_zencode src/zenroom.c:380
#     #31 0x562762af3d44 in main src/cli-zenroom.c:427
#     #32 0x7f13847ec1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#     #33 0x7f13847ec28a in __libc_start_main_impl ../csu/libc-start.c:360
#     #34 0x562762a51384 in _start (/home/jrml/devel/zenroom/zenroom+0x3e1384) (BuildId: 5763bdd4c63058eaf41f4e887d10d941b1f19501)
#
# SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/zen_time.c:183:11 in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants