aboutsummaryrefslogtreecommitdiff
path: root/targets
diff options
context:
space:
mode:
authorAkos Kiss <akiss@inf.u-szeged.hu>2020-03-27 11:03:28 +0100
committerGitHub <noreply@github.com>2020-03-27 11:03:28 +0100
commitf29e6f9020d89d5b071ada8a59a962ce78a656d3 (patch)
tree32a74bfeeeb0eb3907e25c027f3358c5669f8908 /targets
parent18a4cba06299e32fc718f85601185846b45f83ac (diff)
Fix undefined overflow behavior when converting double to integer (#3629)
Overflows in conversions from floating-point to integer are undefined behavior in the C99 standard. (Clause 6.3.1.4: "If the value of the integral part cannot be represented by the integer type, the behavior is undefined.") When UBSAN is enabled, this gets reported at `srand()` calls. (The random seed is usually initialized using the date port API, which represents dates as `double`s. But `srand` takes an `unsigned int`. A simple cast from `double` to `unsigned` becomes undefined behavior if the value is too large. And "now" is too large nowadays. So, effectively, all executions start with an undefined behavior.) This patch fixes this by casting the floating-point value of the date to an integer through a union. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
Diffstat (limited to 'targets')
-rw-r--r--targets/curie_bsp/jerry_app/quark/main.c3
-rw-r--r--targets/esp8266/user/jerry_run.c3
-rw-r--r--targets/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp3
-rw-r--r--targets/riot-stm32f4/source/main-riotos.c3
-rw-r--r--targets/zephyr/src/main-zephyr.c3
5 files changed, 10 insertions, 5 deletions
diff --git a/targets/curie_bsp/jerry_app/quark/main.c b/targets/curie_bsp/jerry_app/quark/main.c
index 2aee9143..2975bbb0 100644
--- a/targets/curie_bsp/jerry_app/quark/main.c
+++ b/targets/curie_bsp/jerry_app/quark/main.c
@@ -136,7 +136,8 @@ void eval_jerry_script (int argc, char *argv[], struct tcmd_handler_ctx *ctx)
void jerry_start ()
{
- srand ((unsigned) jerry_port_get_current_time ());
+ union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
+ srand (now.u);
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t global_obj_val = jerry_get_global_object ();
jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print");
diff --git a/targets/esp8266/user/jerry_run.c b/targets/esp8266/user/jerry_run.c
index ab70ff76..e7fff4c3 100644
--- a/targets/esp8266/user/jerry_run.c
+++ b/targets/esp8266/user/jerry_run.c
@@ -26,7 +26,8 @@ static const char* fn_sys_loop_name = "sysloop";
void js_entry ()
{
- srand ((unsigned) jerry_port_get_current_time ());
+ union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
+ srand (now.u);
jerry_init (JERRY_INIT_EMPTY);
js_register_functions ();
diff --git a/targets/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp b/targets/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp
index 1acf2f16..4c70be88 100644
--- a/targets/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp
+++ b/targets/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp
@@ -68,7 +68,8 @@ static int load_javascript() {
}
int jsmbed_js_init() {
- srand ((unsigned) jerry_port_get_current_time());
+ union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
+ srand (now.u);
jerry_init_flag_t flags = JERRY_INIT_EMPTY;
jerry_init(flags);
diff --git a/targets/riot-stm32f4/source/main-riotos.c b/targets/riot-stm32f4/source/main-riotos.c
index d77aa274..28b9df84 100644
--- a/targets/riot-stm32f4/source/main-riotos.c
+++ b/targets/riot-stm32f4/source/main-riotos.c
@@ -98,7 +98,8 @@ const shell_command_t shell_commands[] = {
int main (void)
{
- srand ((unsigned) jerry_port_get_current_time ());
+ union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
+ srand (now.u);
printf ("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
printf ("This board features a(n) %s MCU.\n", RIOT_MCU);
diff --git a/targets/zephyr/src/main-zephyr.c b/targets/zephyr/src/main-zephyr.c
index 6ea0b846..e250f9d3 100644
--- a/targets/zephyr/src/main-zephyr.c
+++ b/targets/zephyr/src/main-zephyr.c
@@ -79,7 +79,8 @@ static int shell_cmd_handler (char *source_buffer)
void main (void)
{
- srand ((unsigned) jerry_port_get_current_time ());
+ union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
+ srand (now.u);
uint32_t zephyr_ver = sys_kernel_version_get ();
printf ("JerryScript build: " __DATE__ " " __TIME__ "\n");
printf ("JerryScript API %d.%d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION, JERRY_API_PATCH_VERSION);